Chapter Twenty-One · Problem Fifteen
Ad-Click Aggregation
Count ten billion ad clicks a day and bill on the count. A chart can be fast and still moving; an invoice cannot, so one record has to produce two numbers that stay honest with each other.
"Design a system that counts ad clicks in real time, so advertisers see per-minute totals and the most-clicked ads, and are billed on them."
This count ends up on someone's invoice, and that reorders the problem. A hundred thousand clicks a second is real work, and it is the easier half. The harder half is the advertiser who asks why Tuesday's total changed, or whether one click was charged twice. Here correctness outranks the write path, and the design needs an answer to that advertiser before it needs a log.
The tools are ones you already hold. Chapter 6's Delivery Semantics at Scale supplies the click ID this chapter is built on, and its Queues, Logs and Pub-Sub supplies the log and its replay. Chapter 5's Clocks and Ordering hands over the time a click happened as the thing to count by, and its IDs Without Coordination covers an ID made where the event is born. Chapter 3 gives the partition key and the write-hot key, in Picking the Partition Key and Hot Partitions. Chapter 4's Consistency Models That Come Up gives the habit of stating how far behind a dashboard may run.
This one is pitched from senior to staff, and three things carry it. Windows over a stream: a click belongs to the minute it happened in and may arrive long after, so a minute needs a rule for when it stops moving. Counting each click once for billing, which takes duplicates caught by ID, a store that a repeated write cannot harm, and a batch recount that the invoice reads, checked against the stream. And the top list at scale, where merging the lists of many partitions is exact under one condition, and approximate counting has to earn its place.
Before the first lesson, give yourself the full 45 minutes and a blank page. At the end your sketch has one question to answer: a phone resends a click, and the counter crashes during the same minute, so what does the invoice say? The four lessons run on that clock: ten minutes for Scope, ten for First Design, ten for Scaling It, and the final fifteen for deep dives and curveballs.