Ad-Click Aggregation: Scope
Is anyone billed on this count? That question goes first, ahead of any rate, because a yes turns one number into two. The first ten minutes settle that, then the size of the stream, then which of the sums actually moves a decision.
Billing First, Then Five More
Each of these six moves something on the board. Hold on to the agreed answers, because the sums below start from them.
- Is money computed from this count, and what error is acceptable? A counter for a chart may drift, and a counter for an invoice may not. Agreed: advertisers are billed on it, so the billed number counts each real click once.
- How many clicks a day? The answer sets the write path. Agreed: ten billion, with a peak of twice the average.
- How fresh must a total be, and when does it stop moving? Fresh and final are two different numbers. Agreed: a first total within a minute of the minute ending, a final one after five, and no invoice ever reads a first total.
- What does the dashboard ask? A fixed question can be computed ahead, and an open one has to keep every row. Agreed: totals per ad per minute, the top 100 ads in a minute, and one ad's last hour. Other breakdowns are a curveball.
- How late can a click arrive, and whose clock stamps it? The answer sets how long a minute stays open. Agreed: the click server stamps it, a phone that batched clicks offline sends each click's age for the server to subtract, and assume 99.9 percent reach the counting within five minutes.
- Do bot clicks count? A filter changes what the billed number means. Agreed: parked as a curveball, and you say so out loud.
The question says billed, and the word is easy to walk past. Stop on it before any arithmetic: "Before the numbers, one question: is anyone billed on this? If so, I have two numbers to design, a fast one for the chart and a right one for the invoice."
The Promises and the Exclusions
Four things are functional: record a click and send the user on to the advertiser, show totals per ad per minute, show the most-clicked ads, and produce a billed total per ad per day. Four are non-functional. The redirect never waits on the counting. A first total arrives within a minute. The billed total counts each real click exactly once. Any total can be recomputed from the raw clicks.
Ad selection and auctions, accounts and budgets, and conversion tracking are out, and you name them. Each is a system of its own, and none of them changes how a click is counted.
From Ten Billion to Four Hundred Partitions
Ten billion clicks a day, over about 100,000 seconds, is about 100,000 a second. At twice the average, the peak is 200,000. The write path is sized from that number.
The bytes come next and leave quickly. Assume 200 bytes a click. That is 2 terabytes a day and 730 a year, kept in an object store as the record of what happened. At peak the stream is 40 megabytes a second, and one three-broker log cluster, at the 600 megabytes a second one vendor reports, carries that fifteen times over, so bandwidth decides nothing.
Partitions do decide. A key-value partition's limit is 1,000 writes a second, so a row per click needs 200 of them at peak, and 400 at half their limit. Counting is the other side of it. Assume one aggregator, the process that adds up clicks, counts 20,000 a second. Run at half that, 200,000 clicks need 20 of them.
Now what the dashboard reads. Assume 5 million active ads, with one in five clicked in any minute. That is a million total rows a minute, one row per ad and minute, or about 17,000 a second. At half a partition's limit, that is 34 partitions, against the 400 a row per click needs. At 100 bytes a row, thirty days of minutes is about 4 terabytes, 13 with three copies, and 7 nodes at an assumed 2 TB each. So writes decide here and bytes do not, and this sum says what counting ahead is worth.
One more, because it sounds expensive. Catching duplicates means remembering click IDs. At 16 bytes an ID and ten billion a day, that is 160 gigabytes, or on average 8 on each of 20 aggregators, the same 8 gigabytes Chapter 6's Delivery Semantics at Scale worked out. So size is not the problem. Where those IDs live is the problem, and the crash probe comes for it.
The Click, the Chart and the Bill
A click is a GET on a click path, carrying the click's ID and the ad, and the answer is a redirect to the advertiser. The ID is made where the click happens, by the ad's script on the phone or in the browser, so a resend carries the same one. Chapter 6's Delivery Semantics at Scale says why, and Chapter 5's IDs Without Coordination says how. A phone that batched clicks offline adds each click's age in seconds, and ignores the redirect, since its user has long moved on.
A second GET returns an ad's minute totals between two times, each marked first or final. A third returns the ranking for a minute. A fourth returns an ad's billed total for a day.
GET /c/9f2c41ab?ad=a-77 302 to the advertiser's page
GET /c/c07e18d2?ad=a-77&age=540 302, and the phone ignores it
GET /ads/a-77/clicks?from=14:00&to=15:00
200 [{"minute": "14:32", "count": 412, "state": "first"}]
GET /top?minute=14:32&n=100
200 [{"ad": "a-12", "count": 90211}]
GET /ads/a-77/billed?day=2026-11-05
200 {"day": "2026-11-05", "count": 588104}
There are two entities. A click holds its ID, the ad, the time it happened, the time it arrived, and the country, which the click server reads from the address. A minute total holds the ad, the minute and the count, and reads as first or final.
- Opening on queries per second before asking whether the count is billed. The interviewer has to redirect a candidate who is designing a chart.
- Agreeing on one real-time total, which by minute 25 has to be both fresh and final.
- Claiming a number for what one relational database can take. No published run supports it, and the partition sum says what you need without it.
- The candidate asks who pays on the number before asking how big it is.
- Ten billion becomes 100,000 and 200,000 out loud, then partitions: 400 against 34.
- The candidate drops bandwidth on purpose, against one log cluster's capacity.
Knowledge Check
The interviewer will answer one question before you start drawing. Which one changes this design the most?
- Which countries advertisers most want to see in their reports
- How many advertisers log in to the dashboard on an average day
- Whether anyone is billed on the count, and what error is fine
- How many months of minute totals the dashboard should show
Which of these estimates decides nothing, so that you can drop it after one sentence?
- 40 MB a second at peak, against one log cluster's 600 MB
- 400 partitions for a row per click, against 34 for totals
- 20 aggregators to count 200,000 clicks a second at peak
- 5 million active ads, with one in five clicked in a minute
Why do the first total and the final total have to be two separate numbers?
- The first total is a sample, and the final one counts them all
- The chart reads from a copy, and the invoice from the primary
- The invoice needs more precision than a minute total can carry
- Clicks keep arriving after a minute ends, and a chart cannot wait
Which of these can go out of scope with no harm to the design?
- Recomputing a total from the raw clicks after a bug is found
- Ad selection, and the auction that decides which ad is shown
- Marking each minute total as first or final for the advertiser
- Stating whose clock stamps a click, and how late one can come
You got correct