Two Writers
You'll open two connections to the same database, make them fight, lose an update on purpose, cause a deadlock, and then fix both properly.
About
Nothing hard happens in a database until a second connection shows up. Then both read a balance of 100, both subtract 30, and the balance ends at 70 instead of 40. That lost update is the oldest bug in databases and it still ships in new apps every week. This badge is two terminals side by side on the Postgres you set up in Local Server, reproducing the classic mistakes and fixing them with transactions, row locks and an optimistic version column. You'll also cause a deadlock and read what the database says about it. SQLite can do the first four steps with its single-writer lock, and each step says what changes; the deadlock needs Postgres. What counts is each race shown happening, then shown fixed.
At a glance
- Per requirement
- 35 XP
- Completion bonus
- 300 XP
- Requirements
- 6
- Takes
- Two weekends
- Skill level
- Advanced (years of practice)
- Cost
- Free
- Supplies
- Nothing special
- Where
- At home
- Other people
- On your own
Requirements
-
1
Run a two-step transfer inside a transaction and quit halfway, to show that nothing moved. Make an accounts table with two rows. BEGIN; then UPDATE accounts SET balance = balance - 30 WHERE id = 1; then close the terminal before the second UPDATE and the COMMIT. Reopen and look: both balances are unchanged. That's atomicity, and it's the reason transactions exist. Film it, or screenshot before and after. Photo or video
-
2
Reproduce a lost update with two terminals doing read-then-write. In both terminals: BEGIN; then SELECT balance FROM accounts WHERE id = 1; and both see 100. Then each runs UPDATE accounts SET balance = 70 WHERE id = 1; and COMMIT. The final balance is 70 and one withdrawal has vanished. In SQLite the second writer gets 'database is locked' the moment it tries to write, and no busy_timeout will make it wait, because waiting here would deadlock and SQLite knows it. That's a different lesson: the engine refuses the race outright, so you get an error instead of a wrong number. Film both terminals, or screenshot the sequence. Photo or video
-
3
Fix it with SELECT ... FOR UPDATE and show the second terminal waiting. The second SELECT ... FOR UPDATE blocks until the first terminal commits, then sees the new balance and subtracts from that. In SQLite, BEGIN IMMEDIATE does the same job by taking the write lock up front, and with PRAGMA busy_timeout = 5000 set in both terminals the second one waits instead of erroring. Film the second terminal hanging and then continuing when the first commits. Video
-
4
Fix it a second way with an optimistic version column and show the loser being told. Add a version INTEGER column. Each writer runs UPDATE accounts SET balance = 70, version = version + 1 WHERE id = 1 AND version = 3. The second one reports UPDATE 0 and has to re-read and retry. This is how most web apps do it, because nobody wants to hold a lock while a person thinks. Take a screenshot of the UPDATE 0. Photo
-
5
Cause a deadlock on purpose, read the error, and make it impossible. This one is Postgres only, because SQLite's single writer can't deadlock on rows. Terminal A updates row 1 then row 2; terminal B updates row 2 then row 1, each inside a transaction, interleaved one statement at a time. Postgres notices within a second and cancels one with 'deadlock detected'. Read the DETAIL line, which names both processes and the locks they held. Then order every update by lowest id first and show it can't happen any more. Take a screenshot of the error and of the fixed run. Photo
-
6
Write ten lines on which fix you'd use for a club's shared spreadsheet replacement, and why. Post it with a screenshot of the final accounts table balancing to the right number. Photo