Coherence and permission to write
Reason about shared copies, ownership transitions, and the boundary between coherence and synchronization.
On this page
The question for today
What you’ll understand
- Explain the single-writer, multiple-reader permission invariant.
- Trace a simplified MESI ownership handoff.
- Distinguish coherent storage from atomic operations and cross-address ordering.
Multiple cores can retain copies of one memory line. Without coordination, a core could keep reading an obsolete copy after another core changes the data. A coherence protocol coordinates those copies and the permission to modify them. It does not turn a multi-instruction algorithm into an indivisible operation, nor does it supply every ordering guarantee across different addresses. Learn the permission story first, then keep those separate correctness questions visible.
States describe permissions and obligations
In the simplified MESI vocabulary, Invalid means a cache lacks a usable copy, Shared permits reading a potentially shared clean copy, Exclusive denotes a clean private copy, and Modified denotes the private dirty owner. Clean and dirty compare the cached contents with backing memory at the relevant point in the protocol. Other protocols add states or use different names. A real implementation also needs transient states while messages, acknowledgments, and data are in flight.
The useful invariant is that write permission excludes other valid readable copies until the protocol completes the necessary transition. Multiple readers can coexist when no writer has exclusive permission. A reader requesting data currently held dirty elsewhere may receive the up-to-date data through the coherence fabric rather than through an immediate memory writeback. Those choices change traffic and latency, but the hardware must still satisfy its specified coherence and memory-model rules.
A coherent counter can still lose an update
Suppose two workers each load zero, compute one, and store one to the same counter. The final value can be one even if the underlying reads and writes participate in a perfectly coherent system. The problem is that the entire read-compute-write sequence was not one atomic operation. Ownership can pass between stores after both workers have already calculated their private results. Coherence cannot reconstruct the increment that the program failed to make indivisible.
In a language such as C++, unsynchronized conflicting accesses to an ordinary shared object can additionally create a data race with undefined behavior. Use the language's atomics or synchronization primitives rather than translating this explanatory hardware trace into unsafe source code. Atomic fetch-add can make each increment indivisible, while its chosen memory order controls additional ordering guarantees. Coherence, atomicity, language legality, and inter-address ordering are related layers with different responsibilities.
Ownership movement is work
A line repeatedly modified by different cores may require repeated permission transfers and data movement. Keeping most updates within one owner and combining them less frequently can reduce that traffic, but only if the algorithm permits delayed aggregation. A hot lock and its protected data may create a different access pattern from a read-mostly configuration object. Identify actual writers and readers, their placement, and how often ownership must change before proposing a new layout.
The reliability lab displays replica count n, unavailable count k, and majority threshold floor(n/2)+1. Increase k and predict when a majority disappears. This is explicitly a contrast: its circles are not CPU cache copies and its threshold does not implement MESI. A count of available copies says nothing about write permissions, message ordering, or the value a load may observe. Explain which missing state and transition rules a genuine coherence visualization would need.
Work it through
Two increments, one stored result
Use an abstract machine with individually atomic coherent loads and stores, no language-level data-race issue, and counter initially zero. Each of two workers performs load, add one privately, then store.
Choose the legal interleaving: worker A loads zero, then worker B loads zero. Both local registers now contain zero without violating the coherence of the memory location.
Both compute one. A obtains write permission and stores one; B later obtains permission and stores its already computed one. The stores have a coherent order even though the increment sequences overlap.
Read the final value after both stores: it is one rather than the intended two. Replacing the whole sequence with two completed atomic fetch-add operations would give two under the no-overflow assumptions.
Coherence orders competing storage operations; it does not make an arbitrary group of operations atomic or legal in a programming language.
The living notebook
Explore the model
Interactive lab · reliability
Copies are not a coherence protocol
Change replica count n and unavailable count k, predicting when the displayed majority becomes unavailable. These are availability counts, not cache states, and do not implement cache ownership or memory ordering.
- Available replicas
- 4 of 5
- Majority needed
- 3
- Majority available?
- Yes
A replica count and a majority threshold, without a consensus protocol. Playback highlights fixed replica states. Having a majority alone does not establish linearizability, durability or failure independence. Parameter changes pause playback and recompute the model at the current elapsed time. Reset restores both parameters and returns time to zero. Playback starts paused. Adjust either parameter, then choose Play or Step.
Look forCorrect replicated state requires rules about values and transitions, not just a count of reachable copies.
Pause & reason
Check your understanding
Your turn
Try it yourself
Eight workers each perform 1,000 completed atomic increments from an initial value of seven. Assume no overflow, resets, or failed operations. Find the final value and explain whether it proves anything about the ordering of unrelated data.
Take a moment to make a prediction or work it out on paper. Open a hint when you need one.
Give me a hint
Count completed increments separately from additional memory-order guarantees.
Compare with the solution
There are 8,000 increments, so the final counter is 8,007. This arithmetic follows from atomic increments to that object. It does not, by itself, show how unrelated ordinary reads and writes are ordered; that requires the language memory order, synchronization relationships, and architecture mapping. A coherent final counter alone is not a publication protocol for another data structure.
Keep the idea
Take it with you
- Coherence coordinates copies and write permissions at a defined granularity.
- Coherence does not make a multi-operation update atomic.
- Reachable copies, legal source-level synchronization, and cross-address ordering are separate questions.
Read further
Original sources and deeper explanations to follow at your own pace.
Optional progress, saved in this browser when available. Revisit any lesson at any time.
Lesson not marked complete.