Transactions Code Katas
15-20 minutes each. Pure technique drills. Repeat until fluent. No need to get it right the first time; get it smooth by the third.
Kata 1: The 2PC State Machine (15 min)
Draw from memory the complete 2PC state machine for both coordinator and participant. Include:
- States:
init,preparing,prepared,committing,committed,aborting,aborted. - Transitions labeled with the message sent or received.
- Durable log write points (what MUST be on disk before the next message).
- For each state, annotate what happens on crash: do we redo the current phase, retry the last message, or wait for input?
Compare against DDIA Figure 9-9 after you finish. Score yourself on:
- Did you put the fsync in the right place?
- Did you model the in-doubt state explicitly?
- Did you show the asymmetry between coordinator and participant recovery?
Kata 2: The Anomaly Matrix (15 min)
Draw from memory a table:
| Dirty read | Dirty write | Lost update | Read skew | Write skew | Phantom | |
|---|---|---|---|---|---|---|
| Read Uncommitted | ? | ? | ? | ? | ? | ? |
| Read Committed | ? | ? | ? | ? | ? | ? |
| Repeatable Read (ANSI) | ? | ? | ? | ? | ? | ? |
| Snapshot Isolation | ? | ? | ? | ? | ? | ? |
| Serializable (2PL) | ? | ? | ? | ? | ? | ? |
| Serializable (SSI) | ? | ? | ? | ? | ? | ? |
Fill each cell with allowed, prevented, or the specific caveat.
Then, for each engine below, mark the cell corresponding to its default isolation level:
- PostgreSQL default (
READ COMMITTED). - PostgreSQL
REPEATABLE READ. - PostgreSQL
SERIALIZABLE. - MySQL InnoDB default (
REPEATABLE READ). - Oracle "serializable" (actually SI).
- SQL Server
SERIALIZABLE. - SQL Server
SNAPSHOT.
Kata 3: The Write-Skew Reflex (15 min)
Write five distinct application invariants that are vulnerable to write skew. For each:
- Describe the invariant in one sentence.
- Sketch the two concurrent transactions that violate it under SI.
- Give the fix.
Do not reuse doctors on-call, the classic example. Draw from inventory, authorization, uniqueness, capacity planning, quotas, rate limits, A/B tests, cross-row constraints.
Time yourself. Five examples in 15 minutes forces you to recognize the pattern, not invent it fresh each time.
Kata 4: Linearizability by Hand (15 min)
Given this history, decide linearizability and justify by producing a linearization or a cycle.
History 1:
C1: write(x, 1) [t=1-2]
C2: write(x, 2) [t=3-4]
C3: read(x) -> 2 [t=5-6]
C4: read(x) -> 1 [t=7-8]
History 2:
C1: write(x, 1) [t=1-4]
C2: read(x) -> 0 [t=2-3]
C3: read(x) -> 1 [t=5-6]
History 3:
C1: write(x, 1) [t=1-2]
C2: read(x) -> 1 [t=3-4]
C3: write(x, 0) [t=5-6]
C4: read(x) -> 1 [t=7-8]
For each, either produce a valid linearization (total order respecting real-time order and register semantics) or name the violating pair.
Kata 5: SI vs SSI Call (20 min)
Given these five workloads, call for each: run at SI (PostgreSQL REPEATABLE READ), run at SSI (SERIALIZABLE), or add an explicit FOR UPDATE / constraint on a specific path. Justify in one sentence.
- A read-only analytics dashboard scanning transactions from the last hour.
- A "charge card once per order" workflow where the order row has a state machine.
- A leaderboard where every write is
UPDATE score = score + 1 WHERE user_id = ?. - A "book a room if no booking overlaps" workflow.
- A scheduled job that moves rows from
pendingtodonein batches, with concurrent inserts intopending.
Kata 6: The Compensation Pattern (15 min)
For each forward action, write its compensation in one sentence. Flag any that cannot be compensated without a human workaround.
- Charge
$50to a credit card. - Send a receipt email.
- Insert a row in
orders. - Deduct
10 unitsfrominventory. - Publish an event to a public webhook.
- Transfer $100 from account A to account B (two internal rows).
- Grant a coupon code to a user.
- Reserve a phone number from a pool.
- Trigger a physical warehouse pick ticket.
- Increment a "lifetime purchases" counter.
Kata 7: PACELC Speed Drill (10 min)
For each system, call the PACELC label (PA/EL, PA/EC, PC/EL, PC/EC) in one line:
- Single-node PostgreSQL.
- PostgreSQL with async streaming replica, reads from replica.
- PostgreSQL with sync streaming replica, reads from primary.
- MongoDB replica set, writeConcern=majority, readConcern=majority.
- DynamoDB with default eventual reads.
- DynamoDB with ConsistentRead=true.
- Cassandra with CL=QUORUM on reads and writes, RF=3.
- Spanner.
- Redis primary + async replica, reads from replica.
- etcd / ZooKeeper.
Score: nine correct in ten minutes is fluent.
Evidence Check
Katas complete when:
- Each kata done at least three times.
- Third attempt was under time.
- You can do Kata 4 (linearizability) on an unfamiliar history within 5 minutes.
- You can do Kata 7 without pausing on any item.