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!






















