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

marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New

Other popular topics Top

marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
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
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
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
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New

We're in Beta

About us Mission Statement