DEV Community

Alexey Tolmachev
Alexey Tolmachev

Posted on

The Ghost of 4 Nanoseconds: How the Linux Scheduler Killed Determinism at 41.5M TPS

Summer Bug Smash: Smash Stories šŸ›šŸ›¹

This is a submission for DEV's Summer Bug Smash: Smash Stories track.

In the world of Medium-Frequency Trading (MFT) and high-load topological netcode, determinism is a religion. If two nodes receive the same sequence of inputs, their final state hash must match identically. There is no room for "almost."

While finalizing the monolithic C++ architecture for the release version of my proprietary engine—TolmachЁv Netcode SDK v36.0.0—we encountered a "Heisenbug" that seemingly broke the laws of physics.

We were targeting absolute zero CPU validation waste, pushing 41.5 Million TPS with a physical RTT of ~24ns. But once every 14 to 16 hours, one node in the distributed mesh would suddenly throw a state hash mismatch and drop off the network. No packet loss. No logic errors. Just a perfect, inexplicable desync.

Here is the story of how a 4-nanosecond ghost in the machine almost broke our deterministic core.

Act 1: The Observer Effect

When a high-frequency system crashes every 15 hours, your first instinct is to profile it.

I attached standard profilers and added timestamp logging to the critical event loop. And immediately, the bug vanished. The system ran flawlessly for days. But the moment I removed the logging overhead to return to our baseline ~24ns target—boom. 14 hours later, the state mismatch returned.

The measurement tool itself was fixing the system by destroying its performance. We couldn't catch the bug because looking at it changed its behavior.

Act 2: Staring Into the Abyss (The Root Cause)

I had to step away from the IDE and look at the silicon. The problem wasn't in our C++ logic; the problem was the Linux OS Scheduler and NUMA (Non-Uniform Memory Access) architecture.

To achieve ultra-precise sorting of incoming MFT events without the overhead of heavy system calls, our SDK utilized the RDTSC assembly instruction (Read Time-Stamp Counter) to timestamp packets the moment they hit the buffer.

Here is what was actually happening:
Once every 14 hours or so, the Linux kernel scheduler would decide to "balance the load" and migrate our main worker thread to a different CPU core located on a different physical socket.

Hardware clocks across different sockets are theoretically synchronized, but in reality, there is a microscopic drift. In our case, the drift was exactly 4 nanoseconds.

Under normal circumstances, nobody cares about 4 nanoseconds. But at 41.5M TPS, an event happens every ~24 nanoseconds. A 4ns drift is a massive 16% margin of error. When the thread jumped to the new socket, the hardware clock shifted by 4ns, causing two network events that arrived simultaneously to be timestamped and processed in the wrong order.

Node A processed Event 1 then Event 2. Node B (due to the thread migration) processed Event 2 then Event 1.
The topology broke. Determinism died. The system crashed.

Act 3: The TolmachЁv Solution

The standard fix for this is adding mutexes or utilizing heavier, safer clocks like CLOCK_MONOTONIC. But in MFT, locks are a death sentence for performance.

Instead of fighting the OS, we bypassed it entirely in v36.0.0:

  1. CPU Isolation: We modified the kernel boot parameters using isolcpus to completely hide specific CPU cores from the Linux scheduler.
  2. Strict Thread Affinity: We implemented pthread_setaffinity_np() to pin our worker threads directly to those isolated cores. The OS was now physically forbidden from migrating our execution context.
  3. Topological Tick Counter: We stripped out the raw RDTSC instruction for event sorting. Instead of relying on hardware wall-clocks, we implemented a purely topological state-machine counter. Events are now ordered strictly by network topology sequences, completely decoupled from the CPU's internal clock drift.

The Takeaway

The fix worked. Zero CPU validation waste was preserved. The 41.5M TPS benchmark was locked in, and the 14-hour crash disappeared forever.

When you are engineering at the absolute edge of hardware capabilities, the operating system is no longer your friend—it is an unpredictable abstraction layer that will quietly destroy your determinism. Trust your logic, but never trust the scheduler.

Top comments (0)