A Read–Modify–Write Sequence and Lost Updates

Separate reads and writes can cause concurrent workers to overwrite each other's changes if the operation lacks a suitable coordination rule.

Source
Anbloq
Language
English
Created
Updated
Chapters
4
Reading time
1 min
In this article4 chapters
  1. Trace a counter
  2. Choose an atomic expression or a checked update
  3. Write both computed values before either is saved
  4. Reproduce the ordering without sleeps

Trace a counter

Two workers read count ten. Each separately calculates eleven and writes that value. The combined intent is twelve, yet the final count can be eleven under a naive sequence. See state traces.

Choose an atomic expression or a checked update

Use a database-supported increment or concurrency control matching the operation. Verify the final state after two successful attempts. Transaction anomalies cover what isolation changes.[1]

Write both computed values before either is saved

Two workers read stock count ten. Each attempts to add two and stores twelve. Even if both writes complete, the result twelve loses one increment. A database-side atomic increment, row lock or version condition can address different versions of this problem. See conditional updates.

A reads 10
B reads 10
A computes and stores 12
B computes and stores 12
Desired final value after both additions: 14

Reproduce the ordering without sleeps

In a concurrency test, pause each worker after it reads and release them according to the planned schedule. Verify committed results from an independent connection. Include failure or conflict handling when using optimistic updates.

References

  1. PostgreSQL Global Development Group, Concurrency Control ↩