Concurrency & IsolationdebuggingAdvanced
Two users booked the same seat
Symptoms
- Occasionally two customers are assigned the same seat, despite a "check if taken, then book" flow.
- Only happens under concurrent load.
-- both requests, interleaved:
T1: SELECT count(*) FROM bookings WHERE seat = '12A'; -- 0, so free
T2: SELECT count(*) FROM bookings WHERE seat = '12A'; -- 0, so free
T1: INSERT INTO bookings (seat, user_id) VALUES ('12A', 1);
T2: INSERT INTO bookings (seat, user_id) VALUES ('12A', 2); -- both succeedInvestigate
Inspect areas in any order (0/4 inspected). When you think you know the root cause, commit to it.
The check-then-insert logic
Isolation level
Constraints on the bookings table
Application locking