I have a table that contains unique account information on my customers.
Whenever there are simultaneous/concurrent updates on an account record, the resulting balance is wrong.
What is the best way to handle this in order to ensure that even when there are multiple update requests on an account in the table, the updates are performed one by one in a such a sequential manner to ensure the balances are correct in the account table?
I would be sincerely grateful for your assistance.
Here are some options you can use to serialize access to rows. Regardless of which of these you use, I highly recommend also having a table that logs balance changes on each account, like a ledger. This allows you to audit the total in the future, and debug any issues.
In this approach, you change the balance in a single SQL query instead of two, avoiding race conditions. Postgres will ensure that the single update query runs atomicly.
Option 2: SELECT FOR UPDATE
If you can’t avoid read, modify, write, then take a lock on the row you wish to update:
It’s a high traffic application, using Elixir and ecto_sql with postgres.
There is usually multiples of requests to the same account.
For each request, I need to reduce the account balance by the requested value before taking an action, hence the need to do read, modify, and write on the record.
Kindly let me try it out and feedback on the outcome.
If you need just to update, then I think the 1st option will be better as you do not need to do 12 trips (at least as it is TCP) and you can just do everything in 6.
Yes, each query is transaction within itself, so within single query it is impossible to have race condition (assuming that there are no bugs in DB implementation). I would say that DB is probably better optimised for single UPDATE queries than locking mechanisms as second one can impose some additional restrictions as DB do not know what you want to do with that lock.
Please pardon me if my question sounds too trivial.
Can I use a normal select query to retrieve the value of account_id in order to execute the query statement below?
How do I ensure that another process doesn’t retrieve that same value of account_id before getting to this stage of running the update statement?
This is still a read, modify, write. This will not work safely. You must use inc: so that the change happens in a single atomic operation, OR you have to explicilty lock.
To be clear update_all isn’t the thing that makes the difference here, it’s that you’re using inc to do the math in the database, and inc requires update_all.
For example this uses update_all but is still wrong
The math is happening in Elixir, so it’s subject to race conditions. If you want to do the math in Elixir you need to use a lock like I showed you in option #2. That lock requires a transaction, it doesn’t matter whether that transaction is handled via Multi or via Repo.transaction with an anonymous function.