I started with Redis and sliding window counters which felt right, but then the interviewer pushed on what happens across datacenters and I kind of stumbled.
Start by clarifying requirements: scale (QPS, number of users/ads), cap granularity (per ad, per campaign), time windows (hourly, daily, lifetime), and latency budget. Then design a two-part system: a low-latency read path for cap checks and a reliable write path for impression recording, using a fast store like Redis with appropriate data structures and an async pipeline to a durable store. Discuss consistency trade-offs (e.g., eventual vs. strong consistency) and how to handle failures and scale.
Pro tip: Emphasize that frequency capping is a best-effort system: slight over-serving due to eventual consistency is often acceptable, but under-serving (blocking ads incorrectly) can hurt revenue. Propose a design that favors availability and low latency, with idempotent impression recording and reconciliation to correct counts.
Ask about QPS, number of active users and ads, cap definitions (per ad vs. per campaign), time windows, and latency SLA. Determine acceptable consistency (e.g., can we tolerate a few extra impressions?).
Outline a low-latency read path: on ad request, fetch current counts for the user-ad/campaign from a fast store (e.g., Redis) and compare against caps. Use caching and local aggregation to reduce load.
After an ad is served, record the impression asynchronously via a message queue (e.g., Kafka) to decouple from the serving path. Ensure idempotency and at-least-once processing, with deduplication.
Use Redis with counters and TTLs for real-time counts (e.g., key per user-ad-window). Persist raw impressions in a durable store (e.g., Cassandra, Bigtable) for analytics and reconciliation. Consider time-bucketed keys for efficient windowing.
Discuss eventual consistency: counts may lag, leading to slight over-serving. Mitigate with short TTLs, periodic reconciliation from durable store, and sharding by user ID. Handle failures with retries and fallbacks.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.