sezaru

sezaru

Handle multiple atomically operations with side-effects

Hello,

Sometimes in my system, I want to change something in the database and also do some other side-effect, for example, filling a cache or sending a message, etc.

Normally what I do is something like this:

alias Ecto.Multi

Multi.new()
|> Multi.insert(:insert_data_to_db, data)
|> Multi.run(:update_cache, fn _, _ -> :ok = MyCache.update(data) end)
|> Repo.transaction(timeout: :infinity)

This works great since if my cache update fails for some reason, my insertion will fail too, avoiding situations that I would have successfully inserted my data into the database and failed to update the cache leaving it into an inconsistent state.

Now, let’s say that I want to also send the data to another process that needs to be done atomically too (it can be another thing too, the process is just an example, but it can be anything that needs to be added atomically).

alias Ecto.Multi

Multi.new()
|> Multi.insert(:insert_data_to_db, data)
|> Multi.run(:update_cache, fn _, _ -> :ok = MyCache.update(data) end)
|> Multi.run(:send_data, fn _, _ -> :ok = SendData.send(data) end)
|> Repo.transaction(timeout: :infinity)

Now the approach above doesn’t work anymore since if send data fails, the database will rollback, but the cache will not.

My question is, how do you guys handle this type of situation that you need to do multiple things and it all needs to be in a “apply all or apply nothing if something fails” manner?

Also, this example I used Ecto, but it can be another database too, or even without a database at all.

Another example, I have one situation that I store the last ids of some data I receive in my node, and I also need to send this data to clients via notification. So I first send the notifications then add they ids to the mnesia database, but if the notifications are sent correctly but my mnesia write fails, then again I would have an inconsistent state.

These types of situations seem very common to me, so I’m really wondering if there is a better solution and how you guys handle it.

Marked As Solved

mindok

mindok

Also Liked

sezaru

sezaru

That’s amazing! Basically what I wanted, thanks a lot of the suggestion!

Where Next?

Popular in Questions Top

New
mgjohns61585
Could someone help me? I’m making my first elixir program, number guessing game. I can’t figure out how to convert the user’s guess from ...
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 I...
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
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a > b) do {:ok, "a"} end if (a < b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
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

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
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 47930 226
New
Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 126479 1222
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

We're in Beta

About us Mission Statement