sezaru

sezaru

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.

Showing Posts 1 to 6

mindok

mindok

derek-zhou

derek-zhou

I don’t understand, in your first example if the transaction fails, will the cache update be rolled back? How?

sezaru

sezaru OP

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

sezaru

sezaru OP

If I understood the Multi code correctly, multi will create a transaction with the database (postgres in my case), then will execute the first command which is the insert, and then the second one which is the cache, if both are successful, the transaction will simply close without a window to fail.

But maybe I’m wrong and there is a little window that it can fail if the transaction itself fails as you suggested? For example if the database connection is lost in that window, something like that?

derek-zhou

derek-zhou

I do not know for sure, However I think there is a possibility. The Saga pattern cited by mindok sounds more robust. You need to provide an explicit rollback function for every single operation that you need to rollback in case of failure in other operations.

sezaru

sezaru OP

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.

— All posts loaded —

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
Onor.io
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
jaybe78
Hello, I’m developing a online persistent chat system (what’s app) like using elixir/dynamodb/aws for a mobile app(flutter). The diffic...
New
Trolleger
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New
widianto
I think I’ve found a small improvement I could contribute to <%= web_namespace %>.CoreComponents (installer/templates/phx_web/compo...
New

Other Trending Topics Top

garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New
aseigo
ICal is a library for interacting with iCalendar data. It parses iCalendars into typed Elixir structs via ICal.from_ics, and can prepare ...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews