Concurrency in LLD · mid
Booking seats and stock without double booking
Checking availability and taking the item must be one atomic step. Lock or update only the seats or products involved, in a fixed order, and give holds an expiry. In-memory locks do not work across many servers.
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceThe notes
What you must know
- Cause of double booking: check-then-act. isAvailable and markBooked are separate steps with a payment step in between. synchronized works only inside one JVM and there are many app servers.
- Model a ShowSeat with status AVAILABLE, HELD or BOOKED, heldBy, holdExpiresAt and a version. Holds expire (checked on read plus a sweeper, with an injected clock) so payment happens on held seats.
- In memory: lock the requested seats in a fixed order (sorted ids), check all, hold all, unlock. Or use compare-and-set on seat status. Never one global lock for every show in the country.
- With a database: one conditional update (SET status = HELD WHERE id IN (...) AND status = AVAILABLE) and check the updated row count, or optimistic locking with a version, or a unique constraint on (show, seat).
- Multiple seats are all-or-nothing in one transaction. On a partial failure, release what was taken. Lock rows in a fixed order to avoid deadlock.
- Flash sale with one lock for the whole inventory: threads queue and CPU stays low. First move slow input and output (audit log) out of the locked section.
- Narrow the lock: per-product state in a ConcurrentHashMap of AtomicInteger, with a compare-and-set loop for one counter. For several fields use a ReentrantLock per product or lock striping. ReadWriteLock does not help a write-heavy path.
- With many servers move the truth to the database (UPDATE ... SET stock = stock - ? WHERE sku = ? AND stock >= ?) or an atomic Redis script. Prove it with a concurrency test and fix affected customers too.
The red pen
Common traps: mistake → fix
Mistake: Add synchronized and it is fixed
Fix: it only works inside one process.
Mistake: The database will handle it
Fix: name the mechanism: conditional update, version or unique constraint.
Mistake: A distributed lock with no expiry is safe
Fix: a crashed holder blocks everyone.
Mistake: Add more threads to fix a lock bottleneck
Fix: more threads only queue on the same lock.
Easy to hard
Questions interviewers ask, easy to hard
- Two users got the same seat. What do you do now and what is the cause?
- How do you design seat holds with expiry?
- How do you book several seats safely?
- One lock for the whole inventory is slow in a flash sale. How do you fix it?
Practise
Practise with these questions
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceYour first interview is free
Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.