Lesson 9 of 9 · 26m
Concurrency, NUMA, and Memory Ordering
Why adding cores does not linearly add speed, how NUMA placement affects memory latency, and what atomics really cost.
Concurrency advice often stops at “use more threads.” Hardware adds the fine print: cores share caches and memory controllers, atomic operations serialize ownership, and on multi-socket or NUMA systems not all memory is equally close. The scalable design is usually the one with less sharing, not the one with more synchronization.
Amdahl is the first wall
If 90% of a task parallelizes perfectly and 10% is serial, infinite cores still leave the 10%. But real systems face a second wall: coordination overhead grows with sharing. A lock protecting a hot counter turns parallel work into a queue at the cache-coherence level.
threads: 1 2 4 8
ideal: 1x 2x 4x 8x
observed: 1x 1.7x 2.6x 3.1xThat curve usually means shared state, memory bandwidth saturation, scheduling artifacts, or all three. Measure before assuming the algorithm is “not parallel enough.”
Atomics are coherent cache traffic
An atomic increment is not merely a normal increment with a lock prefix or atomic instruction. It forces ownership of the cache line so the operation is indivisible and visible correctly. When many cores hammer the same atomic, the line becomes a global hotspot:
for each item:
atomic_fetch_add(&global_count, 1); // contention on one lineThe usual scalable replacement is per-thread accumulation in an ordinary local variable, followed by one merge at the end.
Sometimes atomic operations are correct and necessary. The lesson is not “never use atomics”; it is “do not turn a counter into the world's busiest cache line.”
Memory ordering is about visibility, not just ordering
C++ and Rust atomics expose orderings such as relaxed, acquire/release, and sequentially consistent. Acquire/release pairs establish visibility: writes before a release become visible to a thread that acquires the same atomic. Relaxed atomics only guarantee atomicity of the value itself, not ordering of surrounding memory.
data = 42;
ready.store(true, std::memory_order_release); // producer
if (ready.load(std::memory_order_acquire)) assert(data == 42); // consumerUsing seq_cst everywhere is often the correct starting point because correctness beats cleverness, but understand the cheaper orderings before optimizing them. Incorrect lock-free code is worse than a boring mutex.
NUMA: memory has a neighborhood
On NUMA machines, each socket or node has local memory. Accessing remote memory costs extra latency and bandwidth. Linux exposes topology and policy control through tools such as:
lscpu
numactl --hardware
numactl --cpunodebind=0 --membind=0 ./serverFirst-touch allocation matters: memory is often placed near the thread that first writes it. If thread 0 initializes a giant buffer and threads 8-15 use it from another node, you have built remote-memory traffic into the design.
Cores sharing L3 can cooperate efficiently when they read the same data. They can also evict each other's working sets. Partitioning data by core or node reduces coherence traffic, but too much partitioning creates imbalance. There is no universal topology-independent trick; expose the topology and measure realistic workloads.
Field notes
- Scalability failures often appear only at high core counts. A four-core laptop cannot reveal many eight-socket behaviors.
- Lock-free does not mean wait-free or fast. It describes progress guarantees, not performance.
- False sharing can make independent atomics contend if they share a line.
- Pinning threads can reduce migration noise, but it can also hurt utilization if the scheduler needed flexibility.
- The best synchronization is usually architectural: ownership, batching, message passing, and immutable snapshots.
Was this lesson helpful?