KatInterview

Design a Real-Time Streaming VWAP Computation Service

System DesignModel answer
HardModel answerSystem Designstreamingfinancial-datalow-latency

Last updated

Design a sub-5ms latency pipeline that ingests multi-exchange trade ticks, maintains running VWAP aggregates with correction support, and fans out to tens of thousands of subscribers.

Background

A trading analytics platform needs to compute Volume-Weighted Average Price (VWAP) continuously across every actively traded equity symbol, sourcing raw trade data from dozens of global exchanges simultaneously.

VWAP is defined as:

VWAP = Σ(Pᵢ × Vᵢ) / Σ(Vᵢ)

where Pᵢ and Vᵢ are the price and volume of each individual trade.

The service must compute two flavors of VWAP in parallel:

  • Session VWAP — accumulates from the opening print of each trading day through the current moment, resetting at each market open.
  • Rolling-window VWAP — covers a sliding lookback period (for example, the trailing 5 minutes or trailing 1 hour), using the exchange's own execution timestamp as the event clock.

Downstream clients range from latency-sensitive algorithmic trading desks that need raw, unconflated streams to external portals that need throttled WebSocket feeds and on-demand historical lookups.

Requirements

Functional

  • Ingestion and normalization: Accept raw trade data from 50+ exchanges using a variety of wire protocols (binary multicast, FIX session, proprietary ITCH-style feeds). Normalize every incoming trade into a single internal canonical format before processing.
  • Continuous aggregation: Maintain and publish session VWAP and rolling-window VWAP per symbol, updating with each new eligible trade.
  • Trade corrections: Exchanges periodically issue cancellations ("busts") and amended prints. The running aggregates must adjust immediately and accurately when corrections arrive.
  • Delivery and querying: Stream live VWAP updates to subscribers and expose an API for point-in-time lookups and end-of-day figures.

Non-Functional

  • Latency: p99 end-to-end time from tick arrival to published update must stay under 5 milliseconds.
  • Recovery: If an aggregation node fails, the service must restore correct state within 5 seconds — without replaying an entire day's worth of ticks from scratch.
  • Availability: 99.99% uptime during active market hours.

Scale

DimensionFigure
Active symbols20,000
Sustained tick rate100,000 ticks/second
Peak tick rate1,000,000 ticks/second
Concurrent downstream sessions50,000
Raw tick retention7 years
VWAP snapshot retention90 days

What to Cover

  1. Overall architecture — how components from feed handler through fan-out tier connect and interact.
  2. State representation — what data structure holds running cumulative sums, and how rolling windows are maintained efficiently.
  3. Correction handling — how a bust or amended trade propagates through session and rolling accumulators without reprocessing history.
  4. Pub/Sub and API design — protocols, conflation strategy, and the query interface for historical access.
  5. Key trade-offs — highlight at least two design decisions where you chose one approach over a reasonable alternative and explain why.

Hints

Hint 1

Start by sizing the problem: compute canonical tick size, daily byte volume, and session-state footprint. The 5ms p99 constraint rules out certain off-the-shelf components on the hot path — figure out which ones and why before picking your transport layer.

Hint 2

VWAP accumulators are additive: the only state you need per symbol is cumulative notional (Σ P×V) and cumulative volume (ΣV). Think about what data structure makes rolling windows O(1) amortized without storing every individual tick, and what data type prevents floating-point drift when busts subtract back out.

Hint 3

A bust is just a subtraction — you need to look up the original trade's price and quantity in O(1) to reverse it. Think about how you keep that lookup table, what key you use, and how it fits into your snapshot/recovery strategy.