Goose97

Goose97

Avoid redundant database roundtrips

Hi everyone. I’m designing a web backend service in Elixir and having some problems. This question is not unique for Elixir language but I will post it here anyway to see if we have a solution written in Elixir.
Here’s my case:
Let’s say my business has some coupons that users can apply. To simplify, the coupon apply procedure can be broken down into two steps: validate the coupon and increase the claim counter. I would like to split the above two steps into two separate functions, each exposes a minimal interface (Eg: we can simply pass the coupon_id to the function). But doing so will result in two roundtrips to database to fetch the coupon struct and most of the time, the second one is unnecessary.
The solution that I have in my head now is to combined these two functions into a third one validate_and_claim. But that approach contains a lot of duplication code.
So is there any better approach to this problem? I know there won’t be a perfect on which is the best of both worlds but I’m willing to consider other solutions and their trade-offs.
Thanks and have a nice day!

Marked As Solved

kokolegorille

kokolegorille

If You are validating coupon, why pass coupon_id? Just pass a coupon struct. This way, no need to call db.

And You could pattern match on Coupon to be sure You are working with a valid struct.

validate_coupon(%Coupon{} = coupon)

This would put side effect (loading from db) outside the function, making it pure.

And if You return the updated coupon, You could chain…

coupon_id
|> load_from_db()
|> validate_coupon()
|> increase_counter()

Also Liked

kokolegorille

kokolegorille

I think the previous post from @al2o3cr is a good advice.

Most prefer to have a functional core, free of side effects, as it is easier to test.

That means pushing db calls out of the core.

Anyway, I don’t see the advantage of passing the id, because sooner or later You will use it to make db call.

NobbZ

NobbZ

A multi will not reduce roundtrips, it will just have them in a single transaction.

al2o3cr

al2o3cr

IMO passing around IDs is not really “minimal”; a better alternative is to convert those incoming parameters into domain structs as soon as possible in the controller and pass the structs around in the business logic.

Last Post!

kokolegorille

kokolegorille

I think the previous post from @al2o3cr is a good advice.

Most prefer to have a functional core, free of side effects, as it is easier to test.

That means pushing db calls out of the core.

Anyway, I don’t see the advantage of passing the id, because sooner or later You will use it to make db call.

Where Next?

Popular in Questions Top

JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New

Other popular topics Top

baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 44167 214
New

We're in Beta

About us Mission Statement