Lost update

On this page 7

A lost update is a concurrency anomaly in which two transactions read the same data item, each computes a new value from what it read, and the later write silently overwrites the earlier committed change. In database transactions, it is one of the classic failures that isolation and locking mechanisms exist to prevent, because the database reports success to both clients while one result disappears.

The anomaly typically follows a read-modify-write cycle performed in application code: read a balance, add to it, write it back. It is not a dirty read, since both transactions read only committed data, and it occurs at isolation levels that many production systems use by default, which makes it one of the most common real-world data integrity bugs.

How a lost update unfolds step by step

A lost update requires two overlapping transactions that read before either writes. Each transaction bases its new value on a snapshot that becomes stale the moment the other one commits, and nothing in the write path compares the stored value with the value that was read.

The interleaving below shows two clerks adding stock to the same inventory row, which starts at 100 units. Each adds a delivery, yet the final value reflects only one of them.

StepTransaction ATransaction BStored quantity
1Reads quantity 100Idle100
2IdleReads quantity 100100
3Writes 100 + 20 = 120 and commitsIdle120
4IdleWrites 100 + 30 = 130 and commits130
ResultDelivery of 20 is lostDelivery of 30 is keptExpected 150, stored 130
Interleaving that produces a lost update

The critical point is step 4: transaction B writes a value computed from an outdated read. Without a lock, version check or isolation rule, the database has no reason to reject it.

Lost update and isolation levels

Whether a database prevents lost updates depends on the isolation level and the engine’s implementation. The SQL standard defines levels through dirty reads, non-repeatable reads and phantoms, and the well-known critique by Berenson and colleagues added the lost update as a separate phenomenon that read committed permits and stronger levels must exclude.

Isolation levelLost update possibleTypical mechanism
Read uncommittedYesNo read protection
Read committedYes, for read-modify-write in application codeStatement-level snapshots or short read locks
Repeatable readEngine-dependentDetection in PostgreSQL, not in MySQL InnoDB
Snapshot isolationNoFirst-committer-wins write conflict check
Serializable isolationNoFull serializability guarantees
Lost update protection by isolation level

Engine differences matter in practice. PostgreSQL repeatable read aborts the second writer with a serialization failure, whereas MySQL InnoDB repeatable read lets the second update proceed against the latest committed row, so the lost update still occurs unless the application uses locking reads such as SELECT ... FOR UPDATE.

Techniques that prevent lost updates

Several well-established techniques prevent lost updates, and they differ mainly in when the conflict is detected and what the losing transaction must do. The simplest is often an atomic update that moves the arithmetic into the database, for example UPDATE stock SET qty = qty + 20 WHERE id = 7, which never exposes a stale value to the application.

TechniqueHow it worksMain trade-off
Atomic updateComputes the new value inside one statementOnly fits simple expressions
Pessimistic lockingLocks the row on read with FOR UPDATEBlocking and possible deadlocks
Optimistic lockingWrites only if a version column is unchangedLosing transaction must retry
Compare-and-setWrites only if the value still equals the old oneSubject to the ABA problem
Stronger isolationDatabase aborts conflicting writersRetries and higher abort rates
Prevention techniques compared

Optimistic locking suits web applications where users read a record, think for minutes and then save, because holding a lock across user think time is impractical. A version number or timestamp travels with the form, and the update includes WHERE version = 12; zero affected rows signals a conflict that the application reports or merges.

The lost update is easily confused with neighboring anomalies, yet each has a distinct pattern of reads and writes. Distinguishing them matters because each needs a different defense.

AnomalyPatternData affected
Lost updateTwo writers overwrite the same item from stale readsSame row
Dirty readReader sees uncommitted dataSame row
Non-repeatable readSame row reads differently within one transactionSame row
Write skewTwo writers update different items based on a shared readDifferent rows
Phantom readA repeated query returns new matching rowsSet of rows
How the lost update differs from related anomalies

Write skew is the closest relative: it is effectively a lost update spread across different rows, and snapshot isolation does not prevent it, while it does prevent the single-row lost update.

Where lost updates hide in real systems

Lost updates often escape testing because they require precise timing under concurrent load and leave no error in any log. They surface as small, unexplained discrepancies in counters, balances and inventories.

  • ORM entity saves that write every column, including fields another user changed meanwhile.
  • Counters and balances incremented in application memory instead of with atomic statements.
  • Read-modify-write of JSON documents, where one client overwrites keys another client added.
  • Cache-aside patterns that write a stale cached object back to the database.
  • Retry logic that replays an old value after a timeout instead of rereading the row.

Detection usually starts with reconciliation checks that compare a stored total with the sum of its underlying events. Concurrency tests that run the same operation from many threads against a shared row, followed by an assertion on the final value, reproduce the anomaly far more reliably than manual testing.

In everyday language

Imagine two people editing the same shared shopping list on paper copies. Both start with the same list, each adds an item, and each hands back a fresh copy. Whoever hands in last replaces the other copy, so one person’s addition simply vanishes.

A lost update is that situation inside a database. Nobody sees an error, which is why it is dangerous. The fixes are to let only one person edit at a time, to check that nothing changed before saving, or to send the change itself instead of a whole new copy.

Seen in the real world

An online ticket seller stores remaining seats per event as a single number. During a popular release, thousands of checkout requests each read the count, subtract the purchased seats in application code and write the result back. Running at read committed, the system sells about 3 percent more tickets than seats because concurrent purchases overwrite each other’s decrements.

The engineering team rewrites the purchase as one atomic statement, UPDATE events SET seats = seats - 2 WHERE id = 81 AND seats >= 2, and treats zero affected rows as sold out. Overselling stops completely, and the change removes a round trip, so checkout latency also drops.