How to store data in some kind of memory (for eg: ETS, GenServer..) using elixir and getting those data using any other method if connection fails with Elixir?

Now i want to insert some data or ID in a memory (*not in database) during successful transaction and if the transaction fails i want to retrieve the data stored in a memory and perform some Cron Job to rollback already successfully inserted data. So what is the best method i can go with??

Can you please describe more indepth what you are trying?

Cron has no access to anything inside your memory.
Without a database, there will be no transaction that could fail, at least not in what I’d understand as a transaction right now.
If a transaction fails, it should be rolled back by whatever you use to store your data, so if it fails you don’t need your construct as I do understand it so far anymore

For example now i am using mongoDb. so, When i add user to group he should be added to user document, group document and group_membership document as well. When i am inserting the data suppose for example user get inserted to user document and group document but fails to insert in group_membership document due to some system connection failure or database issue. so, at this point of time i need to get the successfully inserted document and remove them from the database. so to make this i need to store successfully inserted Id in some memory to use later if insertion get failure.

In your case I’d try to find a database schema that does not need to change 3 records/documents/tables just for a single change.

Also, if possible by any means, I’d opt for a database which does provide transactions on its own.

You will never be happy with any workaround, since until you get to the point where you are able to “repair” the databse, other queries or insertions might have happened that are based on the earlier saved but faulty data. by simply deleting only the initially broken data, you will have leftovers which may happen to be even worse!

2 Likes

This does seem like a smell of some kind. But if I had to do this I would use some kind of job queue system with success acknowledgements and idempotent expectations. The last job should update or set the user to active.

That also means you will have probably have to generate an ID ahead of time.

Seems like overkill, but without Transactions @NobbZ is right. You will never be happy with any workaround solutions.

Personally, I would have to be out of options before I did this.

On a higher level this may be relevant Gregor Hohpe: Your coffee shop doesn’t use two phase commit.