Mechanical sympathy
Mechanical sympathy is the practice of designing software with an understanding of the hardware it runs on, so that code works with the machine rather than against it. The term comes from motorsport, where a driver benefits from feeling how a car behaves under load. In software it means knowing how CPUs, memory, caches, and buses interact, and shaping programs to match those mechanics.
The idea was popularized by Martin Thompson, who applied it to building high-throughput systems such as the LMAX architecture. Rather than treating hardware as an opaque platform, mechanically sympathetic development makes concrete characteristics of the machine explicit design constraints.
Common principles include:
- Predictable memory access. CPUs and caches favor sequential, localized access over random jumps. Choosing data layouts and access patterns that match this behavior reduces cache misses and improves performance.
- Cache-line awareness. Data is moved between memory and caches in fixed-size lines, commonly 64 bytes. Variables that are unrelated but share a cache line can cause false sharing, where cores invalidate each other’s caches even though they are writing to different data. Padding or aligning data to separate cache lines can avoid this cost.
- Single-writer principle. Writable state is owned by one thread or actor, and other threads submit writes through message passing instead of contending for shared mutable data. This reduces locking, avoids race conditions, and improves concurrency and parallelism.
- Natural batching. Instead of waiting for a fixed batch size or a timeout, a worker gathers available items as they arrive and dispatches a batch as soon as the queue is empty or the batch is full. This reduces latency jitter and increases throughput.
Mechanical sympathy is closely related to data-oriented design and to careful memory allocation. It is not premature optimization: it assumes measurable goals exist and that changes are driven by profiling, not by guessing.