Skip to content
DenseChip
Learn
PracticeCode labInterview

DenseChip

Navigate

Menu

LearnPracticeCode labInterview

Tools

All toolsSV editorCode repoResume
DenseChip

Practice, courses, and chip-design tools in one focused workspace.

LearnPracticeCode labInterviewToolsLearning paths

© 2026 DenseChip

AboutContactPrivacyTerms
Skip to lesson

Course contents

  • How to Use This Course
  • Reading Paths
  • Concept Map
  • How to Read the Diagrams
  • Part 0: What Is a Protocol?10
  • Part 1: Timing and Clocking11
  • Part 2: Encoding and Framing12
  • Part 3: Handshakes13
  • Part 4: Flow Control17
  • Part 5: Transactions19
  • Part 6: Concurrency and Performance17
  • Part 7: Ordering13
  • Part 8: Sharing and Arbitration12
  • Part 9: Deadlock and Liveness13
  • Part 10: Reliability and Recovery20
  • Part 11: Bring-Up and Link Management10
  • Part 12: Power8
  • Part 13: Shared State and Coherence9
    • Why Coherence
    • Invalidate or Update
    • MSI, MESI and MOESI
    • Snooping or Directory
    • Ownership and Forwarding
    • Coherence and Consistency
    • I/O Coherence and Cache Maintenance
    • Protocols Compared: Shared State and Coherence
    • Drills: Shared State and Coherence
  • Part 14: Security and Isolation7
  • Part 15: Specifying and Verifying11
  • Part 16: Protocols Dissected17
  • Part 17: Capstones5
  • Part 18: Interview Preparation6
Loading lesson

‹Part 13: Shared State and Coherence
  1. Topics
  2. First Principles of Hardware Protocols
  3. Part 13: Shared State and Coherence
  4. Why Coherence

Part 13 · Shared State and Coherence · Working (W)

Why Coherence

Caches make copies, and copies go stale. Why a system with private caches needs a coherence protocol, the single-writer, multiple-reader invariant every protocol enforces, and what it costs when unrelated data shares a line.

On this page

    Note

    Level: Working (W) · Answers: What if there are several copies of one value?

    Before this: Memory Consistency Models

    Core 0 and core 1 each have a private cache. Both read a flag from memory: it is 0, and each cache keeps a copy. Core 0 sets the flag to 1, in its own cache. Core 1 checks the flag again, finds its copy, and reads 0. It will keep reading 0 for as long as its copy survives. Nothing is broken in either cache; each is doing exactly what a cache does. The system as a whole is wrong.

    When every core keeps its own copy of memory, how do all of them see one consistent value?

    The obvious answer, and why it breaks

    The obvious fixes are to not cache shared data, or to write every store straight to memory. Not caching shared data throws away the reason caches exist: shared data is often the most frequently used. Writing through to memory updates memory, but core 1's stale copy is still in core 1's cache, and core 1 never looks at memory again while it hits. The copies themselves must be kept in step, and that needs messages between the caches: a protocol.

    Each cache behaves correctly on its own; together, two copies of one location hold different values, and nothing tells core 1.

    The idea: one writer or many readers

    Every coherence protocol enforces the same two rules for each memory location:

    • Single writer, multiple readers (SWMR): at any moment, either one cache may write the location, or any number may read it, never both.

    • Data value: a read returns the value of the most recent write, in the order the writes were made.

    To enforce them, a cache that wants to write must first get rid of every other copy (or update them), and a cache that wants to read must get the latest value, even if it is only in another cache. Each cache line carries a state saying which permission this cache holds; the protocol is the set of messages that changes those states.

    Cache wants to

    Must first

    Read a line it does not have

    Get the latest value: from memory, or from the cache that holds it dirty

    Write a line it shares

    Make every other copy invalid (or send them the new value, lesson 13.2)

    Write a line it does not have

    Both: get the value and remove the other copies

    The cost: lines, not variables

    Coherence works on whole cache lines, typically 64 bytes. Two variables that happen to share a line behave as one: if core 0 writes a and core 1 writes b in the same line, the line bounces between their caches on every write, although they share no data. This is false sharing, and it can make a parallel program slower than its serial version. Padding or aligning the data so each core's variables sit in their own lines removes it.

    How real systems chose

    System

    Coherence

    Multicore processors

    Hardware coherence among all cores' caches, invisible to software

    AMBA ACE and CHI

    Coherence built into the interconnect protocol, with snoop channels or snoop messages (fact A23)

    Many accelerators and DMA engines

    Not coherent, or coherent only one way; software manages copies (lesson 13.7)

    What breaks

    A parallel counter (illustrative) gave each of eight threads its own counter in an array of eight 8-byte integers. All eight sat in one 64-byte line. The program ran slower on eight cores than on one, because every increment moved the line. Padding each counter to its own line gave a near-linear speed-up.

    Why this matters

    Senior lens. Coherence is a protocol like any other in this course: requests, responses, ordering and deadlock all apply. Before the details of any particular protocol, be able to state the invariant it enforces (SWMR plus the data value) and the unit it works on (the line), because most coherence bugs and most coherence performance problems come back to one or the other.

    Interview

    Asked: "Why do multicore systems need cache coherence?"

    Weak answer: To make caches faster.

    Strong answer: Because each core caches its own copy of memory, and when one core writes, other cores' copies go stale and they keep reading the old value from their own caches. Coherence keeps the copies consistent by enforcing, per location, single-writer, multiple-reader: either one cache may write or many may read, and a read returns the latest write. In practice a writer invalidates other copies first and a reader fetches the latest value, even from another cache. It works per cache line, which is why unrelated variables in one line cause false sharing.

    Follow-ups: (1) What is false sharing? (2) Why doesn't write-through fix it?

    Check yourself Why doesn't writing every store through to memory make caches coherent?

    Memory is updated, but other caches still hold their old copies and keep hitting on them without looking at memory.

    Check yourself Two threads write different variables in the same line. Is there a correctness problem? A performance one?

    No correctness problem (coherence keeps both writes), but a performance one: the line bounces between caches on every write (false sharing).

    Key takeaways

    • Private caches create copies that go stale when another core writes.

    • Coherence enforces single-writer, multiple-reader and the latest-value rule, per location.

    • It works on whole lines: unrelated data in one line causes false sharing.

    Keep going

    Next: Lesson 13.2, Invalidate or Update. Remove the other copies, or send them the new value? Modelled.

    Related topics

    • Part 13 overview: Shared State and Coherence

    • Architecture course: MESI States

    Previous

    180 / 234

    Next: Invalidate or Update

    Up next

    Up nextInvalidate or Update

    Sign in to save progress

    Practice this lessonReport an error

    See also

    • MSI, MESI and MOESI
    • Snooping or Directory
    • Ownership and Forwarding