KatInterview

Hierarchical Sliding-Window Throttle for Outbound Alerts

System DesignModel answer
MediumModel answerSystem Designrate-limitingdistributed-systemssliding-window

Last updated

Design a distributed throttle that accepts or rejects alerts using exact 10-minute rolling limits for a user, their team and their company, atomically and correctly across many stateless servers.

Hierarchical Sliding-Window Throttle for Outbound Alerts

You are building the gatekeeper for a messaging platform that pushes alerts to end users. Before any alert goes out, a service must decide whether the send is allowed.

The decision API is:

allow_alert(user_id, timestamp) -> bool

Each user belongs to exactly one team, and each team belongs to exactly one company, so a fixed user -> team -> company lookup exists.

An alert is allowed only if, at the moment of the request, all three of these hold over an exact rolling 10-minute window:

ScopeMax accepted alerts per 10 minutes
User3
Team10
Company20

Rules:

  • If allowing the alert would break any of the three limits, it is denied and no counter changes at any level.
  • If it is allowed, it counts against all three scopes.
  • The window is a true sliding window: an accepted event stops counting exactly 10 minutes after it occurred. This is not a fixed or tumbling bucket.
  • The answer must stay correct when many stateless application servers call the limiter concurrently.

Assumptions (confirm them in your answer)

  • Roughly 10^7 users, up to about 10^6 decisions per second at peak, p99 decision latency in single-digit milliseconds.
  • The hierarchy is a strict tree. Mappings change rarely (reorgs, transfers) but they do change.
  • The limits are small (3 / 10 / 20). You are expected to take advantage of that.

What to deliver

  1. Core algorithm. Explain the decision logic and why it is exact. Walk through a concrete example that includes (a) an event landing exactly at the 10-minute boundary and (b) a case where the team or company limit rejects a request even though the user is under their own limit.
  2. Distributed design. Cover the API (including idempotency), storage choice and data model, how the three-scope check-and-update is made atomic, sharding and horizontal scaling, handling of user -> team -> company lookups and their changes, and operational concerns (clock skew, hot keys, failure behavior, observability).
  3. Trade-offs. Name the main ones explicitly and say which side you pick and why.

Also list the clarifying questions you would ask before committing to a design.

Hints

Hint 1

Because the limits are tiny (3, 10, 20), you can afford to store the actual timestamps of accepted events per scope rather than approximating with counters. Memory per key is bounded by the limit.

Hint 2

A rejected request must change nothing, so the check across all three scopes must happen before any write, and the check plus the writes must be one indivisible step. Think about what single-node primitive gives you that (a server-side script or a transaction on one shard).

Hint 3

To make a multi-key atomic operation possible, the user, team and company keys have to live on the same shard. Ask what that does to load for a large company, and what you would do if one company becomes a hotspot.

Hint 4

Think about what can go wrong beyond the happy path: retried requests double-counting, caller-supplied versus server-assigned timestamps, a user moving teams mid-window, and the store being unreachable.