How do you handle concurrent updates to the same row? Optimistic versus pessimistic locking.
What they are really testing: Whether you can prevent lost updates and choose the right strategy based on contention, rather than ignoring concurrency entirely (the classic read-modify-write race).
A real interview question
How do you handle concurrent updates to the same row? Optimistic versus pessimistic locking.
What most people say
drag me
“I would lock the row so only one update happens at a time.”
Pessimistic locking is one valid option, but always locking hurts concurrency and risks deadlocks. The complete answer names the lost-update problem and weighs optimistic vs pessimistic by contention level.
The follow-ups they ask next
How does optimistic locking actually detect a conflict?
A version/timestamp column: you read it, then UPDATE ... WHERE version = the value you read, and bump it. If another writer already changed it, your update matches zero rows, signaling a conflict to retry.
When is pessimistic locking the better choice?
High-contention rows where conflicts are frequent: optimistic would constantly fail and retry (thrash), so taking the lock up front (SELECT FOR UPDATE) is more efficient, accepting reduced concurrency and watching for deadlocks.
What the interviewer is listening for
- Names the lost-update problem
- Explains both strategies + version check
- Chooses by contention level
What sinks the answer
- Only "lock the row"
- Ignores concurrency entirely
- No version-based optimistic option
If you genuinely do not know
Say this instead of freezing. Reasoning out loud from what you do know beats silence every single time, and a good interviewer is listening for exactly that.
“Prevent the [lost update (read-modify-write race)]. [Pessimistic: lock the row (SELECT FOR UPDATE), others block, safe under high contention but holds locks/can deadlock]. [Optimistic: no lock, read a version and UPDATE ... WHERE version = X, retry on mismatch, great under low contention]. Choose [by contention level]; never [ignore it].”
Keep going with databases
Foundation
What is a database index, how does it speed up queries, and what does it cost?
Foundation
What are the ACID properties of a database transaction?
Foundation
What is normalization, and what are primary and foreign keys?
Junior
A query is slow in production. How do you diagnose and fix it?
Junior
Why is database connection pooling important, especially at scale?
Junior
How do read replicas help scale a database, and what is replication lag?
Knowing the answer is not the same as recalling it under pressure
Sign in to send the questions you fumble to spaced recall, so they come back right before you would forget them, and learn the concepts behind them with hands-on labs.
Start free