Most developers learn early that wrapping database writes in a transaction makes them safe: either every statement succeeds or none of them do. That part is true. What surprises people — usually in production, usually under load — is that a transaction does not, by itself, stop two concurrent users from reading the same data, making decisions based on it and both writing results that are wrong when combined.
The classic example is the last seat on a flight, the last ticket for a concert or the last item in stock. Two requests arrive at nearly the same moment, both see one seat left, both book it, and the system has sold the seat twice. Both requests used transactions. Understanding why that can happen, and what isolation levels do and do not promise, is one of the most practically useful pieces of database knowledge a developer can have.
What a transaction actually guarantees
The usual summary is ACID: atomicity, consistency, isolation and durability. Atomicity means the statements in a transaction take effect together or not at all. Durability means that once a commit is acknowledged, the change survives a crash. Consistency, in this sense, means the database’s own constraints — unique keys, foreign keys, check constraints — hold before and after.
Isolation is the property that matters for concurrency, and it is the one that comes in degrees. Full isolation would mean every transaction behaves as if it ran alone, one after another. Providing that is expensive, so databases offer weaker isolation levels by default and let you choose stronger ones when you need them.
The four standard levels
The SQL standard defines four isolation levels, from weakest to strongest: read uncommitted, read committed, repeatable read and serializable. Each level rules out certain kinds of interference, called anomalies, and permits others. Real databases implement these levels in their own ways, so the exact behaviour of a level depends on the engine, but the vocabulary is shared.
Defaults differ too. PostgreSQL uses read committed by default; MySQL’s InnoDB engine uses repeatable read by default. Many applications never change the default and never think about it, which is fine for most operations and quietly dangerous for a few specific ones.
The anomalies, in plain terms
A dirty read happens when a transaction sees another transaction’s changes before they are committed — changes that might still be rolled back. Most engines effectively never allow this in practice, even at the weakest level. A non-repeatable read happens when a transaction reads the same row twice and gets different values because another transaction committed a change in between.
A phantom happens when a transaction runs the same query twice and gets a different set of rows, because another transaction inserted or deleted matching rows. And there are anomalies the original standard did not name clearly but that bite real systems: the lost update, where two transactions read a value and both write a new value based on it, so one update silently overwrites the other; and write skew, where two transactions each check a condition, each make a change that is valid on its own, and together violate the condition.
Walking through the double booking
Picture a seats table with a column for remaining seats. Request A starts a transaction, reads the row and sees one seat remaining. Request B starts a transaction, reads the same row and also sees one seat remaining, because A has not written anything yet. A decides the seat is available, decrements the count to zero and inserts a booking. B makes the same decision from the same stale information and also books.
At read committed, nothing here breaks the rules: each transaction read committed data and wrote valid statements. The bug is in the pattern — read a value into the application, decide in application code, write back — which leaves a window in which the decision can become wrong. Whether stronger isolation catches it depends on the level and the engine, which is why it is better to design the pattern safely rather than rely on a default.
Fix one: make the database do the check
The simplest fix is to move the decision into a single statement that the database executes atomically. Instead of reading the count and then writing, issue an update that only succeeds if a seat is available — an update that decrements the remaining count with a condition that the count is greater than zero — and check how many rows were affected. If the answer is zero, the seat was gone and the booking is refused.
Because the condition and the change happen in one statement, two concurrent requests cannot both pass the check: the database serialises the two updates on that row, and the second sees the already-decremented value. Constraints help in the same way: a unique constraint on a seat number in a bookings table makes a second booking of the same seat fail outright, whatever the application logic believed.
Fix two: lock what you are about to change
When the logic is too complex for one statement, you can take a row lock explicitly. In many SQL databases, reading with a locking clause such as SELECT … FOR UPDATE locks the selected rows until the transaction ends. A second transaction trying to lock the same rows waits until the first commits or rolls back, and then sees the updated values.
Locks work, but they have costs. They make concurrent requests queue, so they should be held for as short a time as possible — never across a network call to another service or a user’s think time. And locking rows in inconsistent orders across different code paths can produce deadlocks, which the database resolves by aborting one of the transactions; your code must be ready to retry.
Fix three: optimistic concurrency
Another approach avoids holding locks at all. Give the row a version number or a last-modified timestamp. Read it along with the data, and when writing, include the version in the condition: update the row only if the version is still the one you read, and increment it as you write. If another transaction changed the row in between, your update affects zero rows, and you know to reload and try again or report a conflict.
Optimistic concurrency suits situations where conflicts are rare and holding locks would be wasteful — editing a document, updating a profile, changing settings. It turns a silent lost update into a visible, handleable conflict, which is almost always the right trade.
Fix four: serializable isolation with retries
The strongest option is to run the sensitive transaction at the serializable level, which promises that the outcome is equivalent to running the transactions one at a time in some order. PostgreSQL, for example, implements this with a technique called serializable snapshot isolation, which tracks dependencies between concurrent transactions and aborts one of them when it detects that their combination could not have happened serially.
The catch is that serializable transactions can fail with a serialization error even though nothing is wrong with your code, and the correct response is to retry the whole transaction. Applications that adopt serializable isolation need a retry loop around the transaction, with a limit, and transactions that are safe to repeat — which means no side effects such as sending emails inside them.
Where the rules stop applying
Everything above concerns a single database. As soon as a decision spans two systems — a payment provider and your database, two microservices with their own stores, a cache and a database — database isolation cannot protect you, because no single transaction covers both. Those cases need different patterns: idempotency keys, outbox tables, compensating actions and careful handling of retries.
Isolation guarantees also assume you use the database’s transactions properly. Reading data in one transaction, doing work in the application, and writing in a separate transaction gives up most of the protection, whatever level you choose. Keep the read, the decision and the write inside one transaction, or collapse them into one statement.
A practical checklist
Find the places in your system where a read is followed by a decision and a write: stock levels, balances, quotas, seat counts, unique usernames. For each, ask what happens if two requests interleave. Prefer a single conditional statement or a database constraint; use row locks or optimistic versioning when the logic is more complex; reserve serializable isolation, with retries, for the cases where invariants span many rows.
Then test it. A simple concurrency test that fires many simultaneous requests at the same resource and checks the invariant afterwards — no negative stock, no double booking — will find in minutes the race that would otherwise surface on your busiest day.
Keep transactions short
Whatever isolation strategy you choose, long-running transactions cause trouble. They hold locks longer, increase the chance of conflicts and deadlocks, and in engines that keep old row versions for consistent reads, they can prevent cleanup of those versions and slowly bloat tables.
Do the slow work — calling external APIs, rendering templates, waiting for user input — outside the transaction, then open the transaction, perform the read-decide-write sequence quickly and commit. A transaction should be measured in milliseconds wherever possible, not seconds.
In short
Transactions make groups of writes atomic and durable, but isolation comes in levels, and common defaults allow anomalies such as lost updates and write skew. The double-booking bug is a read-decide-write pattern with a window between the read and the write.
Close the window by letting the database check and change in one statement, by enforcing constraints, by locking rows briefly, by versioning rows optimistically, or by using serializable isolation with retries. Know your engine’s default, keep decisions inside a single transaction, and test the invariant under concurrent load.