STM - Middle¶
An STM tracks a read set and write set, validates versions, then publishes writes atomically.
flowchart TD
Begin --> Read[Read transactional variables]
Read --> Compute
Compute --> Validate{Versions unchanged?}
Validate -->|yes| Commit
Validate -->|no| Backoff[Retry]
In Clojure, use dosync and alter for coordinated refs; use atoms for one independent value. In Haskell, retry blocks until a read transactional variable changes, while orElse composes alternatives. Keep the block short and move warehouse or object-store I/O after commit.
Continue to senior.md.
Test yourself¶
- What information is in the read and write sets?
- Why is
retrybetter than busy waiting? - When is an atom simpler than STM?