5.4.14 · D3Memory Hierarchy & Caches

Worked examples — Cache coherence problem

2,701 words12 min readBack to topic

You have met the coherence problem in the parent note Cache coherence problem. There you saw one failure table and three example sketches. This page does the opposite of "sketch": it lists every kind of situation the coherence problem can throw at you, then works each one to the last message count so you never meet a case you haven't seen.

Before we start, let us re-earn the vocabulary so no symbol sneaks in unexplained.

Everything below is just these words applied to different starting conditions.


The scenario matrix

Coherence outcomes depend on who holds the line, what operation happens, which protocol enforces coherence, and when memory gets refreshed (write-through vs write-back). Here is the full grid of case-classes this topic can produce:

# Case class The variable being stressed Covered by
C1 Single writer, other caches stale, no protocol baseline bug Ex 1
C2 Write-invalidate restores coherence the invalidate cure Ex 2
C3 Write-update restores coherence the update cure Ex 3
C4 Repeated writes, one writer (invalidate favoured) write-run length Ex 4
C5 Producer/consumer, read-after-each-write (update favoured) sharing tightness Ex 5
C6 Write-through vs write-back: is memory correct? when memory refreshes Ex 6
C7 Degenerate: only one core caches the line (no sharer) zero other copies Ex 7
C8 Degenerate: two cores only read, nobody writes zero writes Ex 8
C9 Limiting case: N sharers, one write — how traffic scales scaling with N Ex 9
C10 Real-world word problem + exam twist (false sharing) different addresses, same line Ex 10

Read the matrix as: rows C1–C3 are the three protocol behaviours; C4–C5 are when each protocol wins; C6 isolates the memory-freshness axis; C7–C8 are the degenerate (nothing-to-fix) cases; C9 pushes the sharer count to the limit; C10 is the sneaky one.

Throughout, the base scenario is: variable X starts at 5 in memory, cores are Core A and Core B (plus more when stated), caches are write-back unless a row says otherwise.


Worked examples


The scaling picture

Figure — Cache coherence problem

The figure plots coherence traffic for one write as the number of sharers grows (Example 9). The pale-yellow flat line is write-invalidate (always 1 broadcast). The chalk-pink rising line is write-update ( point-to-point messages). Look at where they cross: at they tie (1 each); beyond that, update loses, and by it costs 63× more. That crossing is the entire argument for why real CPUs are invalidate-based.


Recall

Connections