Design Ad Click Aggregation
Count billions of ad clicks per ad per minute — exactly enough to bill advertisers, and quickly enough for a live dashboard.
Like counting votes at many polling stations. Each station tallies locally, a late ballot still counts for the right hour, and no ballot is counted twice.
- Clients
- Browser
- Advertiser dashboard
- Services
- Click counting service
- Dedup step
- Stream job
- Daily batch
- Queues, caches
- Click log
- Data, outside
- Totals store
- Raw click archive
1. Requirements and scope
- Count clicks per ad per minute, and per campaign and country.
- Serve recent counts to dashboards within about a minute.
- Produce exact daily totals for billing.
- Filter duplicate and fraudulent clicks.
- Billing-grade correctness: no double counting, no lost clicks.
- Handle late and out-of-order events.
- Peaks of about 50,000 clicks per second.
2. Back-of-the-envelope numbers
- Writes: 12,000 clicks a second, on average (99%)
- Reads: 100 report queries a second (1%)
Write-heavy: about 120 writes for each read. A queue and a store that is fast at writes help most here.
3. API
4. Data model and storage
CLICK_EVENT(click_id, ad_id, ts, …) · AD_CLICKS_MINUTE(ad_id, minute, count, PRIMARY KEY (ad_id, minute))
5. The design, in three versions
Version 1: Insert every click, COUNT on read
Write each click to a database and count with GROUP BY.
Ad Click Aggregation: Version 1: Insert every click, COUNT on read Version 1 of 3
The picture follows one request through the parts of this version, in order. Under it, switch a part off to see what the request does without it.
One request, step by step
Each numbered arrow is one step. Press "Simulate Request Flow" to follow them one at a time.
- The order of the steps
- A request or its reply
The parts: switch one off
1. Clients
2. Services
3. Data, outside
The steps in words0 of 5 steps shown
Browser to Click counting service
A browser reports a click on ad 7.
Click counting service to Clicks database
It inserts one row for the click.
Advertiser dashboard to Click counting service
The dashboard asks for the totals.
Click counting service to Clicks database
It counts the rows for each ad. This is slow at scale.
Click counting service to Advertiser dashboard
The totals go back to the dashboard.
Write path, read path and failures
Each path follows one request through the final design (version 3), one hop at a time.
Write pathRecord a click
- Browser
- Click edge
- Kafka
- Dedup
- Stream processor
- OLAP store
- Object storage
Browser to Click edge
A click on an ad reaches the click edge. It gives the click an id and sends the user on to the advertiser's page.
Click edge to Kafka
The edge writes the click to Kafka, keyed by ad id.
Kafka to Dedup
A dedup step drops any click id it has already seen in the last few minutes.
Dedup to Stream processor
The stream processor counts clicks per ad per minute, by the time each click happened.
Stream processor to OLAP store
It writes each minute's count over the old one, so a rerun after a restart cannot add the same clicks twice.
Kafka to Object storage
Every raw click is also kept for the daily batch job.
Read pathAn advertiser opens the dashboard
- Advertiser
- Dashboard API
- OLAP store
Advertiser to Dashboard API
The advertiser asks for clicks per hour for one campaign.
Dashboard API to OLAP store
It adds up the minute counts for that campaign and time range.
Dashboard API to Advertiser
It returns the totals. Bills use the daily batch totals, not these.
What breaks, and what happens then
The stream processor restarts
- What users see
- It reads some clicks again.
- What the design does
- It restarts from its last checkpoint, and each minute's count is written over the old one, so nothing is counted twice.
A click arrives after its minute was closed
- What users see
- That minute's count is already written.
- What the design does
- Allow some lateness, for example 5 minutes, and update the count. The daily batch picks up anything later.
A browser sends the same click twice
- What users see
- The count could rise by two.
- What the design does
- Both copies have the same click id, so the dedup step drops the second one.
The streaming and batch totals differ
- What users see
- An advertiser could see one number and be billed another.
- What the design does
- Bills use the batch total. Each difference is logged and looked into.
6. Deep dives
7. Follow-up questions
- How do you make billing numbers exact? (Deduplicate by click id, use exactly-once checkpoints or idempotent writes, and treat a batch recomputation from raw events as the source of truth for invoices.)
- The stream processor is down for an hour. What happens? (Kafka keeps the events; on restart the processor replays from its last checkpoint; dashboards are stale meanwhile.)
What it costs
Raw clicks are the bulk: about 73 TB a year. Keep them in cheap object storage, and keep only the per-minute totals in the fast store. The stream job needs spare capacity for the 50,000-a-second peak.
Another way to design it
If the dashboard can be a few minutes late, small batch jobs every minute can replace the stream job. There is one path to maintain, not two. Billing stays correct, and the live view is a little old.
8. How answers are judged
A passing answer at Mid level. A complete design with numbers: about 12,000 clicks a second on average, 50,000 at peak. It explains windowed counting with Kafka and goes deep on one part, such as deduplication.
| What is judged | Junior | Mid | Senior | Staff |
|---|---|---|---|---|
| BreadthHow much of the system does the answer cover? | Names the core parts: clients, servers, a database and a cache. | Covers the whole main flow, from the request to the stored data and back. | Builds a complete design without being led through it. | Covers the system, the systems around it and who owns each part. |
| DepthHow far does it go into the hard parts? | Explains what each part does and why it is there. | Goes deep on one part, such as the database or the cache. | Goes deep on the two or three riskiest parts. | Goes deepest where the risk is largest, and explains why the risk is there. |
| DrivingWho leads the conversation? | Follows the interviewer, step by step. | Moves forward alone, with an occasional nudge. | Leads the conversation from start to finish. | Changes the problem when the question asked is not the right one. |
| Trade-offsAre choices compared and justified? | Names more than one option. | Says why one option was picked over another. | Compares options in detail, with what each one costs. | Ties each choice to cost, speed and risk for the business. |
| Unclear questionsWhat happens when the question is vague? | Answers the questions that are asked. | Asks questions to make the problem clear. | Sets the scope and says what is left out. | Questions the assumptions behind the problem itself. |
| ScopeWhat does it consider beyond the normal path? | Covers the normal path, when everything works. | Adds what happens when a part fails. | Adds growth: what breaks at ten times the load, and the fix. | Adds abuse, cost and the daily work of running the system. |
Requirements and scope
Before I design, I want to check the scope. Does 'duplicate' mean the same click id retried by a flaky edge server? Or does it mean the same user clicking an ad many times, which is fraud, not delivery? I will assume this design only dedupes retried click ids; fraud scoring is a separate system.
I would build a model that scores each click for bot-like patterns and drops anything that looks like fraud. That way every count we show is already clean of duplicates.
Shows: Unclear questionsThe difference: The weak answer treats fraud scoring as the deduplication problem, without asking. The strong one scopes dedup as retried click ids, a separate concern from fraud.
Estimates
1 billion clicks a day is about 12,000 a second on average, with a stated peak of 50,000 a second. Deduplication only needs the last hour of click ids: 3,600 seconds × 50,000 × 40 bytes, about 7 GB, not a full day's clicks.
To catch every duplicate, I would keep every click id we have ever seen in memory. In theory, a duplicate could arrive at any later time.
Shows: DepthThe difference: Keeping every click id forever grows memory without bound. The strong answer bounds it to the roughly one-hour window duplicates actually need to be caught within.
Core design
Edge servers publish each click to Kafka, partitioned by ad id. The stream processor then counts within one-minute windows, and raw events stay available for later reconciliation. Dashboards read pre-aggregated rows from an OLAP store, never raw clicks.
Edge servers call an API that increments a per-ad, per-minute counter directly in the OLAP database for every click. That way dashboards always read the latest count with no extra pipeline.
Shows: DepthThe difference: Writing straight to the aggregate loses the raw click log a later batch job needs to recompute exact billing totals and reconcile them.
Deep dive
The hard part is a click landing in the right one-minute window even when it arrives late. A watermark tracks how far real time has probably progressed, and a window closes only once the watermark passes it. Clicks that arrive after that grace period go to a side output for correction instead of being silently dropped.
I would close each one-minute window the instant the clock hits the next minute, and count only the clicks that had arrived by then.
Shows: DepthThe difference: Closing strictly on the clock drops any click that arrives even a little late. A watermark lets the window wait for straggling events before closing.
Failures and trade-offs
If the stream processor is down for an hour, Kafka keeps the clicks. Dashboards show stale numbers meanwhile, and a restart resumes from its checkpoint with idempotent upserts, losing or double-counting nothing. A viral ad's clicks also spread across several partition keys instead of one, so no single partition overloads.
If the stream processor is down for an hour, those clicks are gone from the counts. Nothing was running to process them while it was down.
Shows: ScopeThe difference: Kafka retains the clicks whether or not the processor is running. The strong answer resumes from its checkpoint and catches up, so nothing is actually lost.
We wrote this guide in our own words, informed by public interview guides such as AlgoMaster and Hello Interview. We are not connected to either site. Interviewers differ. The guide and the example answers show the direction each level takes, not a checklist.
10. Check your design with your own AI
Write your design first. Then copy this prompt into the AI chat you already use, and add your design at the end. It asks for a score in each area, one fix for each, and two follow-up questions.
The site sends nothing anywhere. An AI review can be wrong, so check what it says against sections 8 and 9.
See the prompt
You are a system design interviewer. Review my design for the interview problem below. Problem: Design Ad Click Aggregation Count billions of ad clicks per ad per minute — exactly enough to bill advertisers, and quickly enough for a live dashboard. What users must be able to do: - Count clicks per ad per minute, and per campaign and country. - Serve recent counts to dashboards within about a minute. - Produce exact daily totals for billing. - Filter duplicate and fraudulent clicks. How well it must work: - Billing-grade correctness: no double counting, no lost clicks. - Handle late and out-of-order events. - Peaks of about 50,000 clicks per second. Numbers to assume: - 1 billion clicks a day across 2 million active ads. - Clicks come from many edge servers; some arrive minutes late. Judge my design at the Mid level. A passing answer at this level: A complete design with numbers: about 12,000 clicks a second on average, 50,000 at peak. It explains windowed counting with Kafka and goes deep on one part, such as deduplication. For each area below, say whether my design is strong, fine or weak. Give one reason and one fix for each: 1. Requirements and scope 2. Estimates 3. Core design 4. Deep dive 5. Failures and trade-offs Then ask me the two follow-up questions an interviewer would ask next. Do not write a full model answer for me. My design: [Describe your parts, how one request moves through them, and why you chose each one.]