Lost Updates


Last Updated on Feb 17, 2021

The Lost Update problem can occur if an application reads some value from the database, modifies it, and writes back the modified value (a Read-Modify-Write cycle). If two transactions do this concurrently, one of the modifications can be lost because the second write does not include the first modification. In other words, the later write clobbers the earlier write.

Some scenarios where this can occur are:

  • Incrementing a counter or account balance.
  • Changing a part of a complex value, like a JSON document, and writing back the modified document.
  • Two users edit a document (like a wiki) simultaneously, sending the entire page content to the server to overwrite the existing record.

While databases can automatically detect lost updates and abort transactions, it is essential to watch for data loss possibilities and safeguard against them.

One way to avoid losing updates is to lock the object(s) being changed and disallow other transactions from reading them. The other transactions have to wait until the first transaction commits or aborts.

The other way would be to perform atomic writes when the database supports atomic operations and when the update is concise enough to atomic.

The application can also do a compare-and-set operation for every change. But it has to fetch the current value in the database, not the value of the beginning of the transaction or an older snapshot.


© 2022 Ambitious Systems. All Rights Reserved.