Photo by Brian Kostiuk on Unsplash
Cache Line False Sharing: Hidden Contention in Multi-Core Systems
Modern multi-core processors deliver parallel performance by giving each core its own cache hierarchy, but this architecture introduces a subtle performance trap: false sharing. When two threads access logically independent variables that happen to reside on the same cache line, the cache coherence protocol forces expensive synchronization between cores, even though there’s no actual data dependency.
The Cache Line Boundary Problem
CPUs don’t move data between caches in byte-sized chunks. Instead, they transfer entire cache lines, typically 64 bytes on x86 and ARM architectures. This granularity makes sense for spatial locality—when you access one field in a struct, you’ll likely need adjacent fields soon after. But it becomes a liability when multiple threads each own separate variables that happen to share a cache line.
Consider a simple counter array where each thread updates its own element. If those elements sit on the same cache line, every write by one thread invalidates the cache line in other cores, forcing them to reload it from a shared cache level or main memory. The threads are logically independent but become accidentally coupled through the cache coherence mechanism.
MESI and Cross-Core Invalidation
The MESI protocol and its variants govern how caches stay synchronized. When one core writes to a cache line, it must transition that line to the Modified or Exclusive state, which requires invalidating copies in other cores. Those cores then experience cache misses on their next access, despite modifying completely different variables.
This invalidation happens at cache line granularity, not byte granularity. The coherence protocol has no way to know that the two cores are touching different offsets within the line. All it sees is conflicting writes to the same 64-byte block.
Real-World Manifestations
False sharing appears frequently in concurrent data structures. A struct containing per-thread statistics might allocate an array of counters, one per thread. Without padding, these counters pack tightly in memory, and adjacent threads’ counters land on shared cache lines. Lock implementations can suffer similarly—a lock and its associated metadata might share a line with unrelated data that’s frequently updated.
Thread-local storage implementations sometimes fall into this trap when allocating small per-thread values sequentially in memory. The allocator optimizes for memory efficiency but creates cache line contention that destroys multi-core scalability.
Even read-only data can trigger false sharing if it shares a cache line with frequently written data. A thread repeatedly reading a constant while another thread writes to a nearby variable will see its read-only cache line repeatedly invalidated.
Detecting False Sharing
Profiling tools on modern CPUs can detect false sharing through performance counters that track cache line invalidations and remote cache accesses. Tools like Linux perf can sample on events like MEM_LOAD_L3_MISS_RETIRED or OFFCORE_RESPONSE, revealing which addresses experience excessive cross-core traffic.
The symptom typically appears as poor scaling when adding cores. A workload that should scale linearly instead plateaus or even degrades because the cores spend more time synchronizing cache state than doing useful work. The code has no visible locks or atomics, making the contention non-obvious in source review.
Mitigation Strategies
The standard fix is padding. Insert enough empty space between independently accessed variables to ensure they land on separate cache lines. Many codebases define a CACHE_LINE_SIZE constant and align hot variables to that boundary, either through compiler attributes or manual padding in struct layouts.
Some languages and libraries provide abstractions that handle this automatically. Rust’s crossbeam crate includes a CachePadded wrapper type. Go’s runtime adds padding to certain internal structures. Performance-critical C++ code often uses alignas(64) to force alignment.
Another approach is data layout redesign. Instead of an array of structures where each structure contains per-thread data, use a structure of arrays where each array is thread-local. This naturally separates each thread’s working set into distinct memory regions.
When Sharing is Intentional
Not all cache line sharing is false. When threads genuinely coordinate through shared variables, cache line bouncing is unavoidable and correct. The distinction matters: false sharing is accidental contention between logically independent data, while true sharing reflects actual algorithmic dependencies.
Understanding this difference guides optimization priorities. Eliminating false sharing can unlock linear scaling, but no amount of padding will help code with fundamental serialization points. Profiling tells you which situation you’re in.