lost update
A concurrency bug where two sessions read the same value, both write a result based on it, and the later write silently erases the earlier one.
It silently discards real changes, such as a sale or a payment.
In more detail
Two sessions each read stock 5, each sell one and write 4, and one sale vanishes. Fix it by computing the change in the database with a conditional write, such as SET stock = stock - 1 WHERE stock >= 1, or with a stricter isolation level.
Related terms
- compare and set — An update that changes a row only if it still holds the value the program expects, and reports whether it did.
- race condition — A bug whose outcome depends on the timing of two things happening at once.
- isolation level — A setting that controls how much concurrent transactions can see of each other's changes and how they may interleave.
- affected row count — The number of rows an INSERT, UPDATE or DELETE actually changed, reported back to the program that ran it.
- row lock — A database lock on one row that makes other transactions wait before changing it.
- optimistic concurrency control — A way to prevent lost updates by reading a version, preparing a change, and making the write succeed only if that version is still current.
Taught in
- Isolation Levels And The Lost Update — PostgreSQL, Indexing & Relational Data Modeling
- Worked Design One: Ticket Booking, Correctness First — Scalable System Design & Distributed Architecture