Some rows are far busier than the rest: a large merchant's wallet, a global counter, the inventory of a flash-sale item. Every transaction that updates the row needs its exclusive lock, and InnoDB grants that lock to one transaction at a time. The row becomes a queue, and the queue sets the throughput of the whole feature.
Recognising the problem
- Latency of one kind of update rises sharply with load while CPU on the database stays low.
- Many sessions are in the state "updating" or waiting on
innodb row lockinSHOW PROCESSLISTor the Performance Schema, all on the same table and key. - Lock wait timeouts (error 1205) appear for one merchant or one product only.
The reasoning is simple. If one transaction holds the lock for 5 milliseconds, the row can handle at most about 200 updates per second, whatever the hardware. Adding more database connections makes it worse, because the extra sessions only join the queue.
First, shorten the critical section
The lock is held until commit, so anything inside the transaction after the update extends the queue. Update the hot row as the last statement before commit, never call external services inside the transaction, and commit immediately. This alone often doubles capacity.
Option 1: split the row into sub-accounts
Represent one logical wallet as N physical rows. Each incoming payment picks one at random, so contention drops by a factor of N. The balance is the sum of the parts.
-- merchant 500 is split into 8 buckets
UPDATE wallet_buckets SET balance = balance + 25000
WHERE wallet_id = 500 AND bucket = 3;
SELECT SUM(balance) FROM wallet_buckets WHERE wallet_id = 500;Reading the total touches N rows, which is cheap for display. The trade-off is that a debit, which must not overdraw, has to pick a bucket that holds enough money or combine several, so this approach suits wallets that mostly receive money. Crediting never needs a balance check.
Option 2: write the ledger first, update the balance in batches
Appending a row to the ledger does not contend with other appends, because each insert creates a new row with its own key. Let payments only insert ledger entries, and let a background job fold them into the balance every few hundred milliseconds with a single update per wallet.
UPDATE wallets w
SET balance = balance + (
SELECT COALESCE(SUM(amount), 0) FROM ledger_entries
WHERE wallet_id = w.id AND id > 41000 AND id <= 41500)
WHERE w.id = 500;Hundreds of payments become one update on the hot row. The cost is that the stored balance lags by a moment, so it is fine for display and for crediting, and unsuitable as the only check for "enough money to withdraw" unless you also include the unapplied ledger tail in that check.
Option 3: queue per wallet
Route all operations for a wallet to one worker, for example by hashing the wallet id to pick a partition or a queue. One worker processes them sequentially with no lock contention, and it can group operations into batches for a single transaction. Throughput improves because the queue removes lock waiting, and ordering for that wallet is preserved. The price is extra infrastructure and an asynchronous result for the caller.
Choosing between them.
- Mostly credits, strong read-your-write not needed: sub-accounts or ledger-first batching.
- Debits that must be checked precisely: a queue per wallet, or keep a single row and make its transaction as short as possible.
- Measure first. Many "hot rows" are actually slow transactions that hold locks too long.
Takeaway
A hot row serialises every writer, and more connections cannot fix that. Shorten the transaction, then spread the writes across buckets, move contention-free ledger inserts ahead of batched balance updates, or serialise through a queue. Accept slightly delayed balances only where the business can.
Comments