Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

eskiesirius's avatar

Increment/Decrement on High Traffic

I created a fintech app, right now i am using ->increment/decrement for the balance.. my current problem is that when a certain merchant has lots of traffic, it causes a problem it needs to wait before it updates because of the atomic lock.. I also have a ledger of the wallet transaction but using sum to get the real balance will be very expensive when it has a lot of records.. how do you handle this stuff?

1 like
3 replies
vincent15000's avatar

it causes a problem it needs to wait before it updates because of the atomic lock

I don't think that there is another way than waiting ...

imrandevbd's avatar

Stop running increment() or decrement() during the HTTP request. Dispatch a job (e.g., UpdateMerchantBalance). The API responds immediately, and your queue workers deal with the lock waiting in the background. The workers will still wait on each other for that specific row, but your web server won't hang and the user won't experience timeouts.

2 likes
martinbean's avatar

@eskiesirius You shouldn’t be using a single column to hold the balance. You should instead of some form of ledger table, where you write individual increments and decrements as their own rows, and then derive the balance from the sum of those increments and decrements. It’s not “expensive” if you have a proper index in place.

2 likes

Please or to participate in this conversation.