Search
⌘K
Get Premium
Concurrency
Correctness
Learn how to ensure multiple operations succeed or fail together.
🔒 Correctness is about preventing data corruption when multiple threads access shared state. Two threads both book the same seat. A counter that should be 1000 reads 847. A bank balance missing deposits. The danger isn't deadlock or performance, it's silently producing wrong results.
The Problem
You're building a ticket booking system for a concert venue. Users can browse available seats and book them. Simple enough. But what happens when two users try to book the same seat at the same time?
Alice wants seat 7A. Bob also wants seat 7A. Here's what should happen:
Booking Correct
Alice gets the seat, Bob gets an error. But here's what can happen in a concurrent environment:
Booking Broken
Both users checked availability before either completed their booking. Both saw the seat as available. Both proceeded to book it. Bob's write overwrote Alice's, and now Alice thinks she has a ticket but will show up to find Bob in her seat.
This is a correctness problem. Can multiple threads corrupt shared state? Yes—the check ("is the seat available?") and the action ("book it") happened as two separate steps. Another thread snuck in between them and invalidated the assumption. Alice checked, the answer was yes, but by the time she acted on that answer it was no longer true.
The same pattern appears throughout low-level design interviews. Rate limiters checking if under the limit before allowing a request. Connection pools checking if a connection is free before handing it out. Caches checking if there's room before adding an item. Whenever the validity of a check can change before you act on it, you have a correctness problem.
This article walks through four solutions to correctness problems, from simplest to most complex:
- Coarse-grained locking protects all related state with one lock
- Fine-grained locking allows concurrent access to independent resources while protecting related ones
- Atomic variables work for single variables but fail for multi-field invariants
- Thread confinement eliminates concurrency entirely for related data
Then we cover the two patterns where correctness bugs appear most often in interviews and how to apply the solutions to them.
- Check-then-act like booking a seat if available
- Read-modify-write like incrementing a counter
The Solutions
Every correctness problem can be solved with one of four approaches. We'll walk through them in order of frequency with which they appear in interviews.
Coarse-Grained Locking
Challenges
Read-Write Locks
Fine-Grained Locking
Challenges
Atomic Variables
Challenges
Thread Confinement (Shared Nothing)
Common Bugs
Check-Then-Act
Examples
Read-Modify-Write
Examples
Conclusion
Purchase Premium to Keep Reading
Unlock this article and so much more with Hello Interview Premium
Purchase PremiumCurrently up to 25% off
Hello Interview Premium
Reading Progress
On This Page

Schedule a mock interview
Meet with a FAANG senior+ engineer or manager and learn exactly what it takes to get the job.