17. Transactions and Locking
1
The picture to keep in your head2
17.1 What is a transaction?3
17.2 What does ACID mean in depth?4
17.3 What is a lost update?5
17.4 What can a transaction see that it shouldn't?6
17.5 What are isolation levels?7
17.6 What is pessimistic locking?8
17.7 What is optimistic locking?9
17.8 What is a deadlock?10
17.9 What are distributed locks, and why are they tricky?11
17.10 What is two-phase commit?12
17.11 What is a saga?13
17.12 How do seat holds with expiry work?14
Memory card and where the analogy breaks15
Practice: five levels16
Interview questions17
Solutions18
Sources19
Code lab17. Transactions and Locking
Practice: five levels
3 min read+10 XP
Assignments
Five levels. Do them in order. Answers are in "Solutions" at the end, so try first.
Level 1: Think (no code)
A. Name the problem.
- Two fans book the last ticket; the event shows −1 seats left.
- A report reads an order's total twice in one transaction and gets two different numbers.
- "Check no booking exists for seat 14, then insert" sells seat 14 twice.
- Two transactions each wait for a row the other has locked; both hang until one is killed.
- A cache-refresh lock expired during a long pause, and two workers wrote different values.
B. Pessimistic lock, optimistic lock, atomic update, or a unique constraint? One line each.
- Decrement
seats_leftfor a general-admission event. - Prevent two bookings of the same numbered seat.
- A user editing their profile in a form for several minutes.
- Moving ₹200 between two wallet rows in the same database.
Level 2: Calculate
- Every booking locks the single
eventsrow for 50 ms. What's the maximum bookings per second for that event? - 100 seats are held for 10 minutes; 30% of holds are abandoned. How many seats come back after the first wave? If holds were 30 minutes, roughly how long would those seats sit unsellable?
- A 2PC transaction has 3 participants. How many messages does the coordinator send in a successful run (prepare and commit)?
- With optimistic locking, 1,000 users try to book 100 seats at the same instant, each retry taking 20 ms. Roughly how many failed attempts happen in the first round?
Level 3: Build
In Python:
- Show the lost update with two interleaved read-modify-writes, and fix it with an atomic
UPDATE ... WHERE seats_left > 0insqlite3: 130 attempts for 100 seats must sell exactly 100. - A
VersionedRowwithcompare_and_set(expected_version, new_value), and an optimistic booking function with retries. find_deadlock(waits_for)that finds a cycle in a wait-for graph.- A
Seatsclass withhold_seat(seat, user, now)(10-minute expiry) andconfirm(seat, user, now). - A
run_saga(steps)that runs compensations in reverse when a step fails.
Level 4: Debug
The signing sold seat 14 to two people. The booking code:
python
- Find the bug. Walk through how two requests both succeed.
- Fix it so the database itself makes double booking impossible.
Level 5: Design
Design booking for the signing: 100 numbered seats at ₹499, opening at 10:00, with about 20,000 fans trying in the first minute, and separate seat, payment and ticket services.
Deliver:
- The seat data model and how a hold is made atomically.
- How payment and ticket issuing are coordinated across services, and what happens on each failure.
- How expired holds are released.
- How you stop 20,000 people from hammering the database at 10:00.
- Which isolation level and locking approach you use where.