Skip to content
LeetQuant
QD05 Operating Systems and Computer Architecture

QD05 · Operating Systems and Computer Architecture · Lesson 1 of 6

Systems & Low-Latency Basics

Memory hierarchy, cache-friendly data layout, lock-free queues and measuring latency: what trading-systems interviews probe.

25 min

Know your latencies

Rough orders of magnitude every quant developer should know:

OperationTime
L1 cache reference~1 ns
L2 cache reference~4 ns
Branch mispredict~5 ns
Main memory reference~100 ns
Kernel-bypass network hop, same rack~1–5 µs
Round trip within a data centre (kernel network stack)~100–500 µs
Disk seek (spinning)~10 ms

A trading system's hot path (market data in, decision, order out) often has a budget of a few microseconds, so a single cache miss or memory allocation is visible. In interviews, reasoning from these numbers is more valuable than reciting them precisely.

Data layout and the cache

Memory moves in cache lines of 64 bytes. Code that walks memory sequentially uses every byte it fetches and lets the hardware prefetcher run ahead.

  • A contiguous array (std::vector) beats a linked list (std::list) for iteration by a large factor, even when big-O says they are equal, because each list node is a likely cache miss.
  • Structure of arrays (one array per field) beats an array of structures when a loop reads only a few fields.
  • Avoid allocation on the hot path: pre-allocate, use object pools or arenas, and reuse buffers.
  • Predictable branches are cheap; unpredictable ones cost a pipeline flush. Branch-free code helps in tight loops.

Concurrency without locks

A mutex can block a thread for microseconds. Trading systems usually pass messages between threads through single-producer, single-consumer (SPSC) ring buffers:

  • A fixed array whose size is a power of two, so the index wraps with a bit mask rather than a division.
  • The producer writes the slot, then publishes by advancing the write index with a release store; the consumer reads the index with an acquire load. No locks are needed.
  • Put the producer's and consumer's indices on separate cache lines. Otherwise each write invalidates the other core's copy of the line, called false sharing, which can cost more than a lock.

Pin hot threads to dedicated cores and keep them busy-polling, rather than sleeping and waking.

Measure the tail

Average latency hides what matters. Report percentiles: median, 99th, 99.9th and the maximum. A system with a 2 µs median and a 200 µs 99.9th percentile loses races when markets are busiest, which is exactly when it matters. Measure with timestamps taken as close to the hardware as possible, and beware of measurement code that itself allocates or takes locks.

Common C++ follow-ups: the cost of virtual calls (an indirect branch, and a lost inlining opportunity), move semantics, RAII, templates versus runtime polymorphism, and why exceptions are kept off the hot path. In Python roles, expect questions on vectorising with NumPy and the global interpreter lock.

Worked example

estimating a loop

You sum one field from 10 million 64-byte records. Estimate the time if the records are stored contiguously, versus as nodes of a linked list scattered through memory.

Show solution

Contiguous: the loop streams 640 MB from memory. At roughly 10–20 GB/s of memory bandwidth, that is about 30–60 ms, with prefetching hiding most of the latency.

Scattered list: each node is a likely cache miss at about 100 ns, and the next address is not known until the current node arrives, so the misses cannot overlap. 107×10010^7 \times 100 ns =1= 1 second. Same big-O, roughly 20 times slower. (With a structure of arrays reading only the needed 8-byte field, the contiguous version would read just 80 MB and be faster still.)

Knowledge check

Two threads update two different counters that sit next to each other in memory, and performance is terrible. What is the most likely cause?

Knowledge check

Which statistic best describes whether a trading system is fast enough when markets are busy?

Knowledge check

Why are ring-buffer sizes usually powers of two?

Practice problems

Finished reading?

Answer the 3 knowledge checks to complete this lesson (0 done).