{"canonical_url":"https://engineer.company/glossary/transaction-isolation/","content_markdown":"# Transaction isolation\n\n## What it is\n\nTransaction isolation determines which changes from concurrent transactions an\noperation can see and which combinations of results a database will allow to\ncommit. A transaction can group several statements into one decision. For\nexample, a booking service can read available places, write a reservation, and\nrecord the amount it expects to collect. Isolation governs what happens when\nanother transaction reads or changes the same information during that decision.\n\nA database can use snapshots to give readers a consistent view while other\nsessions write. PostgreSQL uses a new snapshot at the start of each ordinary\nquery under Read Committed, its default isolation level. A later query in the\nsame transaction can therefore see work committed after the earlier query began.\nUnder PostgreSQL's Repeatable Read, ordinary reads use a transaction snapshot\nset when the first relevant statement runs. The transaction also sees its own\nchanges. PostgreSQL's Serializable level checks dependencies between concurrent\nwork and can reject a transaction when their combined result would be impossible\nin any serial order. Several transactions can still run at once.\n\nThe labels describe guarantees, and implementation details matter when choosing\none. A snapshot explains what a query can see. Write conflicts, constraint\nchecks, and explicit locks add rules about the changes a transaction can make.\nIt helps to trace both halves. Ask which rows each statement reads, then ask\nwhich later commits can invalidate the decision those rows support.\n\n## Why it matters\n\nConsider a team documenting the last place in a training session. Two workers\ncan each see one place available and each try to record a booking. An\napplication that performs a check followed by an unconditional write has left\ntime for another decision between its two statements. Committing each worker's\nstatements together still leaves a question about whether both decisions can\nsucceed. The invariant is that confirmed bookings fit within capacity. The data\nmodel and the statements have to give the database enough information to protect\nit.\n\nSome invariants are local to a row. A conditional decrement can tie an inventory\nwrite to the current quantity. Other invariants span records. Suppose a rota\nrequires at least one operator on duty. Two people can each use a snapshot that\nshows the other person on duty, then each update a different record. Their\nwrites can avoid a direct row conflict even though the combined decision would\nleave nobody on call. This is a case of write skew. A transaction-level snapshot\nsupplies repeatable observations but can still leave that combined decision\npossible in systems that allow snapshot isolation without a serializability\ncheck.\n\nChoose isolation with the decisions in mind. An approximate dashboard that\nrefreshes in several independent panels can tolerate each panel seeing a recent\nsnapshot. A report that reconciles a ledger total against its component rows\nneeds those reads to describe the same point in time. A workflow that updates\nseveral related records may need a documented serialization guarantee, a\ndatabase constraint or a shared lock that writers consistently use. The\nconsequences of an inconsistent result tell you which behaviour to test.\n\n## What it looks like in practice\n\nA useful test gives two independent database connections a predictable schedule.\nBegin a transaction on the first connection and read a stock item. Commit a\nchange from the second connection. Then read the same item again on the first\nconnection. Record what each read returns and the exact moment each connection\ncommits. Run an additional case in which both connections are trying to reserve\nthe last item. An explicit wait in the test should correspond to a visible\nevent, such as a completed statement, so its result has meaning under a busy\ntest runner.\n\nEngineer's\n[limited-stock engineering note](/notes/selling-limited-stock-without-overselling/)\nillustrates a simpler shape for the last-item decision. One conditional database\nstatement performs the decrement only while stock is available. In PostgreSQL\nunder Read Committed, when that update encounters a row already updated by\nanother transaction, it can wait for the first one to finish and re-evaluate its\ncondition against the updated row. An application can use the count of affected\nrows to distinguish a successful change from a reservation that could not be\nmade. The note also calls out the cost of holding the database lock across a\nnetwork payment.\n\nAn illustrative review would test both the final balance and the decision\nreported to each caller. It could then add a pair of operations that depend on\nseveral rows and check which isolation level or constraint enforces that wider\nrule. Separating the tests makes the evidence easier to interpret. A passing\nlast-item test establishes a particular invariant for that write path. A\nmulti-row scheduling rule needs its own case and an explicit explanation of what\nmakes concurrent decisions safe.\n\n## Where it goes wrong\n\nApplications sometimes retry only the final statement after a serialization\nfailure. The earlier reads can be the reason the database rejected the\ntransaction. Start a new transaction, repeat the decision against its new view,\nand bound the number of attempts so busy clients leave room for other work.\nDistinguish a serialization failure from a validation error and from a client\ndeadline. The application should report whether a decision was accepted,\nrejected or still uncertain after a lost response.\n\nExternal side effects need their own policy. A database rollback can discard a\nreservation record. A message another system already received needs a separate\nrecovery path. For a retryable transaction, keep that external action outside\nthe retried decision and give it a stable identity that helps its consumer\nhandle duplicates. After a lost connection near commit, use a durable operation\nidentifier or a read-back procedure to establish whether the action was already\nrecorded.\n\nLong-running views also have a cost. Reports that retain snapshots for a long\ntime can delay the removal of older row versions. Keep the time spent inside a\ntransaction visible, particularly when application code waits on the network or\na user interface. A test fixture should cover the configured isolation level\nused by the application's production connection factory. Finding a driver\ndefault in documentation is useful. Observing the actual connection setting and\nthe two-session result makes the decision easier to defend when the database or\nclient changes.\n\n## Further reading\n\n- [PostgreSQL transaction isolation](https://www.postgresql.org/docs/18/transaction-iso.html).\n  Describes concurrent reads and writes at each supported level.\n- [PostgreSQL SET TRANSACTION](https://www.postgresql.org/docs/18/sql-set-transaction.html).\n  Documents when a connection assigns transaction characteristics.\n- [PostgreSQL concurrency control](https://www.postgresql.org/docs/18/mvcc.html).\n  Connects isolation to locks, consistency checks and application behaviour.\n\n\u003chttps://engineer.company/glossary/transaction-isolation/\u003e\n","date_modified":"2026-11-25T07:18:46Z","description":"See what two database sessions can observe, how to exercise an inventory invariant and when to retry a complete transaction.","formats":{"gemtext":"https://engineer.company/glossary/transaction-isolation/index.gmi","gopher":"https://engineer.company/glossary/transaction-isolation/gophermap","html":"https://engineer.company/glossary/transaction-isolation/","json":"https://engineer.company/glossary/transaction-isolation/index.json","markdown":"https://engineer.company/glossary/transaction-isolation/index.md","pdf":"https://engineer.company/glossary/transaction-isolation/index.pdf"},"kind":"page","language":"en","title":"What is transaction isolation?","url":"https://engineer.company/glossary/transaction-isolation/","version":1}