Library
35 min read

Design an ad click aggregator

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.

BrowserAd serversEvent logDedupeStream processorServing storeDashboardRaw archiveBatch recompute

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.

Step 1: Understand the problem#

The word "count" hides four different requirements. Separate them before you draw, because each one pulls the design somewhere else.

You askThey sayWhat 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.

What you are building#

Functional requirements
What the system does.
  • Ingest click and impression events. From ad servers worldwide, at rates that make per event database writes impossible.
  • Aggregate by advertiser, campaign, ad and minute. Minute granularity, rolled up on read.
  • A near real time dashboard. Advertisers watch spend live and pause campaigns based on it.
  • A billing grade number. Correct, reproducible, and reconcilable, even if it arrives hours later.
Non-functional requirements
How well it has to do it. The numbers you commit to.
  • 1 million events per second at peak.
  • Dashboard within about a minute of the event.
  • Billing figures must be exact and must not change after they are finalised.
  • A replay must produce byte identical results, because disputes get audited.
Out of scope
What you cut, and say so out loud.
  • Ad serving and auction. Microsecond budgets and a completely different design.
  • Click fraud detection. It sits downstream of this and is a machine learning problem.
  • Attribution windows and conversion tracking. Related, larger, and its own question.

Back of the envelope#

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.