Skip to content
EngineeringAll explainers

Transaction management: ACID properties, isolation levels, sagas

A short guide to transaction management in databases, covering the ACID properties, what each SQL isolation level actually prevents, and why transactions across microservices need sagas rather than a database guarantee.

Published: Updated: Reading time: 6 min
On this page

Transaction management is the set of database mechanisms that make a group of operations succeed or fail together, as one unit, instead of leaving a system where some of them happened and others didn't. Each such unit is a database transaction, and the rules it follows are the ACID properties. Take a transfer of $500 between two bank accounts: the database debits one account and credits the other as a single operation, because stopping after the debit would make $500 disappear. That guarantee is what lets an application attempt a multi-step change, such as a checkout that also writes a ledger entry, without custom recovery code for every way it could fail.

ACID properties: what the four guarantees mean

ACID names the four guarantees a database gives every transaction it runs. The term comes from a 1983 paper by Haerder and Reuter.

  • Atomicity. The transaction happens completely or not at all. Any failure partway through rolls back every change already made.
  • Consistency. A transaction only moves the database from one valid state to another. Constraints like a foreign key or a non-negative balance check still hold after it commits.
  • Isolation. Concurrent transactions don't see each other's uncommitted work, to a degree set by the active isolation level.
  • Durability. Once a transaction commits, the change survives a crash, because it's recorded in a write-ahead log on durable storage first.

Database isolation levels and what they prevent

Atomicity and durability are all or nothing. Isolation isn't: you pick how much of it you want. The SQL standard defines four isolation levels, and each rules out an anomaly the level below it allows. Real engines don't always match the standard, so it's worth reading your database's own rules, such as the PostgreSQL transaction isolation documentation.

LevelPrevents (SQL standard)Still allows (SQL standard)In PostgreSQL
Read uncommittedNothingDirty, non-repeatable and phantom readsBehaves as read committed, so no dirty reads
Read committedDirty readsNon-repeatable and phantom readsThe default level
Repeatable readDirty and non-repeatable readsPhantom readsAlso blocks phantoms, but write skew is still possible
SerializableAll threeNothingAborts conflicting transactions, which you must retry

A dirty read means seeing changes another transaction hasn't committed yet, and some databases (SQL Server and MySQL among them) do allow it at read uncommitted. A non-repeatable read means the same query returns different values twice in one transaction because someone committed in between. A phantom read means a second query turns up rows another transaction inserted in the meantime.

Serializable makes transactions behave as if they ran one after another. Locking engines enforce that by making transactions wait. PostgreSQL instead lets them run and aborts one with a serialization failure when they conflict, so your code needs a retry loop.

Why distributed systems break the model

A database transaction is a promise the database can keep because it controls every piece of data involved. That stops holding once the operations span more than one database, the normal shape of a system integration problem: an order service, a payment service, and an inventory service each own their own data, and no single commit can cover all three.

Two-phase commit is the classical answer. A coordinator asks every participating database to prepare the change, and only once all confirm does it tell them to commit for real. It reproduces ACID's all-or-nothing guarantee across separate databases, but participants typically hold their locks and wait during the prepare phase. If the coordinator dies after some participants have prepared but before sending the final instruction, those participants stay locked with no safe way to decide alone. That blocking risk is why teams often avoid two-phase commit across services, even though it's still used inside some databases and in enterprise XA setups.

The saga pattern for distributed transactions

A saga replaces one distributed transaction with a sequence of small, local transactions, each committed on its own and paired in advance with a compensating action. The idea dates to a 1987 paper by Garcia-Molina and Salem. If step four of six fails, a saga doesn't unwind steps one through three inside the database. It runs their compensating actions instead, usually in reverse order, each one its own transaction. Sagas are often coordinated through events, which ties them closely to event-driven architecture.

Here's the part teams miss: a compensating action isn't an undo. Crediting money back is a new transaction, separate from the original charge, so a statement usually shows both the charge and the reversal. If you design the refund flow as if the charge never happened, it won't match the ledger, and someone ends up reconciling the difference by hand.

Common transaction management mistakes

Two customers can check out the same last unit in stock and both think they got it, if nothing locks the row between the read and the write. That race is the textbook case. In practice, these show up more often:

  • Long-running transactions that hold locks while waiting on something slow, like an external API call or a user's next click. Every other transaction touching the same rows queues behind it, and the whole app feels slow even though the database itself is fine.
  • Treating a call to another service as if it were inside the transaction. If the transaction rolls back after sending a payment confirmation, the processor still has that confirmation. The transaction boundary only covers the local database.
  • Retrying a failed payment request with no idempotency key. HTTP methods like PUT and DELETE are idempotent under RFC 9110, but a payment is usually a POST, which isn't. Without an application-level key that lets the server spot the repeat, a retry after a timeout can charge someone twice.

When transaction management needs care

Most application code never touches an isolation level directly, and that's fine. Framework and ORM defaults handle ordinary reads and writes well enough.

It changes once money moves, inventory has to match a physical warehouse, or a record exists that a regulator might ask to see. A sensible starting point for those systems:

  1. Keep read committed as the default for ordinary work.
  2. Use serializable, or explicit row locks, for balance and stock updates, and wrap them in a retry on serialization failure.
  3. Give every request that moves money an idempotency key.
  4. Design each saga step's compensating action before you write the step.

Mistakes here rarely throw an error when the code runs. They tend to surface later, as a balance that doesn't reconcile. That's why payment and ledger code is worth an outside review before release, the kind of work behind our custom software development and software development consulting engagements on fintech systems.

Frequently asked questions

  • A database transaction is a group of reads and writes that the database treats as one unit. Either every change in it commits, or the database rolls all of them back. Transaction management is the machinery that enforces this, so a failure halfway through a transfer or a checkout never leaves half the work saved.

  • Atomicity, consistency, isolation, and durability, the four guarantees a database gives every transaction. Atomicity means a transaction happens completely or not at all. Consistency means it can't leave the database violating its own rules. Isolation means concurrent transactions don't corrupt each other's view of the data, to a degree set by the active isolation level. Durability means a committed change survives a crash.

  • The SQL standard defines read uncommitted, read committed, repeatable read, and serializable. Each one rules out more concurrency anomalies than the level below it, at the cost of more waiting or more retries. Real databases differ in the details. PostgreSQL, for example, treats read uncommitted as read committed and blocks phantom reads at repeatable read.

  • Two-phase commit keeps one transaction's all-or-nothing guarantee across several databases, but every participant holds locks and waits during a coordination step, which gets costly at scale. A saga splits the operation into separate local transactions, each with its own compensating action, and accepts a short window where the overall operation is only partly complete.

  • No. A transaction only covers the database that started it. Once an operation touches a second service with its own database, you need a pattern built for distributed systems, usually a saga, rather than a bigger transaction. Calls to other services also need idempotency keys so a retry can't apply the same step twice.

  • Concurrency. Serializable gives the strongest guarantee but makes transactions wait on or retry against each other far more than read committed does. Most operations never touch data where a race would corrupt anything. Keeping the strict level for the few that do, like a balance transfer or a contended inventory decrement, keeps the rest fast without giving up correctness where it counts.

Still unanswered
Ask us directly

A senior engineer replies under 4 hours.