patternModerate
Lost Update Understanding
Viewed 0 times
understandinglostupdate
Problem
https://habr.com/en/company/postgrespro/blog/467437/ gives the following example of a Lost Update:
For example, two transactions are going to increase the amount on the same account by ₽100 (₽ is the currency sign for Russian rouble). The first transaction reads the current value (₽1000) and then the second transaction reads the same value. The first transaction increases the amount (this gives ₽1100) and writes this value. The second transaction acts the same way: it gets the same ₽1100 and writes this value. As a result, the customer lost ₽100
I read this a few times. But I don’t understand how the customer lost P100. Please explain.
For example, two transactions are going to increase the amount on the same account by ₽100 (₽ is the currency sign for Russian rouble). The first transaction reads the current value (₽1000) and then the second transaction reads the same value. The first transaction increases the amount (this gives ₽1100) and writes this value. The second transaction acts the same way: it gets the same ₽1100 and writes this value. As a result, the customer lost ₽100
I read this a few times. But I don’t understand how the customer lost P100. Please explain.
Solution
There are two separate transactions (T1 and T2) that each add ₽100 to the customer's balance.
The intended outcome is:
Or the other way around (T2 then T1). The important point is that both increments of ₽100 are applied.
In the example of a lost update, something like the following occurs:
This way the final balance is ₽1100, not ₽1200, so the customer has lost ₽100.
The intended outcome is:
- T1 reads the current balance as ₽1000, adds ₽100, and writes ₽1100
- T2 reads the current balance as ₽1100, adds ₽100, and writes ₽1200
Or the other way around (T2 then T1). The important point is that both increments of ₽100 are applied.
In the example of a lost update, something like the following occurs:
- T1 reads the current balance as ₽1000
- T2 reads the current balance as ₽1000
- T2 adds ₽100 (to the ₽1000 it read), and writes ₽1100
- T1 adds ₽100 (to the ₽1000 it read), and writes ₽1100
This way the final balance is ₽1100, not ₽1200, so the customer has lost ₽100.
Context
StackExchange Database Administrators Q#249351, answer score: 14
Revisions (0)
No revisions yet.