One row or many: what a transaction must keep together

What single rows get for free, why several rows need a transaction, and how to retry safely. A free visual lesson with sample data.

More on Transactions: ACID

Keep learning

Overview

chapters · transactions, isolation and locks One row or many: what a transaction must keep together Two changes that belong together can come apart, even when nothing fails on its own. emails / unread INSERT email 💥 crash no transaction unread + 1 never ran Wrong counter until someone fixes it by hand.

1. ACID is a promise with fine print

What each ACID letter really depends on Really means Provided by abort safety: partial work thrown away (not concurrency) database (undo log) your rules stay true (sums, no orphans) as if one at a time, to a chosen degree database, by level database (WAL, replication)

2. Single-object writes are protected almost for free

Crash mid-row → old or new row (redo/undo). Row lock: no one sees half a row. Single-object ops Single-object operation One safe read-modify-write. Many NoSQL “transactions” are only this. Multi-object transaction Many reads/writes across rows, tables, indexes: one outcome. MongoDB < 4.0 atomic per document only embed order lines in one doc duplication, size limits, two docs disagree MongoDB 4.0 (2018)

3. Why many objects must change together

Crash between the two inbox writes, with and without a transaction. Foreign keys A line without its order is an orphan. Denormalised copies Counters, cached totals, duplicates: move together. Secondary indexes Without transactions After failure, find which steps ran and undo by hand. With transactions Always before or after, never between.

4. The weakest guarantee you still must have: no dirty writes

A dirty write overwrites another transaction's uncommitted value. buyer / invoice Alice / – overwrites uncommitted Bob / – invoice → Bob Bob / Bob invoice → Alice Bob / Alice Without a row lock held until commit. Blocked at every level Even read uncommitted: row lock until commit. Dirty read ≠ dirty write A different, optional problem. Lost updates still possible No dirty writes doesn’t stop them.

5. An abort is a feature, and retrying it is not free

Retry pitfalls What goes wrong Commit OK, ACK lost client retries → done twice idempotency key / unique request id Failure is overload retries add load Error is permanent constraint fails forever retry only deadlock, serialization, timeout Side effects inside email / payment each attempt after commit, or outbox table In-memory state retry reuses stale values

Remember

Consistency is yours A, I, D come from the database. One row: free Many objects: need a transaction (or one document). Dirty writes blocked Lost updates, write skew are not. Safe retry Idempotent, backed off, side effects outside. → next: lost updates and write skew

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.