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.

Where Next?

Popular in Questions Top

stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
Werner
Hi, I’m using Ubuntu 18.04 and after updating to OTP-24.0 yesterday i have this warning when I run “mix local.hex”: 14:57:30.512 [warn] ...
New
quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
johnnyicon
Hi all, I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I'm trying to use Postg...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New

Other popular topics Top

shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 35421 110
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
Nvim
Elixir appears to be a superior language to Python. I don’t see any advantage of Python over Elixir. Are there any?
New

We're in Beta

About us Mission Statement