Scrivio Interview library
DebuggingIntermediate7 min read · updated 13 November 2026

Account updates in a payments service are wrapped in a Redisson lock named after the account id. At 3,000 requests per second, p99 latency has reached 2 seconds, logs fill with failures to acquire the lock within the 500 ms wait time, and a few accounts receive most of the traffic.

Why has the lock become the bottleneck, and how would you redesign account updates so correctness does not depend on a distributed lock?

The lock adds two Redis round trips plus queueing to every update, and on a hot account all requests wait in a single line, so that account's throughput is capped by the lock hold time; the waiting requests hold threads and database connections, which spreads the slowdown to everyone. We remove the lock from the correctness path by letting the database enforce the rule with an atomic conditional update or optimistic locking, and for the hottest accounts we route commands through a partitioned log so a single writer applies them in order.

Why this question is asked

A distributed lock around each account update feels like the safe choice, and at low traffic it works. Then traffic grows, a few accounts become very busy, and the lock turns into the system's bottleneck. Interviewers use this scenario to see whether we can reason about throughput under contention and whether we know that databases already provide the guarantee the lock was added for. In this answer, we will recap what the Redisson lock does, calculate why it limits throughput, see how the waiting spreads to other accounts, and then redesign updates in three steps.

1A Quick Recap of What the Lock Does

Redisson is a Java client for Redis, and its RLock is a distributed lock stored as a Redis key. Each update in this service looks roughly like the code below: try to get the lock for up to 500 ms, update the balance, release the lock.

Java
RLock lock = redisson.getLock("account:" + accountId);
if (!lock.tryLock(500, 30_000, TimeUnit.MILLISECONDS)) { // wait up to 500 ms, lease 30 s
throw new LockTimeoutException(accountId); // the error filling the logs
}
try {
Account a = repo.findById(accountId).orElseThrow(); // SELECT
a.setBalance(a.getBalance().subtract(amount)); // check-then-act in Java
repo.save(a); // UPDATE, commit
} finally {
lock.unlock(); // another Redis round trip
}

Each update therefore pays at least two extra network round trips to Redis, one to acquire and one to release, on top of the database work. And while one request holds the lock for an account, every other request for that account waits.

2Why the Lock Caps Throughput

Let us put numbers on it. Suppose acquiring the lock, reading, updating and committing, and releasing takes about 6 ms in total. Only one request per account can be inside the lock at a time, so a single account can never process more than about 1000 / 6, roughly 165 updates per second, however many servers we add. Most accounts see a few requests per second and never notice. But the overview says a few accounts receive most of the 3,000 requests per second. If one account receives 500 per second, its queue grows by about 335 requests every second.

Waiting requests do not wait quietly. Each one holds a request thread for up to 500 ms, and if the transaction was opened before the lock was taken, it also holds a database connection. By Little's law, the number of requests in the system equals the arrival rate times the time each spends there: 500 requests per second waiting up to half a second means about 250 threads tied up for that one account. A Tomcat pool of 200 threads and a Hikari pool of 20 connections run out, and now requests for quiet accounts queue too. That is how p99 latency for everyone reaches 2 seconds.

Step throughHow one hot account slows every account
  1. Hot account gets 500 req/slock allows about 165/s
  2. Requests queue on one lockeach waits up to 500 ms
  3. Waiting requests hold threadsand often DB connections
  4. Thread and connection pools exhausted200 threads, 20 connections
  5. Quiet accounts wait for a threadp99 reaches 2 s for everyone

5 steps in order. Play them, or step through them one at a time.

3Let the Database Enforce the Rule

The lock exists to stop two updates from both reading the same balance and overdrawing it. The database can do that check atomically in one statement, with no external lock at all.

Java
-- Atomic conditional update: the check and the change happen together
UPDATE account SET balance = balance - 40.00
WHERE id = 9001 AND balance >= 40.00; -- UPDATE 1: debited; UPDATE 0: insufficient funds
// Or optimistic locking with JPA when the logic is more complex
@Entity class Account {
@Id Long id;
BigDecimal balance;
@Version long version; // UPDATE ... SET version = 13 WHERE id = ? AND version = 12
}
// a concurrent change makes the update hit 0 rows: OptimisticLockException, so retry

The conditional update holds the row lock only for the microseconds of the statement, inside the database, with no network hops in between. Concurrent debits on the same account simply run one after another, and the WHERE clause guarantees the balance never goes negative. When the business logic is too complex for one statement, optimistic locking with a version column works: the second writer gets zero rows, re-reads and retries. Retries are cheap when conflicts are rare and should be limited, for example three attempts with a short backoff.

4A Single Writer for the Hottest Accounts

For a handful of extremely busy accounts, even row-level contention can be too much, and retries under optimistic locking pile up. The standard answer is a single writer. We send each debit or credit command to a Kafka topic keyed by account id. Because records with the same key go to the same partition, and each partition is consumed by exactly one consumer in the group, one thread applies all commands for an account in order. No lock is needed, and the consumer can batch: apply 50 debits for the same account in one transaction instead of 50 transactions.

This changes the API: the caller gets an accepted response and learns the result asynchronously, or waits on a reply with a timeout. That is a real cost, so we use it only for the accounts that need it, such as a merchant settlement account, and keep synchronous conditional updates for the rest.

Options for concurrent account updates
ApproachCost per updateBest for
Redisson lock + read-modify-write2 Redis round trips + queueingrare, coarse coordination only
Conditional UPDATEone statementsimple debits and credits
Optimistic locking (@Version)one statement, retry on conflictcomplex logic, low contention
Single writer via Kafka partitionasync, batchedvery hot accounts

5Review the Other Locks

Once this lock is gone, it is worth asking where else the same pattern hides. Many distributed locks in a codebase duplicate what a unique constraint, a conditional update or a transaction already guarantees, and each one adds latency and a dependency on Redis. Distributed locks still have a place for coarse, rare coordination, such as making sure only one pod runs a migration, but they should not sit on the hot path of every request. Remember also that a Redis lock is not a correctness guarantee on its own: during a Redis failover a lock can be lost, so the database was always the real line of defence.

6Follow-up questions

Would a shorter wait time or a faster Redis fix it?

It would move the limit, not remove it. A faster Redis shortens the hold time slightly, and a shorter wait time turns more requests into errors sooner, but one lock per account still serialises all its requests with network hops inside the critical section. The capacity of a hot account stays capped well below what the database can do with a conditional update.

Why not use SELECT ... FOR UPDATE instead?

It is far better than the Redis lock because it is inside the database, but it still holds the row lock across a round trip to the application and back. For a simple debit, a single conditional UPDATE holds the lock for less time and needs no extra code. FOR UPDATE suits cases where we must read several values and decide before writing.

How do you handle the insufficient-funds case with a conditional update?

The statement returns zero rows updated, and the application turns that into a business error. If we need to tell apart "account does not exist" and "not enough money", we can use RETURNING or a follow-up read only in the failure case, which is rare.

7Conclusion

In this answer, we saw that a distributed lock per account caps each account at about one update per lock hold time, and that on hot accounts the queue of waiting requests exhausts threads and connections, slowing every account. We moved correctness into the database with an atomic conditional update or optimistic locking, used a single writer through a Kafka partition for the hottest accounts, and kept distributed locks for coarse, rare coordination only.

What to say in the interviewAbout 90 seconds aloud

Open with

The lock serialises every update to an account and puts network hops inside the critical section: acquire and release are two Redis round trips on top of the read, update and commit.

Then, if they want more

So a single account can only do about one update per hold time, maybe a hundred and fifty to two hundred per second, no matter how many servers we have.

With a few accounts taking most of the three thousand requests per second, their queues grow, requests wait the full five hundred milliseconds and fail, and while they wait they hold Tomcat threads and often database connections. Once the pools are exhausted, requests for quiet accounts wait too, which is why p99 hits two seconds everywhere.

I would remove the lock from the correctness path. For a debit, one conditional update does the check and the change atomically: set balance to balance minus the amount where id matches and balance is at least the amount. Zero rows means insufficient funds. For more complex logic, optimistic locking with a version column and a bounded retry.

For the very hottest accounts I would use a single writer: commands keyed by account id go to a Kafka topic, so one consumer applies them in order and can batch them. Then I would review the other distributed locks, because many just duplicate guarantees the database already gives, and keep locks for coarse, rare coordination.

About this answer

Last updated
13 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Why has the lock become the bottleneck, and how would you redesign account updates so...": https://scrivio-interview-library.netlify.app/architecture/distributed-systems/862ea3d69b/