KatInterview

Real-Time Impression Frequency Control System

System DesignModel answer
HardModel answerSystem Designdistributed-systemsreal-timeapi-design

Last updated

Build a low-latency distributed system that enforces rolling-window impression limits across multiple dimensions and time horizons for an advertising platform.

You're building a real-time impression control system for an advertising platform. When the serving layer decides which advertisement to display, your system must enforce rules that prevent users from seeing the same content too frequently.

The Problem

Each advertisement can have multiple independent frequency rules. For instance:

  • A user should see campaign X no more than 3 times per hour
  • The same user should see campaign X no more than 8 times per day
  • The same user should see campaign X no more than 15 times per week

These are rolling time windows measured backward from the current moment, not calendar boundaries. When a serving request arrives, it must satisfy every applicable rule to proceed.

What You Must Build

Your system needs at least two operations:

  1. Reserve — Answer whether showing this ad to this user right now would violate any rule, and if not, hold a slot
  2. Confirm — Record that the impression actually occurred after the ad was rendered

Design Expectations

Address each of these areas:

  • How you model the rolling window problem — what data structure answers "how many times in the last N hours" efficiently
  • Storage technology — what backs your hot path and why that choice fits your latency budget
  • Request flows — how reserve and confirm move through your system, including error cases
  • Handling duplicates and races — multiple concurrent reserve calls, retried confirm events
  • Data lifecycle — how old counts expire, how reservations timeout
  • Partitioning approach — how you distribute load while keeping related data together
  • Observability — metrics that tell you both whether the system is healthy and whether it's enforcing rules correctly

Questions You Should Ask

Before designing, clarify:

  • What happens if we occasionally let one extra impression through — is perfect accuracy required or is bounded error acceptable?
  • How precise must the time windows be? Can a 24-hour window have a few minutes of slop?
  • What user identifier do we receive? Stable logged-in ID, device fingerprint, cookie?
  • If our system is down, should we block all ads (safe) or allow them through (available)?
  • Is there a separate authoritative log of impressions we can reconcile against?

Scale Parameters

Use these numbers to ground your design:

  • Request rate: ~2 million reserve calls per second at peak
  • Latency target: p99 under 2 milliseconds for the reserve operation
  • User scale: hundreds of millions of active users
  • Campaign scale: thousands of active campaigns at any moment
  • Event ordering: confirm events may arrive late or out of order

Hints

Hint 1

Think about what state actually needs to exist — you don't need every impression stored forever, just enough to answer the window queries

Hint 2

The consistency choice drives your storage and sharding decisions — strict correctness and best-effort availability have very different architectures

Hint 3

Reservations that never get confirmed need to expire, otherwise they permanently consume quota