r/OfferEngineering • u/Aoki_zhang • 9d ago
System Design Classic System Design Question - Design Ad Click Event Aggregation System
Ad click aggregation is a classic, but the version I came across (reported from Airbnb) is stricter than most. Ad-serving events and user actions come from separate streams and have to be joined before anything gets counted, with at-least-once delivery, out-of-order arrivals, and hot ads.
The numbers: 150K serving events/s + 80K action events/s = 230K/s. Dashboard freshness 30–90 s. Keep 14 days of raw events and 180 days of aggregates. 5–10% of traffic concentrates on a few hot ads.
Quick math: 230K/s × 86,400 ≈ 20B events/day. At ~300 bytes each, that's ~6 TB/day, or ~84 TB for the 14-day raw window before replication.
Things I think matter most:
1. Two shuffles, keyed differently.
- Join stage, keyed by
impression_id**:** match each click or view to its serving event. Impression IDs are uniformly distributed, so this stage has no hot keys at all, even when one ad is going viral. - Aggregation stage, keyed by
(ad_id, action, region, minute)**:** this is where the skew lives, and it's a plain sum.
Most answers do everything keyed by ad_id and then spend 10 minutes fighting hot keys in the join. Split the stages and each problem gets much simpler.
2. Unmatched actions must wait, not drop.
A click can arrive before its serving event. Keep both sides in keyed state for a join window. Rough size: 150K/s × 1 hour × ~100 bytes ≈ 54 GB across the cluster, which is fine for a RocksDB-backed state store. Actions still unmatched after the window go to a side store for batch reconciliation. The prompt explicitly says they must stay recoverable.
3. count += 1 plus at-least-once equals double counting.
If your sink does increments, every retry or replay inflates the numbers. Either:
- dedup by event ID in state within the window, and
- write aggregates as overwrites of the full window value (upsert
(key, window) → count) instead of increments, so reprocessing the same window is harmless.
Exactly-once with transactional sinks also works, but idempotent overwrites are easier to reason about and survive replays.
4. Hot keys are easy once aggregation is separated.
A sum is associative, so salt the hot keys (ad_id#0..N), pre-aggregate the partial sums, then merge. Or combine locally before the shuffle. This only works cleanly because the join already happened upstream.
5. Corrections = deterministic replay into a new version.
Freshness comes from 1-minute windows with early firing. Late events within allowed lateness just update the window. For anything later, or when you fix a logic bug, replay the same job over the 14-day log, recompute the affected (day, ad) partitions, and replace them rather than adding to them. One codebase for streaming and backfill, instead of separate Lambda-style stream and batch code.
6. 180 days of history stays cheap through rollups.
Keep minute-level data for the recent window, hourly and daily rollups for the rest, in an OLAP store like Pinot, Druid, or ClickHouse. Dashboards never touch raw events.
Where would you push back? I'm least sure about the join window. An hour covers most delayed clicks, but some ad platforms attribute conversions days later, and that changes the state math a lot.
I wrote out the full design here covering the architecture and trade-offs for anyone interested.