KatInterview

Design a Slot-Filling Ad Service with Frequency Caps

System DesignModel answer
MediumModel answerSystem Designapi-designconcurrencyidempotency

Last updated

Design a production HTTP API that assigns ads to page slots while enforcing per-user display limits correctly under concurrency and retries.

Design a Slot-Filling Ad Service with Frequency Caps

An upstream targeting system has already ranked and filtered a set of candidate ads for a given user. Your job is to build the service that takes those candidates and maps them onto the available page slots — top, sidebar, bottom, and so on — while respecting each ad's configured per-user display limit (a frequency cap). The assignment must be random among eligible ads, no ad may appear in more than one slot per response, and every successful response must increment the user's display counts so future calls respect the cap.

You are not being asked to write the assignment algorithm. You are being asked to design the API and the surrounding system that makes it production-ready.

Address each of the following:

  1. Protocol and endpoint shape — REST, gRPC, or something else? Why? How many endpoints, and what do the URLs look like?
  2. HTTP method choice — Which method drives an assignment, and what is your reasoning?
  3. Request and response schemas — What fields does each carry? How do you represent a slot that could not be filled?
  4. Per-user display-count storage — Where does this state live? What data structure and key scheme do you use? Does the store need a TTL?
  5. Concurrency correctness — A user can have several in-flight requests at the same time (multiple tabs, fast reloads). How do you prevent cap violations?
  6. Safe retries — Assignment mutates state. How do you let clients retry without double-counting an impression?
  7. Error taxonomy — What errors does the API surface, and how do clients distinguish a transport failure from a business-level outcome like "no eligible ad"?
  8. Reliability and observability — What does the service need to be trustworthy in production?

Constraints and assumptions

Treat these as defaults; flag anything you'd confirm with the interviewer:

  • Candidate ads and their cap values are supplied in the request body by the caller. Your service does not fetch or rank them.
  • Exceeding a frequency cap is a correctness bug, not a soft quality issue — billing and fraud systems depend on it.
  • A single user may have multiple simultaneous in-flight requests at any moment.
  • The service sits on a high-QPS internal ad-serving path; p99 latency on the state store matters.
  • Both web frontends and backend services call this API.

Good clarifying questions to raise

  • Over what window does the display limit apply — session, calendar day, lifetime? Does the answer imply a TTL on stored counts?
  • If no eligible ad exists for a slot, should that slot be left empty or should the entire request fail?
  • Who calls this API — trusted internal services, or untrusted external clients? That determines whether it is safe to accept cap values from the request body.
  • Do downstream systems (billing, analytics) need a durable record of every impression, or is the in-memory count enough?
  • What are the QPS and latency targets?

Hints

Hint 1

Start by classifying the core operation: does it read state, write state, or both? That classification drives your HTTP method choice, your idempotency story, and your storage strategy all at once.

Hint 2

Think carefully about what 'concurrent requests for the same user' actually means for a counter — a read followed by a separate write is not atomic. What primitives does your chosen store expose that let you collapse those two steps?

Hint 3

An idempotency key solves the retry problem, but where you store that key and for how long has its own trade-offs. Think through what happens if the service crashes after writing state but before sending the response.