Skip to main content

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 readDirty writeLost updateRead skewWrite skewPhantom
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:

  1. Describe the invariant in one sentence.
  2. Sketch the two concurrent transactions that violate it under SI.
  3. 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.

  1. A read-only analytics dashboard scanning transactions from the last hour.
  2. A "charge card once per order" workflow where the order row has a state machine.
  3. A leaderboard where every write is UPDATE score = score + 1 WHERE user_id = ?.
  4. A "book a room if no booking overlaps" workflow.
  5. A scheduled job that moves rows from pending to done in batches, with concurrent inserts into pending.

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.

  1. Charge $50 to a credit card.
  2. Send a receipt email.
  3. Insert a row in orders.
  4. Deduct 10 units from inventory.
  5. Publish an event to a public webhook.
  6. Transfer $100 from account A to account B (two internal rows).
  7. Grant a coupon code to a user.
  8. Reserve a phone number from a pool.
  9. Trigger a physical warehouse pick ticket.
  10. 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:

  1. Single-node PostgreSQL.
  2. PostgreSQL with async streaming replica, reads from replica.
  3. PostgreSQL with sync streaming replica, reads from primary.
  4. MongoDB replica set, writeConcern=majority, readConcern=majority.
  5. DynamoDB with default eventual reads.
  6. DynamoDB with ConsistentRead=true.
  7. Cassandra with CL=QUORUM on reads and writes, RF=3.
  8. Spanner.
  9. Redis primary + async replica, reads from replica.
  10. 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.