Concurrency: the double-booking bug
Almost every low-level design problem worth asking has a concurrency question hiding inside it, waiting for the follow-up: "...and what if two users do that at the same time?" Two people book the last seat. Two people withdraw the last ₹1000. Two cars take the last parking spot. If you can diagnose this bug and rank its fixes, you clear a bar most candidates miss. Let's build that skill properly.
Why it happens: the gap between look and act
if (seat.isAvailable()) { // thread A reads: true
// ...meanwhile thread B reads: true
seat.assign(user); // both assign. two people, one seat.
}
The intuition: checking and acting feel like one motion, but they are two separate operations with a gap between them, and in that gap, anything can happen. It's two people seeing the same last biscuit across the room and both walking over — each one's information was true when they looked. This shape is called a check-then-act race, and it is far and away the most common concurrency bug in existence. Train your eye: any code that reads a value, decides, then writes based on that decision is a suspect.
Four fixes, ranked — start at the top
Fix 1: Make it atomic in the database (almost always the right answer). Collapse the check and the act into one indivisible statement:
UPDATE seats SET user_id = :me WHERE id = :seat AND user_id IS NULL; -- then check rows_affected: 1 = you won, 0 = someone beat you
The WHERE user_id IS NULL is the availability check, and the database executes check-and-write as one atomic operation — there is no gap for a rival to slip into. No locks held across network calls, works no matter how many servers you run, and the loser gets a clean, honest "0 rows" to handle. When an interviewer asks the double-booking question, this is the answer they're hoping for.
Fix 2: Optimistic locking — assume peace, detect war. Add a version column. Read the row (version 7), compute, then write with WHERE id=:id AND version=7, setting version to 8. If someone else wrote in between, the version moved, your UPDATE matches zero rows, and you know to reload and retry. "Optimistic" because you assume conflict is rare and pay only when it actually happens. Ideal when contention is low — which is most of the time.
Fix 3: Pessimistic locking — assume war, take the lock. SELECT ... FOR UPDATE locks the row up front; rivals queue until your transaction commits. Correct, and sometimes right when conflicts are frequent and retrying is expensive — but you're now holding locks for your transaction's duration, throughput drops, and you've invited deadlock (below) to the party.
Fix 4: In-process lock — and the trap it hides. synchronized, a mutex. It orders threads inside one process. The instant you run two server instances behind a load balancer — which is to say, in any real deployment — each instance has its own lock, the two locks know nothing of each other, and the race is fully back. Interviewers adore this follow-up precisely because so many candidates stop at synchronized. "That protects one JVM; across instances I need the database to arbitrate" is the sentence that separates the levels.
Deadlock: the mutual hostage situation
Transfer money A→B while another thread transfers B→A. Thread 1 locks account A, thread 2 locks account B, and now each waits forever for the lock the other holds. The classic prevention is beautifully simple: always acquire locks in a globally consistent order — for transfers, lock the lower account ID first, always, regardless of transfer direction. Both threads then compete for A first; one wins cleanly, finishes, releases; no cycle can form. Add lock timeouts as a seatbelt, so a mistake degrades into a retryable error instead of an eternal hang.
Reaching for synchronized in a distributed system — see Fix 4. The correct escalation ladder is: atomic conditional DB operation → optimistic lock → pessimistic lock → and only for coordination the database can't arbitrate, a distributed lock (Redis/ZooKeeper — which brings fencing-token subtleties you'll meet in Tier 6). Start at the top of the ladder. Every rung down costs throughput and adds failure modes.
Going deeper
- Java Concurrency in Practice Brian Goetz et al., 2006
Still the clearest treatment of why check-then-act is broken and what to do instead. The language is Java; the reasoning is not.