KatInterview

Profiling CPU, Cache, and Disk Stalls in a Low-Latency Engine

System DesignModel answer
MediumModel answerSystem Designperformanceprofilinglow-latency

Last updated

Build a measurement-first diagnosis and fix plan for three latency problems in a C++ trading service: CPU saturation, cache-hostile memory use, and slow synchronous log writes.

Context

You own a C++ service that ingests a high-rate stream of market updates and sends order acknowledgments. Under replayed peak-load tests, tail latency spikes in three separate setups. Each has a different resource as the suspect.

  1. Compute-limited run: The service falls behind during bursts, and the input queue grows without limit. Disk and network utilization are both low.
  2. Memory-limited run: After hours of running, throughput collapses while the process maintains a large in-memory limit order book. Total memory use is still well below physical RAM.
  3. Storage-limited run: During volatile periods, order acknowledgment latency rises roughly tenfold while the service writes audit records.

Task

For each of the three setups, answer the following:

  1. Measurement: Which OS tools and hardware or kernel counters would you use to confirm where the time is going? Be specific about what each reading would indicate.
  2. Likely cause: Propose a concrete design or code-level flaw that explains the symptoms.
  3. Fix: Describe the architectural, algorithmic, or systems-level change you would make.
  4. Validation: How would you prove the bottleneck is gone without breaking correctness, such as durability, thread safety, or deterministic order handling?

A strong answer is structured, ties each conclusion to a measurement, discusses trade-offs, and avoids jumping to fixes before there is evidence.

Hints

Hint 1

Start from the symptom and split it. High CPU could be user code, kernel time, or cores that are stalled waiting on memory. Look at IPC and the user/system split before choosing a fix.

Hint 2

For the memory case, total usage is fine, so think about locality: pointer-heavy containers, scattered allocations, cache and TLB misses, and page-level behavior that degrades over a long session.

Hint 3

For the I/O case, ask what the order thread waits on before it can send an ack. Then think about how to take the storage device off that path while keeping a clear durability guarantee.

Hint 4

For validation, think about replaying captured traffic through old and new builds and comparing outputs, plus checking invariants and running long soak tests.