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’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
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
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
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
Other Trending Topics
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
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
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
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
- #ecto-query
- #ai
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 17 to 8- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
jerry
Thanks very much @benwilson512.
I’ve been able to implement the solution as you guided me to do.
Thanks @hauleth for your inputs as well.
It required a total rewrite of that aspect of the application’s engine.
This has been a very good learning process for me.
I shall provide some more feedback after some time of monitoring the application under heavy traffic.
Jerry
jerry
That’s well noted.
Thanks very much @benwilson512
benwilson512
To be clear
update_allisn’t the thing that makes the difference here, it’s that you’re usingincto do the math in the database, andincrequires 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
Multior viaRepo.transactionwith an anonymous function.jerry
Is it permissible to use Ecto.Multi.update_all or Ecto.Multi to perform this transaction and achieve same atomic result?
jerry
Thanks very much @benwilson512. It’s clear to me now.
benwilson512
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.jerry
I am retrieving the account_id and its corresponding current balance in order to go update the record with the new balance.
account_id=record.account_id
current_balance=record.balance
new_balance=current_balance + 5
Account |> where(id: ^account_id) |> update(set: [balance: new_balance]) |> Repo.update_all()
benwilson512
If all you’re doing is grabbing the account id to then run this query, yes.
Why does this matter?
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.
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