Databases without transaction support can avoid lost updates with compare-and-set operations


Last Updated on Feb 17, 2021

In databases that don't support transactions, you can use a Compare-and-Set operation to avoid lost updates.

In a compare-and-set operation, updates are only allowed if the value that is being updated has not changed since the last read. If the current value does not match what was previously read, the Read-Modify-Write cycle must be retried.

An example compare-and-set operation in SQL would look like this:

UPDATE pages SET title='T21'
WHERE id=23424 and title='Terminal21';

Note that lost updates will continue to happen if the database allows reads on an old snapshot. The latest read operation may be fetching outdated data from the snapshot while the value has already been changed concurrently.

So you have to ensure that you are reading the current value from the database - not values from an old snapshot or within a transaction - to rely on a compare-and-set operation.


Related:

© 2022 Ambitious Systems. All Rights Reserved.