jerry
Hello All,
I have an issue I need your assistance on.
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.
Thanks.
Jerry
Trending in Questions
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
Hello,
I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
Hi everyone,
I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding.
I sta...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
I’m new to elixir and just tried to install the elixirLS extension for VScode(ium) and it is throwing some errors that I would like help ...
New
Other Trending Topics
Edit: 2026 May 15 - This post is archived.
Mob is alive!!
Main docs: mob v0.7.11 — Documentation
A bit of explanation for the slightly c...
New
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
Hi everyone!
The first release candidate for the Expert language server project is now available!
We’ve published a press release detai...
New
A little off-topic, but I feel like people here have a good head on their shoulders.
I used to be quite good at making software. Was luc...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ai
- #ecto-query
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #metaprogramming
- #hex











Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
benwilson512
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.
Option 1: Avoid read, modify, write patterns.
Instead of doing:
Do:
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:
This locks access to the account row for the duration of the transaction, allowing your process to safely reason about it.
If conflict is rare, you can also try doing Ecto.Changeset — Ecto v3.14.0 instead.
jerry
Many thanks @benwilson512.
I think the option two will work for me.
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.
Regards,
Jerry
hauleth
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.
jerry
Hello @hauleth,
Considering the nature of the query statement in Option 2, does Option 1 give the same level of integrity to the data being modified?
Thanks.
Jerry
hauleth
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
UPDATEqueries than locking mechanisms as second one can impose some additional restrictions as DB do not know what you want to do with that lock.jerry
Thanks so much for the clarification, @hauleth.
I really appreciate it.
What does the below mean, please? especially, the inc
benwilson512
You can find more information here: Ecto.Query — Ecto v3.14.0
jerry
Thanks very much @benwilson512.
Information seen.
Kindly let me run the tests and then I’ll feedback on the results.
I’m grateful to you all for the assistance.
Regards,
Jerry
jerry
Hello,
I have another question, please.
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?
Thanks.
benwilson512
If all you’re doing is grabbing the account id to then run this query, yes.
Why does this matter?