Home / Learn / Chiplets and Memory / Cache Coherence Explained

On this pageWhy caches existThe problem with several coresWhat coherence guaranteesThe MESI protocolHow caches find out about each otherWhat coherence costsFalse sharingCoherence and consistencyCoherence in manycore designsCoherence beyond the processorWhy it matters for system designCommon questionsRelated guides

Cache Coherence Explained

Cache coherence is the guarantee that when several processor cores hold copies of the same data in their caches, they all see a consistent value. Each core keeps recently used data in its own fast cache. If one core changes a value, the copies held by others become stale. A coherence protocol detects this and makes sure no core goes on using outdated data. It is what allows multicore processors to share memory safely.

Why caches exist

Main memory is slow compared with a processor. To avoid waiting for it constantly, each core has a small, fast memory called a cache that holds copies of the data it has used recently. Most accesses are served from the cache.

With one core, this is uncomplicated. The cache holds copies, and the core is the only thing that can change them.

The problem with several cores

Suppose two cores each have a copy of the same value in their caches. Core A changes it. Core B’s copy is now wrong. If Core B carries on using it, the program produces incorrect results.

A multicore processor that shares memory must prevent this. The mechanism that does so is the cache coherence protocol.

What coherence guarantees

Two rules capture the idea.

  • One writer or many readers. At any moment, a given piece of data can be either writable by exactly one core or readable by any number of cores, never both.
  • Latest value. A read returns the value from the most recent write.

The MESI protocol

The most widely taught protocol is named after its four states. Each block of data in each cache is in one of them.

StateMeaning
ModifiedThis cache has the only copy, and it has been changed. Main memory is out of date.
ExclusiveThis cache has the only copy, and it matches main memory.
SharedOther caches may also have copies. All match main memory. The data is read-only.
InvalidThis entry does not hold valid data.

Blocks move between states as cores read and write.

  • A core that reads data nobody else has receives it in the Exclusive state.
  • If another core then reads it, both copies become Shared.
  • A core that wants to write Shared data must first tell every other cache to invalidate its copy. It then holds the data as Modified.
  • If another core wants data that is Modified elsewhere, the owner supplies the current value.

Variants add further states to optimize common cases, such as allowing a cache to share modified data without first writing it back to memory.

How caches find out about each other

Snooping. Every request is broadcast to all caches. Each checks whether it holds the data and responds. It is fast and simple with few cores. Broadcast traffic grows quickly as cores are added.

Directory-based. A directory records which caches hold each block. A request goes to the directory, which forwards messages only to the caches concerned. There is less traffic and more delay per request. It scales to many more cores.

SnoopingDirectory
Messages per requestBroadcast to allSent only to relevant caches
LatencyLowerHigher, extra step through the directory
Scales to many coresPoorlyWell
Storage overheadSmallDirectory must track sharers

What coherence costs

  • Message traffic. Every invalidation and every transfer of ownership is a message on the chip’s interconnect.
  • Delay. A write to shared data waits until the other copies are invalidated.
  • Scaling. The more cores share data, the more messages are needed.
  • Storage. State must be kept for every cache block, and a directory needs to record who holds what.
  • Verification effort. Coherence protocols are notoriously hard to get right. Messages can cross in transit, creating rare situations that only careful analysis uncovers. Formal verification is used heavily here.

False sharing

Coherence works on blocks of memory, typically 64 bytes, not on individual variables.

If two cores repeatedly write two different variables that happen to sit in the same block, the hardware treats it as contention over one piece of data. Ownership of the block bounces between the cores on every write, even though they are not actually sharing anything.

This is called false sharing. It can slow a parallel program dramatically. Programmers avoid it by arranging data so that values written by different threads fall in different blocks.

Coherence and consistency

Two related terms are easy to mix up.

  • Coherence is about cache copies of a single location agreeing with one another.
  • Memory consistency is about the order in which reads and writes to different locations appear to happen across cores.

Coherence is a hardware mechanism that is invisible to programs when it works. Consistency models define what parallel programs can rely on, and they vary between processor architectures.

Coherence in manycore designs

As core counts rise into the dozens and hundreds, coherence becomes harder.

Full coherence across every core means a large volume of protocol messages on the on-chip network and a directory that tracks many sharers. Designers have several responses.

  • Hierarchical coherence. Cores are grouped into clusters. Coherence is maintained within a cluster, and a second level handles sharing between clusters.
  • Limiting the scope. Shared, coherent memory within a cluster, and explicit message passing between clusters.
  • Compact directories that track sharers approximately to save storage.
  • Dedicated network resources for coherence messages so they are not delayed by data traffic.

The choice affects both hardware cost and how software is written.

Coherence beyond the processor

Coherence is no longer confined to the cores of one processor.

Accelerators. An accelerator that shares memory with a processor benefits from taking part in coherence, so it can work on data in place without copies being made.

CXL. The Compute Express Link standard includes a protocol that lets an attached device cache the host’s memory coherently.

Chiplets. When cores are spread across several dies, coherence messages must cross die-to-die links, and the protocol has to tolerate the added delay.

On-chip bus protocols. Standard protocols for connecting processor cores to shared caches and to each other include coherence support, so that cores and other blocks from different sources can interoperate.

Why it matters for system design

  • For chip architects, the coherence scheme determines interconnect traffic, storage overhead and how well the design scales.
  • For IP integration, processor cores, interconnect and shared caches must all implement compatible protocols.
  • For programmers, understanding coherence explains why some parallel programs scale poorly and how to fix them.
  • For verification teams, it is one of the most demanding parts of a multicore design.

Common questions

What is cache coherence in simple terms?

It is the mechanism that keeps the copies of data held in different cores’ caches in agreement, so that no core uses a stale value after another core has changed it.

What does MESI stand for?

Modified, Exclusive, Shared and Invalid, the four states a block of cached data can be in under that protocol.

What is false sharing?

A performance problem that occurs when different cores write to different variables located in the same cache block. The hardware treats the block as contended, causing needless transfers between caches.

Related guides