KatInterview

Real-time Market Data Dispatch System

System DesignModel answer
MediumModel answerSystem Designconcurrencydata-structureslow-latency

Last updated

Design an in-memory event matching engine that routes incoming market events to registered subscribers based on filtering criteria.

You need to architect the core logic for a real-time market data dispatch system that runs entirely in memory. The system receives a continuous stream of market events and must deliver each event to all interested subscribers based on their registered filters.

Here is a simplified interface in C++:

using HANDLE = int;

struct MarketEvent {
  std::string ticker;      // security identifier, e.g. "GOOGL"
  int volume;              // transaction volume
  std::string venue;       // exchange or venue code
};

class EventDispatcher {
public:
  EventDispatcher() {}

  // Invoked when a market event arrives from the data feed
  void processEvent(const MarketEvent &event);

  // Register a subscription with a filter pattern and callback
  // Returns a handle for later cancellation
  HANDLE registerSubscription(const MarketEvent &pattern,
                               std::function<void(const MarketEvent &)> handler);

  // Remove an active subscription
  void cancelSubscription(HANDLE handle);

private:
  // Your internal design goes here
};

Matching semantics:

  • A pattern acts as a template with optional wildcards
  • An incoming event matches a pattern when:
    • ticker fields are identical (exact string match)
    • volume == 0 in the pattern means "any volume" (wildcard)
    • venue == "" in the pattern means "any venue" (wildcard)
    • Non-wildcard fields must match exactly
  • When an event matches multiple subscriptions, invoke all corresponding handlers

Functional requirements:

  1. registerSubscription(pattern, handler) — record a new subscription and return a unique handle
  2. processEvent(event) — find all matching subscriptions and execute their handlers
  3. cancelSubscription(handle) — deactivate a subscription so its handler stops receiving events

Constraints and performance targets:

  • Up to 100,000 active subscriptions at any time
  • 10,000+ events per second arrival rate
  • processEvent must be efficient: avoid scanning all subscriptions
  • Multiple threads may call these methods concurrently
  • Memory usage should be reasonable for the scale

Your task:

Design the internal architecture of EventDispatcher:

  1. What data structures will you use to store subscriptions and enable fast lookups?
  2. How will each of the three operations work with those structures?
  3. Analyze time and space complexity
  4. Explain your concurrency strategy: how will you handle simultaneous reads (event processing) and writes (subscription changes) while maintaining correctness and high throughput?

Hints

Hint 1

An event can only match a small number of filter patterns — enumerate them explicitly

Hint 2

Readers far outnumber writers in this workload

Hint 3

After cancellation completes, can an old handler still fire if it's already running on another thread?