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!

Last Post!

sezaru

sezaru

Oh yeah, for sure it’s more robust.

But at the same time it still kinda hard to see the best approach for some cases that it’s not obvious to do the rollback.

For example, let’s say one of your operation is to replace the current value of a row(s) inside the database. After you replaced it, you lost the old information, so I’m not sure what would be the best approach here to restore the old data.

Maybe you can first get the old data and keep it in memory so if a rollback is needed you can replace it back with the old data. That would probably work, but maybe there is a better way.

Where Next?

Popular in Questions Top

hariharasudhan94
I would like to know what is the best IDE for elixir development?
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
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
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
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
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

Other popular topics Top

grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 54092 488
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
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
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New

We're in Beta

About us Mission Statement