Interview: inventory stayed locked after the coordinator restarted
Both cohorts voted YES and the decision lived in memory. Two-phase commit, why a YES cannot be undone, three-phase commit under a partition, Calvin and…
More on Ticket interviews
- Database Interviews in the AI Era: The Ticket Format
- Interview: the same job runs twice
- Deadlock Interview Question: Find and Fix It in SQL
- Slow Query Interview: One Keyword Made It 40× Slower
- Interview: rate limit is 2/min, 30 requests get through
- Interview: “just add an index” slowed ingestion
- Interview: “I changed my name, refresh shows the old one”
- Interview: importing 10M rows takes six hours
- Interview: everything got slow and the disk keeps growing
- Lost Update Interview: Wallet Balance Wrong Under Load
- Interview: API p99 jumps 10× every night at 2 am
- Interview: profile lookups got 40× slower, writes are fine
- Interview: inserts stall, and only one page is hot
- Interview: the write succeeded, the next read shows an empty cart
- Interview: for four minutes, both regions took orders
- Interview: shoes show a 0% discount, then the report crashes
- Interview: March gained $50 after the deploy, and the months don’t add up
- Interview: the new branch’s dashboard takes 38 seconds
- Interview: the index is right there, make Postgres use it
- Interview: warehouse and checkout overwrite the same order
- Interview: the bestsellers report now takes nine minutes
- Interview: an analyst query locked checkout
- Interview: the member is active and the email went out twice
- Interview: one shard is at 96% CPU and the other is idle
- Interview: the menu is slow, and the proposal is event sourcing
- Interview: four dashboards disagree about whether the database is slow
- Interview: checkout got slow the hour the warehouse job started
- Interview: the query is fast in staging and slow in production
- Interview: login looks up an email and reads the whole customers table
- Interview: the proposal splits one database into three shards
- Interview: the order page joins four tables on every view
- Interview: every page view is a read-write query on the leader
- Interview: Sarah and John both debit version 1
- Interview: the CTO read a blog post and wants to move Postgres to MySQL
- Interview: one table takes 50,000 writes a second, another feeds dashboards. Which engine for each?
- Interview: EU users wait on the ocean for every write, can both regions take writes?
- Interview: users search cars by color, but the table is sharded by car id
- Interview: we added a fourth node and most of the data moved
- Interview: two people book the same room at noon, both get a confirmation
- Interview: everything is on the strongest consistency and far requests crawl
- Interview: the order is saved, the message to search and email never left
- Interview: rename a column on a 600 GB table while 30 servers keep serving traffic
- Interview: orders go to 16 shards in 2 regions, what should the order id be?
- Interview: one celebrity posts, and every timeline in the app slows down
- Interview: 5,000 customers share one database, and one of them slows down everyone
Keep learning
Overview
PREPARE orders PREPARE inventory The coordinator asks both databases to promise they can commit. YES, lock the order rows YES, lock the stock rows A YES is durable on that database. The locks stay until a decision arrives. process killed The decision was only in the coordinator’s memory. No COMMIT or ABORT was sent. rows still locked stock still locked Each side voted YES, so neither side is allowed to guess. New checkouts wait on those locks. Same first round. YES, locks held Both votes recorded. commit record fsynced The decision hits the coordinator’s log before anyone is told. A crash after this line still knows the answer. COMMIT (replayed on restart) Recovery sends the same decision again. Applying it twice is a no-op. Coordinator dies after both sides have voted YES Decision log decision in memory decision in a log Orders DB Inventory DB Play step 1 Next step Show all = beats.length ? ( The cohorts still block until recovery runs. They do not block forever, because the replacement coordinator reads the log and finishes the decision. COMMIT is safe to send twice. This is the ticket. Two-phase commit is a blocking protocol: a cohort that voted YES cannot commit or abort on its own, and a coordinator with no durable decision cannot tell it what to do.
Inventory has been locked since the coordinator restarted
Checkout spans two databases in one transaction. The coordinator died mid-way, and stock is still locked 40 minutes later. decision log Calvin / Spanner 200 SKUs stay locked after the coordinator restarted.
Warehouse says 200 SKUs are stuck reserved
Lock-wait timeouts began at 10:12, when the coordinator restarted for a deploy. The orders are neither “placed” nor “failed”. Stock for those SKUs cannot be sold. locked SKUs lock age 40 min order state Restated: both databases wait for a decision the restarted coordinator forgot.
Where is the decision stored?
Both prepares succeeded. The commit calls never ran. Stores the decision where a restart can read it. That is the bug. Spells out recovery for every case, including “do not guess”. Bans the timeout-abort that splits the outcome. Commit record found Resend COMMIT to every cohort. Abort, or a cohort never voted Send ABORT. Both voted YES, no record Do not guess. Page a human. What recovery does on boot, case by case.
No AI from here
Walk through two-phase commit when nothing crashes. PREPARE: each cohort (participant DB) votes NO, or logs, locks and votes YES. Any NO or silence: log ABORT and send it. All YES: log COMMIT, send it. Cohorts apply and release locks. prepare is the vote any NO aborts to all YES / NO Log decision COMMIT / ABORT The coordinator dies after writing the commit record, before sending it. What do cohorts do? They wait, but only until recovery reads the log. The new coordinator sees “commit” and resends COMMIT. A cohort that already applied it ignores the repeat (idempotent). This ticket is worse: the decision was never written at all. blocked until told replay is idempotent Decision never logged New coordinator knows nothing. Locks stay. Logged, not yet sent Recovery reads it and resends. Locks drop. Does three-phase commit remove the block? Only if the network never splits. Under a partition it can split the outcome. It adds a PRE-COMMIT round. Seen it? Commit on timeout. Not seen? Abort. Across a partition, one side commits and the other aborts. In practice: keep 2PC and replicate the coordinator log. timeouts split on partition replicate the log Cohort A Cohort B got PRE-COMMIT — partition, no PRE-COMMIT — timeout → COMMIT timeout → ABORT split outcome 3PC under a network partition. How do Calvin and Spanner avoid sitting in this state? Calvin: agree on order first, then every replica executes the same way. Needs read/write sets up front. Spanner: 2PC, but the decision lives in a replicated (Paxos) group. Calvin: order, then execute Spanner: replicated coordinator no single copy of the decision Order first. No commit vote needed. 2PC with a replicated coordinator. Locks in cells. Abort and retry. Product asks for a saga instead, tonight. What do you give up? Atomicity. Customers can see the in-between state. A saga runs separate local commits, with an “undo” step if one fails. Undo steps must be safe to run twice. Tonight’s fix is a durable 2PC log. A saga is a redesign. compensation replaces rollback the gap is visible saga ≠ 2PC All or nothing. Can block on a lost decision.
Two hundred SKUs are locked right now
What do you do in the next ten minutes? Recover any prepared transaction that has a commit record. Page a human for the rest. Finish the ones with a known outcome. Do not invent an outcome for the rest. Time to unlock Chance of a split outcome ABORT every prepared transaction so the sale can continue Locks drop now. But if COMMIT was already decided, one side commits and one aborts. Leave the locks until the transactions time out on their own Prepared transactions never time out. The SKUs stay stuck, and the next deploy repeats it. Ship a saga today and stop using 2PC Right talk, wrong hour. It unlocks nothing, and a rushed saga brings its own bugs.
Have one ready
Tell me about a distributed transaction that got stuck. What stayed locked, how long. Who waited on which decision. How the decision was recovered. Kill-test the coordinator mid-protocol.
Did you show these?
Crash between YES votes and decision Why a YES voter cannot abort alone Durable decision log before COMMIT Why 3PC fails under partition Calvin / Spanner keep the decision safe Refused to mass-abort prepared txns
More courses
- Jobs, linked to lessons — Browse jobs where every requirement links to the chapter that teaches it. See how ready you are, how long the gap takes to close, and which listings look stale.
- Match a job post to courses — Paste a job post. Each skill is matched to a track here and put in learning order, foundations first.
- React patterns from open source — Patterns extracted from Material UI, Radix, React Aria, React Hook Form, Redux Toolkit, TanStack, and Next.js — not slides about hooks.
- System Design — Core concepts first (SQL vs NoSQL, CAP, indexing, replication, sharding), then designs grouped by what carries them: the database, Redis, Kafka, or many services.
- NestJS: build a real API — One ticket-booking API, built in order: a plain Node server first, then NestJS modules, validation, a real database, and the features real clients lean on.
- Microservices — Microservice patterns one diagram at a time: how to split a monolith, how services talk, and how data stays consistent without distributed transactions.
- Kafka — A beginner-to-senior path through event logs, reliable delivery, production operations, and disaster recovery.
- AI engineering for software engineers — Eight free reads, in order, for software engineers moving into AI: when to build an agent, how to build one, how to test it, and how the model works underneath.
- Frontend system design — How a UI is sliced when it is a product, not a page: ownership of state, cache, routes, and what happens when the network lies.
- TypeScript in depth — The type system as a design tool. Inference, narrowing, generics, and the errors that mean the model is wrong — not a catalog of utility types.
- CSS in depth — See the box, then the page. Visual chapters from the box model through Flexbox surprises, Grid, selectors, and units, then backgrounds, shapes, and visual effects, each with a playground.
- Accessibility — Foundations first (WCAG in plain words, semantic HTML, lists, alt text), then hands-on HTML labs, a full pass of WAI-ARIA 1.3, and a final fix-the-page challenge.
- Docker — What an image is, why layers cache, and how containers find each other on a network.
- Redis — Fifteen Redis patterns, one picture each: cache, session, lock, counter, rate limiter, queue, and when Redis is the wrong tool.
- Kubernetes — Kubernetes from zero to operators: why it exists, Pods, Deployments and Services, then configuration, networking, storage, scheduling, scaling and security.
- AWS — AWS networking in plain English. A VPC is a gated housing society: streets are subnets, the Internet Gateway is the main gate, and a route table is the signboard.
- Networking: the NetRoute game — NetRoute is a packet logistics game. You wire up computers, switches, routers, firewalls and gateways, set each device, and watch glowing packets travel. If a packet gets dropped, the game shows you which header field was wrong.