I’m trying to manage an shared account balance that can be acessed by multiple users, for this I need sequencial transactions to assure actual state. It’s my first prod ready project so probably won’t have many acess, probably would be possible to store the balance on a json based file. Anyway, what approaches could I use to assure that? Ecto has fragments and some lock features, but by the docs sounds like cryptics and with a lot of contraints instead of just plug or run a manual query. I was thinking on a genserver with a variable controlling whether the process is acessing the db or not, similar to a singleton. Would that be viable? what other ways could I do it?
There’s really two parts to this: your data design, and then the data access design.
From a data design standpoint strongly consider a ledger like system where you record credits and debits. This lets you rebuild the history and totals along the way.
For controlling the actual changes though I would 100% use database level locking and not try something at the application layer like a genServer. Postgres locking truly isn’t too hard and it’s way easier than dealing with the complications you’ll have when you want to run more than 1 node.
3 Likes