Concurrency basics: threads, locks and race conditions
Concurrency is several tasks making progress at the same time. A race condition is a bug where the result depends on the exact timing between them.
Why it matters
Real systems serve many users at once. When two of them change the same data at the same moment, results can be silently wrong, like one seat sold twice.
The idea
LLD questions like seat booking or elevators often ask: what if two users act at once? If two threads read a counter, add one and write it back at the same moment, one update is lost.
Locks (mutexes) make a critical section run one thread at a time; atomic operations and thread-safe queues avoid manual locks. Watch out for deadlock: two threads each waiting for a lock the other one holds.
Two people filling in the same paper form at once: both read '3 seats left', both write '2 seats left', and one booking disappears. A lock is a rule that only one person may hold the form at a time.
People can see each other. Threads can't, and the bug may appear once in a million runs, so you have to reason about it, not just test for it.
How it works
- Thread A reads seats_left = 1. Thread B also reads seats_left = 1.
- A sells the seat and writes 0. B also sells the seat and writes 0. Two tickets have been sold for one seat.
- Fix: wrap 'read, check, write' in a lock. Now B waits until A has finished, then reads 0 and refuses.
- Across many servers, do it in the database with a conditional update: UPDATE seats SET sold = true WHERE id = 7 AND sold = false. Then check that exactly one row changed.
Example
Seat booking: 'check the seat is free, then mark it booked' must happen as one step. Within one program, that means inside a lock. Across many servers, it means inside a database transaction with a row lock.
Key points
- Critical section: code that touches shared data; protect it with a lock.
- A race condition: two threads read a value, change it and write it back with no lock, so one of the updates is lost.
- Deadlock needs four conditions; break 'circular wait' by always taking locks in the same order, or use timeouts.
- Across several servers, an in-program lock isn't enough: use database transactions or a distributed lock.
- Prefer thread-safe queues and data that never changes, where you can.
Common mistakes
- Locking only the write, instead of the whole read-check-write.
- Holding a lock while doing slow work, like a network call.
- Taking locks in different orders in different places, which causes deadlock.
Not true: Locking only the write is enough to make a read-check-write sequence safe.
In fact: Two threads can both read and check before either one writes, so both pass the check. Hold the lock around the whole read-check-write.
Not true: Two locks, two threads: the order in which each thread takes them does not matter.
In fact: If one thread holds lock A and waits for B while the other holds B and waits for A, both wait for ever: a deadlock. Always take locks in the same order.
In the interview
Anywhere two users can change the same thing, like booking the last seat, I'd make 'check and update' a single atomic step. Within one process that is a lock. Across servers it is a database transaction with a row lock, or optimistic versioning. I'd take locks in a fixed order to avoid deadlock.
Part of Unit 5: Low-level design: OOP and SOLID in the free System Design course.