Skip to content
LeetQuant
QD10 System Design and Code Review

QD10 · System Design and Code Review · Lesson 3 of 5

Design Trade-offs

Latency versus throughput (batching and queueing), consistency versus availability, build versus buy and simplicity versus flexibility, compared side by side for a research data platform and a low-latency trading path.

17 min

Every design choice buys something with something

Senior interviewers often ask a follow-up of the form "why not do X instead?". The strong answer names the trade-off, says which requirement decided it, and says when the decision would flip. This lesson covers the four trade-offs that come up most and then applies them to two systems that resolve them in opposite directions.

Latency versus throughput

Batching amortises a fixed per-operation cost (a system call, a network round trip, a disk flush) over many items, which raises throughput but makes each item wait for its batch.

Suppose a write costs 50 μs50\ \mu s fixed plus 1 μs1\ \mu s per message. A batch of bb messages takes 50+b μs50 + b\ \mu s:

Batch size bbTime per batchThroughput
151 µsabout 19,600 msg/s
1060 µsabout 167,000 msg/s
100150 µsabout 667,000 msg/s
1,0001,050 µsabout 952,000 msg/s

Throughput rises almost 50× from b=1b = 1 to b=1,000b = 1{,}000, but the last message in a batch of 1,000 waits at least 1 ms, plus however long it takes the batch to fill. A research ingest pipeline wants the bottom row; an order gateway wants the top one.

Queueing is the other half. As utilisation ρ=λ/μ\rho = \lambda / \mu approaches 1, waiting time explodes. For the simplest single-server queue with random (Poisson) arrivals and exponential service times, the mean time in the system is

W=1μ−λ.W = \frac{1}{\mu - \lambda}.

With service rate μ=100,000\mu = 100{,}000 per second (10 µs per item): at λ=50,000\lambda = 50{,}000 (ρ=0.5\rho = 0.5) W=20 μsW = 20\ \mu s; at ρ=0.9\rho = 0.9, W=100 μsW = 100\ \mu s; at ρ=0.99\rho = 0.99, W=1,000 μsW = 1{,}000\ \mu s. The real distribution differs, but the shape is general: a system run near full utilisation has high and unstable latency. Latency-sensitive paths are deliberately run at low utilisation, which is a throughput cost paid for latency.

Consistency versus availability

When the network partitions, a replicated system must either refuse some requests (stay consistent) or answer them with possibly stale or conflicting data (stay available). That is the CAP theorem's real content. The PACELC refinement adds the everyday case: even without a partition, stronger consistency costs latency, because a write must reach more replicas before it is acknowledged.

Decide per piece of data, not per system:

  • Positions, risk limits, order state: must be consistent. Serving a stale position can breach a limit or double-send an order. Prefer refusing to trade over trading on wrong state.
  • Reference data, dashboards, research results: availability wins; seconds or minutes of staleness are harmless if labelled.

Build versus buy

Buy (or adopt open source) when the component is not a source of edge and a mature product meets the requirements: databases, message brokers, schedulers, monitoring. Build when the component is the edge or the requirements are outside what products offer: a feed handler tuned to one exchange's protocol, an order book that must run in a few hundred nanoseconds, a backtester that models your own execution.

Count the full cost of building: the first version is the cheapest part; maintenance, on-call, documentation and the engineer who leaves are the rest. Count the full cost of buying: licences, lock-in, upgrade cycles and the latency or flexibility you cannot get.

Simplicity versus flexibility

A plugin framework, generic configuration and abstraction layers make future changes cheap if you guessed the future correctly; otherwise they are cost with no benefit and more places for bugs. Prefer the simplest design that meets today's stated requirements plus the one extension you can name concretely. In a hot path, every indirection (virtual call, dynamic lookup, extra copy) also costs latency.

Worked example

two systems, opposite answers

Compare a research data platform (dozens of researchers run backtests over years of tick and bar data) with a low-latency trading path (market data in, decision, order out, target under 10 µs inside the box). For each trade-off, say which way each system goes and why.

Show solution
DimensionResearch data platformLow-latency trading path
Primary metricThroughput: rows scanned per second, jobs per hourTail latency: p99 and p99.9 tick-to-order
BatchingLarge batches, columnar scans, vectorised computeNo batching on the hot path; process each message as it arrives
UtilisationRun hot: idle cluster time is wasteRun cold: cores pinned and mostly idle so a burst never queues
ScalingHorizontal: more workers, partitioned dataVertical: one process, one machine, sharded by instrument only if needed
ConsistencySnapshot reads of immutable, versioned datasets; corrections create a new versionStrong for orders, positions and limits; one owner per piece of state
AvailabilityHours of downtime are an inconvenienceFailure must be safe: stop quoting and cancel orders rather than trade on bad state
Build vs buyBuy or adopt: object storage, columnar formats, a query engine, a schedulerBuild: feed handlers, order book, risk checks, order gateway
FlexibilityHigh: new datasets and research questions every weekLow: a narrow, fixed set of messages and actions, kept simple to reason about
Correctness riskLook-ahead bias, survivorship bias, silent data errorsStale state, races, unbounded queues, wrong order sizes

Reasoning to say out loud. The research platform is judged by how many correct experiments it lets people run, so it optimises throughput, reproducibility (immutable versioned data) and flexibility, and buys standard parts. The trading path is judged by tail latency and by never doing something wrong, so it gives up throughput and flexibility, keeps state in one place, and fails closed.

Where they meet. The trading path's recorded market data and order logs feed the research platform. That interface should be an append-only log written asynchronously off the hot path, so research load can never slow trading, and stamped with the same timestamps and identifiers so research results reconcile with production.

Knowledge check

A single-server queue has service rate 50,000 per second. Using W=1/(μ−λ)W = 1/(\mu - \lambda), what is the mean time in the system when arrivals are 45,000 per second?

Knowledge check

During a network partition, the risk service cannot reach the replica that holds the current position for a symbol. Which behaviour is right for an automated trading system?

Knowledge check

Writes cost 50 µs fixed plus 1 µs per message. Which batch size first reaches at least 500,000 messages per second?

Key takeaways

  • Batching trades per-item latency for throughput; queueing near full utilisation makes latency large and unstable.
  • Under partition, choose consistency or availability per piece of data: orders, positions and limits fail closed; reference and research data stay available.
  • Build what is your edge or outside what products offer; buy the rest, counting the full cost of each.
  • Prefer the simplest design that meets stated requirements; every abstraction costs maintenance and, on a hot path, latency.
  • A research platform and a trading path resolve every trade-off in opposite directions; connect them with an asynchronous, append-only interface.

Finished reading?

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