Cache Coherence Explainer
Cache coherence is the machinery that keeps multiple CPU cores from using incompatible cached copies of the same memory. Each core has small, fast caches because reaching main memory is comparatively slow. If two cores read one address, both may hold a copy of the cache line containing it. Trouble begins when one core writes new data while another still has the old line. A coherence protocol tracks ownership and causes other copies to be invalidated or updated before they can be treated as current. It creates a consistent story for each memory location even though the physical data may exist in several places.
The protocol works at cache-line granularity, commonly dozens of adjacent bytes rather than one variable at a time. A line moves among states that describe whether it is clean, modified, shared, invalid, or held exclusively, depending on the processor’s protocol. A core that only reads a shared line can use its local copy. Before writing, it must obtain permission that prevents other cores from retaining a usable competing copy. If another cache owns the newest modified line, that cache may supply the data directly. Main memory can temporarily lag behind because the ownership records identify where the authoritative version resides.
Cores exchange coherence messages through an on-chip interconnect. Smaller systems may monitor broadcast requests, while larger processors use a directory that records which caches might hold each line. A read miss asks for current data; a request-for-ownership obtains exclusive write permission and invalidates sharers. The requesting core sometimes waits for acknowledgments before completing the transition. These transactions happen below ordinary software, yet their delay is visible in performance. A line already exclusive in the local cache is cheap to modify. A line bouncing repeatedly between distant cores can be hundreds of times more disruptive than a local arithmetic instruction.
Coherence does not by itself define every order in which independent memory operations become visible. Modern processors can reorder loads and stores for speed, and compilers can move operations unless language rules prevent it. Memory-ordering models describe the allowed observations, while fences, atomic operations, and synchronization primitives impose the relationships a program needs. Coherence supplies a foundation by serializing writes to an individual location and propagating ownership changes. Correct concurrent software still must use locks or atomics; two coherent caches can faithfully expose a race in which the program never established which operation should happen first.
A common performance trap is false sharing. Two threads may update entirely different variables, yet if those variables occupy the same cache line, every write transfers ownership of the whole line. The program has no logical data sharing, but the hardware sees constant contention. Developers can diagnose the pattern with performance counters and reduce it by separating frequently written fields, padding per-thread counters, or changing how work is partitioned. The opposite change can also hurt: excessive padding enlarges memory use and reduces cache efficiency. The useful goal is to keep genuinely shared read-mostly data together while isolating independently written hot data.
Coherence makes shared-memory programming practical because ordinary loads eventually observe the accepted value for an address without software manually flushing every cache. It is not free synchronization and it is not instant global replication. The protocol spends bandwidth, stores directory information, and may stall cores while permissions move. As systems add cores, designers balance broadcast simplicity, directory scale, topology, and power. For users, the key mental model is ownership of cache lines: reads may share, writes require control, and communication appears whenever that control changes. Program layout and synchronization determine how often the hardware must perform that hidden conversation continually.
Both cores can usually retain a clean line while they only read it. Before either core writes, it must gain write permission and invalidate or update the other usable copy.
If the other cache owns a newer modified version, it can provide that data. Coherence state records which copy is authoritative even when main memory has not yet been updated.
No. Coherence keeps writes to each individual memory location consistent across caches. Memory ordering defines how operations to different locations may be observed relative to one another.
Locks, atomic operations, and fences use both mechanisms to create program-level happens-before relationships. Coherence alone does not repair a data race or choose the intended order.
Caches transfer ownership by whole cache line, not by source-language variable. Two unrelated counters on the same line can therefore make the line bounce between cores whenever either counter changes.
That traffic adds latency and interconnect load. Separating frequently written fields or using per-thread storage can help, although unnecessary padding may waste memory and cache capacity.
Explore more "Explainers"
Discover additional explainers across politics, science, business, technology, and other fields. Each explainer breaks down a complex idea into clear, everyday language—helping you better understand how major concepts, systems, and debates shape the world around us.
