Lost Update in Database Systems

On occasion, data in a database can become incorrect due to a lost updates.

When database transactions are executed, they are typically sequential and very dutifully update the data as expected. On occasion, transactions happen nearly simultaneously, which can lead to something called a lost update. A lost update may cause data within the database to be incorrect, which can lead to problems with normal operations, such as fulfilling customer orders.

Lost Update

A lost update occurs when two different transactions are trying to update the same column on the same row within a database at the same time. Typically, one transaction updates a particular column in a particular row, while another that began very shortly afterward did not see this update before updating the same value itself. The result of the first transaction is then “lost,” as it is simply overwritten by the second transaction.

The image shows the sequence of events that can occur in a lost update. Notice that both transactions are seeing a beginning value of 7 for the quantity column; however, the second transaction needs to see a value of 6 to be correct, as the first transaction was initiated in order to update that same column.

Since the second transaction is unaware of the change made by the first transaction, it simply changes the quantity to 10, overwriting and thus losing the update made by the first transaction. As you can see, if the quantity is first lowered to 6 before the second transaction makes an update, then the second transaction would do the correct calculation and update that value to 9. Instead, the second transaction still sees 7, and thus updates it to 10 instead of 9, causing the quantity to be incorrect!

What Can Be Done to Prevent a Lost Update?

  1. Optimistic locking vs pessimistic record locking

Reference

  1. https://vladmihalcea.com/a-beginners-guide-to-database-locking-and-the-lost-update-phenomena/
  2. https://dzone.com/articles/what-is-a-lost-update-in-database-systems