KatInterview

Designing a Production Stall Detector for Streaming Jobs

System DesignModel answer
HardModel answerSystem Designsystem-designstreamingtimers

Last updated

Design a distributed service that watches start, heartbeat and end messages per job and emits exactly one stall signal when a deadline passes, covering time semantics, sharding, recovery and testing.

A prototype service tracks long-running units of work (for example batch jobs, uploads, or sensor streams). Each unit is identified by an event_id and sends three kinds of messages: start, ping (a liveness update), and end. If no qualifying message arrives within the configured timeout after the last one, the service must report that unit as timed out, once.

The prototype is a single process holding a dictionary and a heap. Your task is to turn it into a production-ready, distributed service. In your answer, cover:

  1. Requirements and scale. State your assumptions: message rate, number of concurrently active events, range of timeout values, accuracy of firing, and delivery guarantees.
  2. API and data model. Define the ingest interface, message schema, per-event state record, and idempotency keys for both input and output.
  3. Time semantics. Compare event time with processing time, explain how you would compute deadlines, and describe how you deal with clock skew between producers and the detector.
  4. Architecture. Explain how you shard by event_id, where state lives, what consistency reads offer, and how a failed worker is recovered.
  5. Performance. Choose a timer structure (timer wheel versus heap, and where LRU does or does not fit), and discuss batching, backpressure, memory limits, and very long timeouts.
  6. Edge cases. Late, dropped, duplicated, and reordered messages; very large timeouts; long silences; restarts.
  7. Operations. How you would test, monitor, alert, and roll the service out.

Be explicit about trade-offs and about the alternatives you rejected.

Hints

Hint 1

Start by writing down numbers (messages per second, active keys, timeout range, allowed firing delay). They drive every later choice, such as in-memory versus external state.

Hint 2

Think about what goes wrong with a pure event-time clock during a long silence, and with a pure processing-time clock after a consumer backlog. A hybrid with a lag guard is the usual answer.

Hint 3

Pings reset timers constantly. Ask which timer structure makes a reset cheap, and whether you can store a deadline in the timer and validate it against durable state when it fires.

Hint 4

For correctness under failure, combine single-writer ownership per partition, epoch fencing, and a deterministic output key such as (event_id, deadline) so duplicate firings are harmless.