Counting is the easiest thing a computer does, right up to the moment the count is money.
Then a duplicate is fraud, a dropped event is a refund, and an advertiser who cannot
reconcile your number against theirs stops spending.
Every time someone taps an ad, the advertiser owes money. An ad click aggregator is the
system that counts those taps, adds them up per ad and per minute, and hands the totals to
two readers: a live dashboard the advertiser watches, and the invoice they pay at the end of
the month. Watch one click go through it.
Click. Someone taps an ad at 14:59. The tap goes through your ad server as a redirect, and the ad server gives the click its id, c-8f21.
A few words come up on every line of this page:
An event is one thing that happened: a click, or an impression (the ad was shown).
A window is a bucket of time, here one minute. Every click that happened between 14:59:00
and 14:59:59 belongs to the 14:59 window.
To aggregate is to turn many events into one number: 1,204 clicks on ad 88231 in the
14:59 window, stored as one row.
Stream processing counts events as they arrive, a little at a time. Batch processing
waits, then counts a whole hour or day in one go.
This is a staff level question because it has no comfortable answer. Streaming gives you
fast numbers that are occasionally wrong. Batch gives you correct numbers hours late. The
design is how you offer both without keeping two versions of the truth.
The word "count" hides four different requirements. Separate them before you draw, because
each one pulls the design somewhere else.
You ask
They say
What it settles
How many events a second, and how spiky?
A million a second, with regional peaks.
An append only log in front of everything: a list you can only add to the end of, which is one of the cheapest writes there is. Nothing that counts events is allowed to slow down the ad server that produces them.
Is the count for a dashboard, or an invoice?
Both. Advertisers watch it live and are billed from it monthly.
The hardest requirement on the page. Live means seconds and approximate, billing means exact and reconcilable, and that tension is the whole design.
How fresh does the dashboard have to be?
About a minute behind is fine.
Minute windows in a stream processor rather than an update per event, which is what makes a million events a second affordable.
What happens to an event that arrives late?
It still has to be counted correctly.
Counting by the time the click happened, a rule for how long to wait for stragglers (a watermark), and the ability to correct a window you already published. This one answer rules out every design that can only add numbers and never fix them.
Can the same click be counted twice?
Never. That is fraud, and it gets refunded.
A click id created at the source, and a pipeline where each click changes the total exactly once, even when messages repeat. The cheap approximations are off the table.
How long do we keep raw events?
Years. Advertisers dispute invoices.
The raw log is archived and is the real source of truth. Every aggregate on this page can be deleted and rebuilt from it, which is what makes restatement safe.
The trick that makes this system affordable is in the last two rows. (New to estimates like
this? The back of the envelope chapter shows the method.)
Ingest and storage
1000 thousand
400 bytes
200 thousand
Events per second1,000,000
Raw event volume per day1,000,000 x 400B x 86,400 = 35 TB
Aggregated rows per minute200,000
Aggregated rows per day200,000 x 1,440 = 288,000,000
Compression from aggregating300x
300x
fewer rows after aggregation, and that ratio is the whole business case
Writing 1,000,000 rows a second, one per event, into the table advertisers query would take a large cluster just to keep up. Aggregating first turns it into 200,000 rows a minute, which any columnar store handles comfortably. Everything difficult in this design comes from the fact that aggregation happens before storage, so correctness has to be settled in the stream rather than fixed later with a query.
A columnar store keeps each column's values together on disk, so summing clicks across
millions of rows reads only the clicks column. That is the opposite of an app database,
which keeps each row together. OLTP vs OLAP
shows the difference live.