Skip to main content

Caches and Cache Coherence

1. Executive Summary​

Caches bridge the speed gap between CPUs and main memory by keeping copies of recently used data close to the cores that need them. In multi-core systems, each core may hold its own copy of the same memory location — creating a coherence problem: when one core writes, all other copies must be updated or invalidated so software observes a consistent memory model.

This chapter explains cache organization (direct-mapped, set-associative, fully associative), replacement policies, write policies, the MESI family of coherence protocols, false sharing, and cache-conscious data structure design. You will learn why a 1% change in data layout can cause a 10x performance swing, and why distributed caches at the application layer echo the same invalidation challenges at the hardware level.

Key takeaway: Caches are not transparent performance boosters — they are shared, finite resources whose behavior shapes scalability, tail latency, and correctness in concurrent programs.


2. Why This Topic Matters​

Principal architects routinely debug production incidents where "nothing changed in the algorithm" but throughput collapsed — often due to cache effects. Interview panels probe:

  • What is false sharing and how do you fix it?
  • How does MESI work at a high level?
  • Why do concurrent hash maps need careful design?
  • How do hardware caches relate to application-level distributed caches?

Understanding cache coherence connects low-level concurrency bugs to high-level architecture decisions: shard ownership, read replicas, and cache invalidation strategies in Distributed Caching mirror hardware invalidation traffic.

This chapter builds on CPU and Memory Fundamentals and precedes Memory Ordering and Concurrency.


3. Problems Being Solved​

ProblemDescriptionWhy it is hard
Capacity missesWorking set exceeds cache sizeMust evict useful data
Conflict missesMultiple addresses map to same cache setPathological access patterns
CoherenceMultiple cores hold stale copiesRequires inter-core communication
Write orderingWhen do writes become visible?Performance vs. correctness tradeoff
False sharingIndependent variables share a cache lineCoherence traffic without logical sharing

The goal is to maximize hit rate for hot data, minimize coherence traffic on shared writes, and design data layouts that align with cache line granularity.


4. Assumptions and System Model​

  • Cache line granularity: We assume 64-byte lines on commodity x86-64 and ARM servers unless stated otherwise.
  • Write-back, write-allocate caches are typical at L1/L2 for performance.
  • MESI or MOESI coherence on multi-core chips; directory or snooping implementation is microarchitecture-specific.
  • Sequential consistency not assumed for multi-threaded programs without synchronization.

Assumption to state in designs: "We assume standard cache coherence on commodity servers. Cross-socket coherence may use a directory or snoop filter; remote invalidation adds latency we account for in NUMA deployments."


5. Essential Terminology​

TermDefinition
Cache hitRequested data found in cache
Cache missData must be fetched from lower level
Cache line / blockUnit of transfer between cache levels
Set-associativeEach address maps to one set of N possible lines
TagAddress bits identifying which memory block occupies a line
Dirty lineModified in cache but not yet written to lower level
MESIModified, Exclusive, Shared, Invalid — coherence state machine
SnoopingCores monitor bus/interconnect for coherence transactions
DirectoryPer-line record of which cores hold copies (common at scale)
False sharingIndependent data in same line causes unnecessary invalidations
Write-throughWrites go immediately to lower level
Write-backWrites update cache; lower level updated on eviction

6. Core Mechanism​

Multi-core caches coordinate through coherence protocols. MESI is the canonical teaching model:

Explanation: A line in Modified state means this core has the only valid copy and main memory is stale. A write on a Shared line requires invalidating other cores' copies. Invalid means the line must be refetched on access. Transitions generate interconnect traffic — the cost of multi-core sharing.

Explanation: counter_a and counter_b are logically independent but physically share a cache line. Each write invalidates the other core's copy, forcing refetch — scalability collapses despite no logical data race.


7. Step-by-Step Walkthrough​

Scenario: Core 0 reads variable x; Core 1 writes x.

Step 1 — Core 0 read miss. x loads into Core 0's L1 in Exclusive state (assuming no other copies).

Step 2 — Core 1 write. Core 1 must obtain the line in Modified state. It sends an invalidate or read-for-ownership request.

Step 3 — Core 0 invalidation. Core 0's copy transitions to Invalid. If Core 0 reads x again, it misses and fetches the updated value.

Step 4 — Visibility. The coherence protocol ensures that after Core 1's write completes (in the coherence sense), Core 0 eventually sees the new value — but not necessarily immediately without memory barriers (see Memory Ordering and Concurrency).

Step 5 — False sharing variant. If Core 0 writes a and Core 1 writes b in the same line, each write triggers invalidation on the other core — ping-pong effect.


8. Invariants and Guarantees​

PropertyGuaranteeScope
CoherenceWrites to one address eventually visible to all coresPer-address, hardware-enforced
Atomicity of aligned accessesNative word-sized aligned loads/stores are atomicISA-dependent size (e.g., 64-bit on x86-64)
Cache inclusionSome hierarchies include lower levels in upper (not universal)Microarchitecture-specific

Not guaranteed without synchronization:

  • Ordering of writes to different addresses across cores.
  • That avoiding data races in source code eliminates false sharing.
  • Constant-time access — coherence misses add variable latency.

9. Failure Scenarios​

Scenario 1: False Sharing in Metrics​

A service exports per-thread request counters stored in a dense array. All threads update distinct indices.

Symptoms: CPU utilization high; throughput flat; coherence traffic dominates (perf c2c shows HITM — hit modified).

Mitigation: Pad each counter to a cache line (alignas(64)); use per-core sharded counters.

Scenario 2: Cache Thrashing in Hash Table​

A hash table with capacity just above a power of two causes many keys to map to the same cache sets (conflict misses).

Symptoms: Miss rate spikes despite small working set; worse than theoretical O(1) expectations.

Mitigation: Increase associativity cannot be controlled in software; change table size, use different hash bits, or restructure for open addressing with better locality.

Scenario 3: Read-Mostly Cache Stampede at Application Layer​

Mirrors hardware: thousands of clients invalidate the same application cache entry simultaneously.

Symptoms: Database overload; latency spike — analogous to coherence storm on a hot cache line.

Mitigation: See Cache Invalidation — probabilistic early expiration, request coalescing, single-flight.


10. Performance Characteristics​

Cache performance depends on:

  • Hit rate: Fraction of accesses served from cache.
  • Miss penalty: Cycles to fetch from lower levels.
  • Coherence traffic: Invalidations and writebacks on shared writes.

Read-heavy, immutable data scales well — lines stay in Shared state without invalidations.

Write-heavy shared state serializes through coherence — the hardware analog of a poorly sharded distributed lock.

Qualitative rule: one hot write-shared cache line can bottleneck an entire socket. Measure with hardware counters; do not invent hit-rate percentages.


11. Scalability Limits​

SignalLimitResponse
Many writers to adjacent fieldsFalse sharingPadding, sharding
Globally mutable hot structSingle line coherence bottleneckSplit fields; per-shard ownership
Working set > LLCCapacity missesReduce footprint; tiered storage
Cross-socket sharingRemote coherence latencyNUMA-local data structures

Application caches face the same limits at cluster scale: a hot key is a distributed false-sharing problem.


12. Operational Considerations​

Profiling tools: Linux perf c2c (false sharing detection), Intel VTune cache analysis, pcm-memory for bandwidth.

Deployment: Document cache-line size assumptions in performance-sensitive libraries. ARM and x86 are typically 64 bytes but verify.

Regression testing: Include microbenchmarks for concurrent counters and queues in CI for platform teams.

Hardware migration: Cache sizes and associativity change across CPU generations — re-profile after upgrades.


13. Security Considerations​

  • Cache timing side channels: Attacker measures access time to infer secrets (AES T-table attacks historically). Mitigation: constant-time algorithms, cache partitioning in some environments.
  • Flush+Reload: Coherence used to detect victim's cache access patterns across security domains — relevant for cloud multi-tenancy threat models.

Security-sensitive crypto code must be cache-aware, not only algorithmically correct.


14. Cost Considerations​

False sharing and coherence storms waste CPU cycles you already paid for — no additional hardware fixes bad layout.

Application-level cache clusters (Redis, Memcached) incur network and memory cost; hardware cache misses incur latency cost. Both reward ownership partitioning: each datum has one primary writer.


15. Production Implementations​

PatternExampleCache analogy
Padded countersstatsd, per-CPU kernel countersAvoid false sharing
SeqlockLinux kernel seqlock_tRead-mostly with rare writes
RCULinux Read-Copy-UpdateReaders without writer invalidation storms
Sharded concurrent mapsJava ConcurrentHashMap segmentsPartition coherence domains
CPU affinity for hot loopsDPDK, trading enginesKeep line in local cache

Extended: Replacement Policies and LRU Approximation​

Caches use replacement policies (LRU approximations, random, NRU) to choose victims on capacity miss. True LRU is expensive at scale; CPUs use pseudo-LRU trees or clock algorithms. Scan resistance matters for streaming workloads — one sequential pass can evict entire working set (cache pollution). Cache-friendly algorithms reuse blocks before eviction completes. Database buffer pools mimic these policies with LRU-K or clock sweeps — hardware and software caches share design tensions.

Extended: Prefetching Strategies​

Hardware stride prefetchers detect regular access patterns and issue prefetch requests. Software prefetch intrinsics (__builtin_prefetch) hint upcoming accesses — useful when access pattern is irregular but predictable by domain knowledge (graph traversal with known next node). Over-prefetching wastes bandwidth and evicts useful lines — profile before adding hints fleet-wide.


16. Alternatives and Tradeoffs​

ApproachProsConsWhen
PaddingSimple false-sharing fixMemory overheadHot counters
Per-core aggregationZero sharing during updateMerge complexityMetrics, histograms
Message passingNo shared mutable stateLatencyPipeline stages
Read-copy-updateFast readsDeferred reclamationRead-heavy config
Distributed cacheCross-process sharingNetwork invalidationMulti-instance services

17. Common Misconceptions​

  1. "Volatile fixes false sharing." — Volatile affects visibility ordering, not cache line placement.

  2. "Mutexes prevent false sharing." — Mutexes prevent data races; adjacent fields can still ping-pong cache lines.

  3. "MESI is only academic." — It explains production scalability cliffs.

  4. "More cores help read-heavy workloads always." — Even reads cause coherence traffic if another core writes nearby.

  5. "L3 is shared so L1 doesn't matter." — L1 miss rate still dominates hot loops.

  6. "Application cache replaces need to think about hardware." — Both layers need invalidation discipline.


18. Principal Architect Perspective​

Principal architects connect hardware coherence to system design:

  • Shard by cache-line-equivalent boundaries at scale: partition tenant data so hot writes do not collide.
  • Metrics pipelines should use per-instance aggregation before global merge — mirrors per-core counters.
  • Platform libraries should expose padded atomic types or document alignment requirements.

Interview signal at principal level: Candidate draws parallel between MESI invalidation and Redis pub/sub invalidation across app servers.

Extended: Associativity and Conflict Misses​

A set-associative cache maps each memory address to exactly one set containing N lines. If a working set of N+1 frequently used lines maps to the same set, capacity exists globally but conflict misses occur — lines evict each other in a ping-pong pattern. Matrix transpose and strided access patterns trigger this behavior. Mitigations include loop tiling (blocking) so submatrices fit cache levels, padding leading dimensions to avoid power-of-two strides, and choosing hash table sizes that reduce set collisions. This is distinct from capacity misses (total working set too large) and compulsory misses (first touch).

Extended: MOESI and Directory Coherence​

Production multi-socket systems often use MOESI (adding Owned — a core may respond to reads without memory fetch) and directory protocols that track which sockets cache each line. Snooping broadcasts invalidations on a bus; directories target only sharers, reducing traffic at scale. Cross-socket invalidation latency explains why NUMA-local allocation matters even when total RAM is sufficient. Interviewers may ask you to connect MESI invalidation to why a single hot lock degrades all cores — the lock metadata line becomes a coherence hotspot analogous to a hot Redis key.

Extended: Cache-Conscious Queue Design​

Lock-free Michael-Scott queues place head and tail pointers in separate cache lines intentionally so producer and consumer threads minimize coherence traffic. Without padding, head and tail updates on the same line cause false sharing despite correct atomic semantics. When reviewing concurrent data structures, always ask: who writes which line? The answer predicts scalability better than Big-O alone.


19. Architecture Review Exercise​

Scenario: A latency-critical order book stores bids and asks in a single struct array updated by 16 threads (one per symbol shard). After adding a per-symbol last_updated_timestamp field at the start of each struct, throughput dropped 40%.

Tasks:

  1. Explain a cache-related root cause.
  2. Propose struct layout changes.
  3. Describe how you would confirm with perf c2c.
  4. Relate to distributed cache invalidation if order book state is replicated.

20. Whiteboard Explanation​

"Each core has private L1/L2 caches but shared memory. When one core writes, hardware invalidates other cores' copies of that cache line — MESI tracks Modified, Exclusive, Shared, Invalid. If two independent variables sit in the same 64-byte line, writes ping-pong invalidations — false sharing. Fix by padding or giving each writer its own line. At scale, the same pattern appears when 100 servers invalidate one Redis key."


Extended Walkthrough: Designing a Sharded Metrics System​

Consider a metrics aggregator receiving 1M counter updates per second from 200 application instances. Version 1 stores all counters in a single ConcurrentHashMap with striped locks. CPU is high; scalability stalls at 32 cores.

Analysis: Profiling with perf c2c shows HITM events on hash bucket headers — false sharing and lock line contention. Even lock-free AtomicLong per key fails if keys hash to adjacent buckets in the same cache line.

Version 2: Partition counters by (tenant_id, metric_name) hash into 64 shards. Each shard is a struct padded to 64 bytes containing only local counters. Aggregation thread per shard; global merge every second.

Version 3: Eliminate cross-core writes entirely during ingest — each application instance maintains local counters; push deltas via UDP to assigned aggregator core using SO_REUSEPORT and CPU pinning.

Result framing (qualitative): Coherence traffic drops because writes stay on owned lines. This mirrors hardware lesson: minimize shared writable cache lines. At distributed scale, the same design appears in Distributed Caching with per-partition ownership.

Interview articulation: Candidate connects MESI invalidation, false sharing, sharding, and merge intervals without inventing throughput numbers — describes measurement plan instead.


Extended Failure Scenario: Cache-Induced Latency Spike During GC​

A JVM service experiences periodic p99 latency spikes aligned with young-generation GC. Investigation shows GC threads scanning or moving objects causing cache pollution — hot application data evicted from LLC. Subsequent requests miss cache until working set reheats.

Mitigation strategies (implementation choices): Increase young gen if allocation rate high; use object pools for short-lived garbage; colocate GC threads on separate NUMA node where possible; consider low-pause collectors for latency targets; reduce allocation rate in hot path. Principal angle: GC is not only heap management — it interacts with CPU cache hierarchy. Capacity reviews should include GC pause and post-GC cache rewarm effects for latency-sensitive tiers.


21. Interview Questions​

  1. What problem does cache coherence solve?

  2. Name the four MESI states and what triggers transitions.

  3. What is false sharing? How do you detect and fix it?

  4. Difference between write-through and write-back caches?

  5. What is a cache conflict miss?

  6. Why does padding a struct to 64 bytes sometimes improve multi-threaded performance?

  7. How does set-associativity reduce conflict misses?

  8. Compare snooping and directory-based coherence.

  9. How do hardware caches relate to application-level distributed caches?

  10. What is HITM in perf c2c output?

  11. Why might a concurrent queue suffer from cache effects even when algorithmically lock-free?

  12. How would you design a per-core metrics aggregator?


22. Interview Follow-Ups​

  1. After Q3: "Does std::atomic on adjacent variables prevent false sharing?" — No; need alignment/padding.

  2. After Q8: "When does directory beat snooping?" — Many cores; reduces broadcast traffic; common cross-socket.

  3. Principal: "Your team's shared config struct is a bottleneck — organizational fix?" — Platform-owned RCU config, versioned snapshots, change review for hot paths.


22A. Production Low-Level Design (Interview + Game Day)​

Design a per-core metrics aggregator that avoids false sharing on hot counters. Production LLD and Game Day Standard.

Control vs data plane​

PlaneRoleGuidance
ControlCounter schema, shard map, flush intervalPlatform-owned layout standards
DataPer-core accumulators → periodic mergePad to cache line; no shared hot lines

Hot-path LLD​

API: Inc(metric_id, delta), Snapshot() → map[metric]value Layout: alignas(64) per-core shard

Step-by-step flow:

StepMechanismOps hook
1–2Core-local padded countersNo HITM on Inc
3Background merge1s staleness OK
4Export totalsAlert coherence stalls
  • Detect: perf c2c HITM; negative scaling
  • SLO: p99 Inc < 100ns

22B. Game Day Playbooks​

#DrillInjectDetectMitigateVerify
GD-1False sharing stormPacked counter arrayHITM + throughput cliffPad 64B/counterLinear core scaling
GD-2NUMA remoteCross-socket threadsRemote DRAM > 30%numactl bindLocal LLC hits
GD-3Coherence stormMany writers/lineInterconnect saturationPartition linesWrites scale
GD-4Lock ping-pongHot lock metadataHolder migratesSharded locksContention drops

GD-1 — False sharing on packed counters​

23. Strong Answer Example​

Question: "Explain false sharing and give a production example."

Answer this as a 45-minute design discussion using the PRACTICE framework. Below is how a strong student sounds aloud—layout + coherence traffic, not "use a mutex."

Student interview script (PRACTICE)​

P — Problem framing​

"False sharing: independent writers in the same 64-byte cache line cause coherence invalidation on every write—even without a data race. Thread count can reduce throughput."

R — Requirements​

TypeRequirement
SafetyCorrect totals after merge
PerfInc avoids cross-core invalidation
OpsHITM before blaming locks

A — Assumptions​

AssumptionWhy it matters
64B linesIsolation unit
Write-heavy countersInvalidation dominates
1s merge OKMetrics tolerate lag

C — Capacity (sketch)​

Threads 8–64; 10k counters; packed = 8× invalidations; padded = isolated lines

T — Topology (draw this)​

Step-by-step flow:

StepClaim
1Same line → both invalidated
2Each Inc = remote HITM
3Pad/shard distinct lines
4–5Per-core merge

I — Invariants​

"Each hot writer owns a cache line; merge is off hot path."

C — Constraints​

ConstraintResponse
Mutex on counterDoesn't fix placement
Atomics onlyStill invalidate line

E — Evolution​

Profile HITM → pad/shard → load test scaling.

Closing line​

"False sharing is a layout bug—pad, shard, or merge off hot path. Mutexes don't fix line placement."

Scoring signals​

StrongWeak
MESI/HITM + layout fixConfuses data race; mutex-only

Compact outline (2 min)​

"Same line + independent writes = ping-pong. Pad 64B or shard per core."

24. Weak Answer Example​

Weak answer: "False sharing is when two threads access the same variable. Use a mutex."

Why weak: Confuses false sharing with data races; mutex does not fix line placement; no detection or layout strategy.


25. Hands-On Exercise​

  1. Implement two increment benchmarks: padded int64 per thread vs. packed array.
  2. Run with 1, 2, 4, 8 threads; plot throughput.
  3. Use perf c2c (Linux) to identify HITM on packed version.
  4. Document cache line size from getconf LEVEL1_DCACHE_LINESIZE.

26. Knowledge Check​

  1. MESI Modified means? (This core has sole valid copy; memory stale.)
  2. False sharing requires a data race? (No — independent variables, same line.)
  3. Typical cache line size? (64 bytes on most commodity servers.)
  4. Write-back vs. write-through? (Write-back defers DRAM update until eviction.)
  5. Coherence vs. consistency? (Coherence: same address; consistency: ordering across addresses.)

27. Flashcards​

FrontBack
MESIModified, Exclusive, Shared, Invalid coherence states
False sharingIndependent vars in same line cause invalidation ping-pong
Cache line64-byte typical unit of cache transfer
Write-backUpdate cache first; DRAM updated on eviction
Invalid stateLine not valid; must refetch on access
HITMHit modified — another core owns dirty line
Set-associativeAddress maps to one of N lines in a set
Conflict missMultiple addresses compete for same cache set
SnoopingCores observe coherence traffic on interconnect
DirectoryTracks which cores cache each line
RCURead-copy-update; minimizes reader invalidation
PaddingAlign data to cache line to isolate writers

28. Cheat Sheet​

MESI: Write → Modified · Solo read → Exclusive · Others read → Shared · Others write → Invalid

False sharing fix: alignas(64) · per-core shards · merge later

Detect: perf c2c · VTune · scalability cliff when adding threads

Read-heavy: Shared state OK · Write-heavy: partition ownership

App layer parallel: Hot Redis key = distributed false sharing



30. References​

  • Hennessy, J. L., & Patterson, D. A. (2017). Computer Architecture: A Quantitative Approach — Cache design and coherence.
  • Sorin, D. J., Hill, M. D., & Wood, D. A. (2011). A Primer on Memory Consistency and Cache Coherence — MESI and memory models.
  • Intel. Intel 64 and IA-32 Architectures Software Developer Manuals.
  • Linux perf-c2c documentation — False sharing detection tooling.