Lost Update and Write Skew Explained with Examples

Lost updates, write skew and phantoms that snapshot isolation misses, with the on-call doctors example and the locks or constraints that fix each.

In this chapter

Write anomalies: lost updates, write skew and phantoms that snapshots miss

More on Transactions: isolation

Keep learning

Overview

chapters · transactions, isolation and locks Write anomalies: lost updates, write skew and phantoms that snapshots miss Snapshots stop the read anomalies, but read → decide → write still breaks when someone changes what you read. in app code meanwhile input changed

1. Same pattern, three disguises

They differ only in what both touch. three write anomalies compared lost update write skew phantom write both read ROW X both write ROW X same object both read SET S write different rows different objects both check "none exists" both INSERT a match rows not there yet easy to fix needs locks or SSI hardest to lock Lost update Same row. Easy to fix. Write skew Different rows. Needs locks or SSI. Phantom write Rows not there yet. Hardest to lock.

2. Lost update: the second write erases the first

Two transactions read one row, compute in the app, write back. The later write uses a stale value. sales, before qty × price product 1 product 2 qty (product 1) reads 10 10 + 10 in app 20 (pending) stale read! T1's +10 gone should end at actually ends at What happens to T2's write? T2 update Change lost? READ COMMITTED, any engine goes through MySQL REPEATABLE READ applies to latest row, writes 15 PostgreSQL RR / SERIALIZABLE serialization error → retry reads 20, writes 25 ✕ Compute in the app ✓ Let the database do it Choosing a fix Best when Watch out for Atomic update Change is a formula on the old value Cannot express multi-read logic Explicit lock Version column Check the affected-row count Engine detection PostgreSQL RR or serializable Last write wins Almost never This is the bug, as a policy Replicated / multi-leader? No single latest value: keep siblings and merge. → lost update interview walkthrough

3. Write skew: no row is written twice

Table test holds a, a, b, b. Two updates run at once under snapshot isolation (PostgreSQL RR, or PG READ COMMITTED). rows 1–2 rows 3–4, no conflict values swapped! Serial results All b (T1 first) or all a (T2 first) Snapshot result b, b, a, a: no serial order gives this MySQL default RR How it catches the swap. Real case: at least one doctor must stay on call. Which protections leave nobody on call? On-call doctor Meeting room claimed twice negative across accounts spent twice Atomic updates Conflict is a read vs the other's write. Constraints, sometimes Unique/exclusion: usernames, room bookings.

4. Phantoms turn the lock idea upside down

A phantom starts matching your condition after you checked. FOR UPDATE cannot lock zero rows. T1: book 10:00 T2: book 10:00 room 4 bookings no rows to lock Ways to close a phantom write Unique / exclusion constraint Index rejects the second row Best when the rule fits Materialise the conflict Rule leaks into schema Gap or range locks InnoDB RR locks the empty range (gap locks) Blocks unrelated inserts in the gap Predicate detection (next chapter) Retries, or range-lock waits

Remember

Read, decide, write Make decide+write one step, or detect the change. Lost update Write skew passes SI Lock inputs, lock row, constraint, or serializable. Phantoms beat row locks The row does not exist yet.

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.