Skip to content

Memory Consistency Models

Coherence (MSI/MESI) guarantees that all cores see the same sequence of values for a single address. But what about the order of operations across different addresses?

Consider this classic example:

Core 0: Core 1:
x = 1 while (flag == 0) {}
flag = 1 print(x)

Coherence guarantees that when Core 1 sees flag = 1, it sees the correct value of flag. But does it also see x = 1? That depends on the consistency model.

A memory consistency model defines the set of allowed orderings for memory operations across all cores. It’s the contract between hardware and software:

  • Hardware promises: “I will only reorder operations in these specific ways”
  • Software assumes: “I can rely on these ordering guarantees”

The fundamental question: when Core 0 writes xx then writes yy, does Core 1 see the write to xx before the write to yy?

Modern CPUs use deep pipelines, out-of-order execution, and store buffers for performance. Strict ordering forces the CPU to stall. Relaxing ordering constraints allows:

OptimizationRequires relaxing
Store bufferStore → Load ordering
Write combiningStore → Store ordering
Speculative loadsLoad → Load ordering
Out-of-order executionAll orderings

The more reordering the hardware is allowed to do, the faster it can run — but the harder it is for programmers to reason about concurrent code.

From strictest to most relaxed:

  1. Sequential Consistency (SC) — no reordering
  2. Total Store Order (TSO) — only Store→Load reordering
  3. Relaxed models (ARM, RISC-V) — nearly all reorderings possible

Each step down the spectrum gives hardware more freedom but requires programmers to use memory fences to enforce ordering where needed.

The widget below enumerates every SC interleaving of two short threads and lists the reachable final-register states. Try both the Store Buffering (SB) and Message Passing (MP) tests — and notice the outcomes flagged “forbidden under SC” at the bottom. Those are exactly the cases real hardware can produce that distinguish weaker models from SC.