Interview: Sarah and John both debit version 1
John commits version 2. Sarah’s update matches zero rows. That is the conflict. Retry by re-reading, and do not tell her the debit succeeded.
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: inventory stayed locked after the coordinator restarted
- 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: 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
read id=1 amt=40 version=1 Both snapshot the same version. No lock. UPDATE … SET amt=20, version=2 WHERE id=1 AND version=1 John’s predicate matches the row he read. COMMIT → amt 20, version 2 One debit of 20 landed. UPDATE … WHERE version=1 → 0 rows Sarah still expects version 1. It is gone, so nothing matches. Account 1 starts at 40, version 1. Each debit subtracts 20, but only if the version is still 1. Play step 1 Next step Show all = BEATS.length ? ( No row matched Sarah. Balance is 20, not 0. The zero row count tells her the debit failed.
Sarah and John both debit version 1. Only John’s write lands.
Two debits, one version number, one winner. This tests optimistic locking: spot the conflict and decide what the loser does. version column 0 rows matched vs FOR UPDATE Code ignored “0 rows updated” from the version check. Treat 0 rows as conflict: re-read, recompute, retry.
Two debits of 20. The balance fell by 20, and one client got success.
Sarah and John both read account 1: amount 40, version 1. John commits amount 20, version 2. Sarah’s update matches no row. The API ignored the row count and told Sarah 200 OK. 0 rows Restated: the database refused the stale write. The app still said “success”.
Four checks, in order
Does the write remember the version it read? Read the UPDATE’s WHERE clause. Why did Sarah match zero rows? Step through both transactions. John already moved it to version 2. Zero rows means conflict, not missing account. Did the API notice? Check what code does with the count. It ignores the count and returns 200. Her debit silently vanished. What is the safe retry? Can she resend the same UPDATE?
You treat zero rows as a conflict, not as success
0 rows? conflict, not success new amount + version Recompute + retry max 3 times Still failing → 409 The retry loop always starts with a fresh read. The version check in WHERE is the whole concurrency control. Zero rows is treated as a detected conflict. Forbids retrying a WHERE clause that can never match. Bounds retries and keeps transactions short.
Say these without the model
Walk John’s commit and Sarah’s update. Why is the balance 20 and not 0? the diagram Sarah’s debit was refused, not lost: her version check failed. Both read 40, version 1. No lock is held after reading. John writes 20, version 2, and commits first. Sarah still requires version 1, so nothing changes. no lock after read WHERE misses refused, not overwritten What must Sarah’s code do when the update count is zero? Stop, never report success, and re-read before retrying. Never retry the write without a version check. If the rule still allows it, update against the new version. 409 not 200 bounded retries When is a version column the wrong lock? optimistic vs pessimistic Optimistic: free on the normal path, cheap retry when rare. FOR UPDATE (lock the row on read) makes the second reader wait. A bare “read 40, write 20” with no check overwrites John. rare conflicts → version common → FOR UPDATE bare write overwrites Optimistic (version) No lock. Detect conflict at write. Best when conflicts are rare. Pessimistic (FOR UPDATE) Lock on read. Others wait. Best when conflicts are common. Is this the same as a database isolation level saving you? Repeatable read gives each a snapshot of 40. It does not merge writes. Forget the check and the second commit still overwrites the first. snapshot ≠ merge you wrote the WHERE isolation won’t invent it
This account is debited dozens of times a second
No lost debit No retries Short locks Atomic UPDATE in SQL FOR UPDATE then write Version + correct retry Version + same retry Conflicts are the common case. What do you switch to? Correct balance Lock wait SELECT … FOR UPDATE, then write Correct: the second debit waits, reads 20, writes 0. But the row stays locked for the round trip. Keep the version check and retry the same UPDATE The stale check matches zero rows every time. You spin and the debit never applies.
Have one ready
Two people save the same record. How does the loser find out? Version or token in the WHERE. Zero rows → a clear status code. Next try Re-read, recompute, cap retries — or one atomic UPDATE.
Did you show these?
John’s v2 commit came first No success on zero rows Re-read before retry Atomic UPDATE under heavy contention
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.