In this lesson · topic 4 of 7
Topic 4
Atomic in the end, visible in the middle
5 min read
Dirty reads
A step commits as soon as it runs. A reader who looks at the record a moment later sees it, even if the saga is about to fail and take it back. In the lab, 25%measured of the reads of half-done work saw something that was later undone. The reader acted on a charge that was refunded.
Other anomalies follow from the same cause. Two sagas can work on one record at the same time, and one can overwrite a value the other read. A saga can read a value, wait, and find it changed when it acts. A database transaction would rule these out. A saga leaves them to you.
Countermeasures
One fix is a semantic lock. The first step marks the record pending, and the last step or the compensation clears the mark. Other code checks the flag. With it on, the lab counted 0%measured dirty reads, because every read of a record in flight was told it is pending. That was 100%measured of the reads of half-done work, so the cost moved to the readers.
Other fixes work on the design. Make updates commutative, so order does not matter. Order the steps so the hardest to undo is last. Re-read a value just before relying on it. Keep a version on the record and refuse stale writes.
Predict first