Concepts and theorems · Intermediate

Cache coherence: coordinating copies of one memory location

Distinguish coordination of cached copies of one location from ordering constraints across different locations.

Ask Logeia about this articleTry the three questionsReview your answers

Updated

On this page
  1. Why multiple caches need coordination
  2. An invalidation example
  3. How false sharing appears
  4. Coherence does not replace synchronization
  5. Ask Logeia about this articleCheck your understanding

Why multiple caches need coordination

When several cores cache copies of a memory location, a write by one core must be coordinated with those copies. Cache coherence concerns the consistency of observations of that location. Implementations can use line states, invalidation, and ownership transfers, although the exact protocol is processor-specific. The mechanism does not require every write to reach main memory immediately. A cache can hold modified data while the overall system still maintains the coherence rules. Distinguish the abstract guarantee from a particular assumption about where the newest physical copy resides.

An invalidation example

Consider an illustrative invalidation-based protocol. Cores A and B both hold a readable copy of one line. Before A modifies it, A obtains the required write permission and B's old copy is invalidated. A later B access cannot simply use that stale cached copy. The example explains the purpose of coordination without claiming that all processors use exactly these states. Notice that the management unit can be an entire cache line, even when the application mutation concerns only one small variable inside it.

How false sharing appears

Two threads can update different counters and still compete if those counters occupy one cache line. Repeated ownership transfers can then impose a cost even though the application-level variables are distinct. This is false sharing: the shared hardware management unit, rather than a logically shared variable, creates interference. Layout changes or padding can help an observed case, but increase memory usage and depend on actual alignment and line size. Measure the write pattern before adding padding everywhere, especially when the larger footprint could worsen capacity behavior elsewhere.

Coherence does not replace synchronization

Coordinating writes to one location is not the same as ordering a payload store before a flag store at another location. A processor with coherent caches can still require the synchronization prescribed by the programming-language memory model. Locks or correctly paired release and acquire operations provide guarantees that cache coherence alone does not establish. It is therefore unsafe to infer that a data race is harmless because the hardware is coherent. Evaluate the costs of sharing separately from the proof that the application publishes and accesses its data correctly.

Reading and practice are recorded separately in this browser, for each language.

Learn this in order

Build your CS foundations · STEP 8

Understand the systems behind the web · STEP 3

Prepare for AI systems · STEP 5

Choose a learning path