Ad-Click Aggregation: Scaling It
Four pushes arrive between minute twenty and minute thirty, each leaning on a different limit you named. First comes the insert rate at ten billion a day. Then the hour of rows that every dashboard load reads again. Then a single click that turns up after its minute has gone out. Last, a crash in the counting, and what the totals look like when it comes back.
A Hundred Thousand a Second
"A hundred thousand inserts a second, twice that at peak. Where are they going?" Not into a table on the redirect's path. The click server appends each click to a partitioned log and answers with the redirect once the log has it. The counting moves to readers of that log, off the path the user is waiting on. It is a log and not a work queue because two readers need the same clicks and the counting must be able to rewind, the choice Chapter 6's Queues, Logs and Pub-Sub sets out.
The log is keyed by ad, as Chapter 3's Picking the Partition Key would have it, so one ad's clicks reach one reader, and that reader holds the ad's whole count. A consumer group gives each partition one reader, so the reader's rate sets the partition count. At the aggregator's half limit of 10,000 a second, the peak of 200,000 needs 20 partitions.
If the log cannot be reached, the server keeps the click on local disk for a while and sends the redirect anyway, because the redirect comes first. This is no habit of buffering. Normal traffic never waits there, so only an outage puts clicks at risk, and only on a server that dies during one. The clicks keep their stamps and go to the log when it returns.
A second reader copies every click to an object store as the raw record, 2 terabytes a day. That keeps the first design's promise that nothing is thrown away. Assume the log itself keeps a day, so a reader that stalls for hours loses nothing. Clicks the archive reader has not copied by the end of that day would be gone from every recount, so its lag pages someone long before then, in time to extend the log's retention.
What it costs: a log to run, and a small buffer on each click server that a crash can lose. A total is no longer available the moment its click is. And delivery is at-least-once, so duplicates are normal traffic from here on, the point of Chapter 6's Delivery Semantics at Scale. The key has a bill as well. One ad with a tenth of all clicks is 20,000 a second, a whole aggregator, and it comes back as a curveball.
The Query That Reads an Hour
"Every advertiser who opens the dashboard runs that query over the last hour. Add it up for me." An hour is 360 million clicks at the average. At 200 bytes, that is 72 gigabytes. An NVMe drive reads about 10 gigabytes a second, so one load takes about 7 seconds with nothing else running. Assume 10,000 advertisers refreshing once a minute. That is about 170 queries a second, and scanning is out.
So the counting happens ahead of the question. Each aggregator keeps a count per ad for each one-minute window and writes a total row when the minute ends, and the dashboard reads those rows. One ad's last hour is now at most 60 rows. The million rows from Scope land across the following minute, the room a first total's promise leaves, which is why the store sees about 17,000 writes a second and not a burst.
The store's copies may run a second or so behind, and you say that staleness out loud, the stated bound Chapter 4's Consistency Models That Come Up wants. If the store goes down, charts stop moving while aggregators hold their rows and retry. No click is lost and no redirect notices, because neither path runs through it.
What it costs: the question is fixed in advance, so a new breakdown needs new counting or a recount of the raw clicks. And the top 100 is still a sort over a million rows a minute, so it stays parked as the top list.
The Late Click
"This click shows up four minutes behind, and you already published its minute. What now?" First settle which minute it belongs to. Count by the click's own time and not by when it reached the aggregator, the handover Chapter 5's Clocks and Ordering makes by name. Counting by arrival lets every delay move clicks between minutes, so one bad link redraws the chart and nobody can say why.
Then settle when a minute stops taking clicks. Keep each one open past a watermark: the point before which the aggregator believes it has seen everything, here five minutes behind the newest click seen. The first total goes out when the minute ends, and each late click rewrites that same row. When the watermark passes the minute's end, the row is final. So a click four minutes late moves a published total, and that is the design.
Take the minute of 2:32. It ends at 2:33, and its first total goes out. At about 2:37 a click stamped 2:32 arrives, and the watermark has not yet reached 2:33, so the row is written again with one more click. Once the newest click is stamped 2:38, the watermark is at 2:33, and the row is final.
Rewrites are writes. Assume one click in a hundred lands after its first total. At peak, that is 2,000 rewrites a second, well inside the room that 34 partitions at half their limit keep. The final mark cannot be a write as well, because a second write on a million rows a minute would spend all of that room. So each aggregator writes one line a minute for its partition, final up to here, once a checkpoint covers that minute, and a read compares a row's minute with that line.
What it costs: open minutes in memory, six of them at a million rows of 100 bytes, 600 megabytes across the fleet, which decides nothing. A dashboard number that can move for those five minutes, shown as first until it stops. And the 0.1 percent past five minutes, 10 million clicks a day, which go to a side log, so that a recount, a batch count of the raw clicks that joins the board in the deep dive, can account for them.
One thing you leave open, and name so you can come back to it: "Each partition keeps its own watermark, and one of them can stop moving. Call that the stuck partition, and I'll take it in the deep dive."
The Thirty-Second Replay
"Your aggregator dies and starts again from thirty seconds back. What did that just do to the totals?" Size the damage first. Thirty seconds of the whole stream is 3 million clicks, and 6 million at peak. Added on top of totals already stored, that is half a minute counted twice.
So the aggregator saves its state. Every thirty seconds it checkpoints its counts, its set of seen click IDs and its log offset as one unit, in the object store. That keeps the read position with the result, the third way in Chapter 6's Delivery Semantics at Scale, so a restart resumes from a state where all three agree. And it writes totals to the store, its sink, by key, ad and minute, so a second write says what the first said. It never adds to a stored number, and the side log is keyed by click ID for the same reason.
A checkpoint writes what changed, not all it holds. At peak one aggregator takes 300,000 clicks in thirty seconds, about 5 megabytes of new IDs, and its share of the open minutes is at most 30 megabytes more. If the object store is down, checkpoints wait and a restart replays further back, within the day the log keeps.
When one dies, the consumer group sees its reader go silent and hands the partition to a spare, which loads the last checkpoint. A full day's IDs are 8 gigabytes, about 6 seconds over an assumed 10-gigabit link. Then the partition redoes 150,000 clicks on average, about 7.5 seconds at 20,000 a second, and twice that at peak. New clicks keep arriving meanwhile, so at peak the partition reaches the live edge in about half a minute. A restart of all 20 takes no longer, because each partition recovers on its own. The totals come out the same. While that runs, a row can step back to an earlier count and climb to the same number again.
That settles a replay, and a replay is only one kind of duplicate. A replay reads the same log record again, and the checkpoint and the write by key catch it. A resend comes from a phone that never heard its answer, and it arrives as a new log record with the same click ID, so no checkpoint can see it. The seen set catches that one. It lives inside the checkpoint with the counts and not in a cache beside them, so a crash cannot fall between the check and the count, the placement Chapter 6's Delivery Semantics at Scale insists on. Both copies carry the same ad, which sends them to the same partition and the same aggregator.
What it costs: 8 gigabytes of IDs per aggregator for a day's window, a checkpoint to write and restore, and a duplicate over a day late that only the recount catches.
When the words exactly once come up, say where the effect comes from: "Exactly once is an effect here: a checkpoint for replays, a click ID for resends, and a write by key so the sink doesn't care."
What the Board Shows Now
Click servers append to a log keyed by ad. Twenty aggregators read it, checkpoint every thirty seconds, and write a row per ad and minute to the store of minute totals, and dashboard servers read those rows. Clicks past the watermark go to a side log. The log also feeds the raw archive in the object store.
Nothing the first design relied on has vanished, though all of it moved. The clicks table became the log, which carries clicks for a day, and the archive, which keeps them. The unique key became the seen set. The query became the aggregators, and a question nobody asked in advance became a recount over the archive. The recount itself joins the board in the deep dive, because no push has forced it yet.
- Saying "Kafka gives exactly once" and stopping. The promise ends at the system's edge, as Chapter 6's Delivery Semantics at Scale puts it, and the totals' store is outside it.
- Incrementing a counter in the sink. The replay adds to it, and the design is back to 3 million clicks counted twice.
- Checking IDs in a cache beside the counts. The interviewer asks what happens if a crash falls between the check and the count.
- Using arrival time because it is simple. The first link failure redraws the chart with clicks in the wrong minutes.
- The candidate tells the two kinds of duplicate apart without being asked, and names what catches each.
- The candidate says a published total can move, and says for how long.
- The candidate gives the replay's size in clicks and the recovery in seconds.
Knowledge Check
Writing each total by its key, ad and minute, protects you against what, and leaves what open?
- It stops every duplicate, so the seen set is no longer needed
- It stops resends, but a replay after a crash still double-counts
- A replay writes the same total again, but a resend still adds one
- It stops lost clicks, but a click that arrives twice still counts
Keeping each minute open past the watermark takes 600 MB of memory. What does it really cost you?
- A number on the dashboard that may still move for five minutes
- Five more minutes before any first total can reach the chart
- A copy of every click, held in memory on all of the aggregators
- Every click written again to the store when its minute closes
A click arrives after the watermark has passed its minute. Where does it go?
- Into the minute it arrived in, so that it is at least counted
- Back into the final row, which then changes one more time
- Into a side log, kept for the nightly check against the raw clicks
- Nowhere, because 0.1 percent is too few to be worth keeping
Serving the dashboard from minute total rows gives up what?
- Exactness, because a total row is only an estimate of its clicks
- The raw clicks, because nothing keeps them once they are counted
- Freshness, because a row cannot be read until its minute is final
- Any question not fixed ahead, short of a recount of raw clicks
You got correct