Asked at
URL Shortener (TinyURL)
Asked from Junior level upFoundations & Storage
Distributed Rate Limiter
Asked from Mid level upAPI Gateway & Security
Distributed Key-Value Store
Asked from Senior level upFoundations & Storage
Distributed Notification Service
Asked from Mid level upMessaging & Queues
Distributed Web Crawler
Asked from Mid level upDistributed Systems
Metrics & Monitoring Platform
Asked from Senior level upObservability & Time-Series
Real-Time Chat App (like WhatsApp or Slack)
Asked from Mid level upReal-Time & WebSockets
Social News Feed (like X/Twitter)
Asked from Mid level upSocial & Feed Systems
Ride-Hailing App (like Uber)
Asked from Senior level upGeospatial & Real-Time
Low-Latency Stock Exchange Matching Engine
Asked from Staff level upFintech & Low Latency
Global Video Streaming Platform (like YouTube or Netflix)
Asked from Senior level upMedia & Streaming
Distributed Hotel & Ticket Reservation System
Asked from Mid level upE-Commerce & Transactions
Shared Document Editor (like Google Docs)
Asked from Staff level upReal-Time & Distributed State
Game Leaderboard
Asked from Junior level upCaching & Data Structures
File Sync Service (Dropbox / Google Drive)
Asked from Mid level upStorage
Payment System
Asked from Senior level upPayments & Transactions
Ad Click Aggregation
Asked from Senior level upStream Processing
AI Support Assistant (RAG)
Asked from Senior level upAI Systems
LLM Inference Platform
Asked from Senior level upAI Systems
Enterprise Semantic Search
Asked from Mid level upAI Systems
AI Code Completion
Asked from Senior level upAI Systems
LLM Gateway
Asked from Senior level upAI Systems
AI Agent That Takes Actions
Asked from Senior level upAI Systems
LLM Evaluation Platform
Asked from Senior level upAI Systems
AI Document Processing Pipeline
Asked from Mid level upAI Systems

Design Ad Click Aggregation

Asked from Senior level upDomain: Stream Processing
Concepts to know first:

Count billions of ad clicks per ad per minute — exactly enough to bill advertisers, and quickly enough for a live dashboard.

An analogy

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.

Where this design ends up. This is version 3 of 3. Part 5 builds it up from version 1 and says why each part was added.
  1. Clients
    • Browser
    • Advertiser dashboard
  2. Services
    • Click counting service
    • Dedup step
    • Stream job
    • Daily batch
  3. Queues, caches
    • Click log
  4. Data, outside
    • Totals store
    • Raw click archive

1. Requirements and scope

Functional Requirements
  • 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.
Non-Functional Requirements & SLAs
  • Billing-grade correctness: no double counting, no lost clicks.
  • Handle late and out-of-order events.
  • Peaks of about 50,000 clicks per second.
Key Traffic Assumptions: 1 billion clicks a day across 2 million active ads. • Clicks come from many edge servers; some arrive minutes late.

2. Back-of-the-envelope numbers

Writes and reads each second
  • 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.

Write Throughput
1B / 86,400 ≈ 12,000 clicks/s on average; ~50,000/s at peak
Read Throughput
Low: ~100 dashboard and report queries/s
Storage Growth
Raw events: 1B × 200 B ≈ 200 GB/day ≈ 73 TB/year in cold storage; minute-level aggregates are rolled up to hours and days
RAM Cache Footprint
Click ids for deduplication over a one-hour window ≈ 3,600 s × 50,000/s × 40 B ≈ 7 GB

3. API

POST/v1/clicksFrom edge servers: { clickId, adId, ts, … }
GET/v1/ads/{adId}/clicks?from=&to=&granularity=minuteAggregated counts

4. Data model and storage

Storage Strategy: Kafka handles the raw stream, and a stream processor (Flink or Kafka Streams) computes windowed counts. An OLAP store (ClickHouse, Druid or Pinot) holds the aggregates, and raw events are archived to object storage for recomputation.
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.

BrowserAdvertiser dashboardClick counting serviceClicks database12345
  • The order of the steps
  • A request or its reply

The parts: switch one off

1. Clients

Browserclicks an ad
Advertiser dashboardreads the totals

2. Services

Click counting serviceaccepts clicks, serves totals

3. Data, outside

Clicks databaseone row per click

The steps in words0 of 5 steps shown

  1. Browser to Click counting serviceHTTPS · 3 ms

    A browser reports a click on ad 7.

  2. Click counting service to Clicks databasequery · 8 ms

    It inserts one row for the click.

  3. Advertiser dashboard to Click counting serviceHTTPS · 3 ms

    The dashboard asks for the totals.

  4. Click counting service to Clicks databasequery · 8 ms

    It counts the rows for each ad. This is slow at scale.

  5. Click counting service to Advertiser dashboardreply · 20 ms

    The totals go back to the dashboard.

Why this version
Simple, and exact at small scale.
Next bottleneck
Tens of thousands of inserts per second plus heavy aggregation queries overwhelm one database.

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

  1. Browser
  2. Click edge
  3. Kafka
  4. Dedup
  5. Stream processor
  6. OLAP store
  7. Object storage
  1. 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.

  2. Click edge to Kafka

    The edge writes the click to Kafka, keyed by ad id.

  3. Kafka to Dedup

    A dedup step drops any click id it has already seen in the last few minutes.

  4. Dedup to Stream processor

    The stream processor counts clicks per ad per minute, by the time each click happened.

  5. 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.

  6. Kafka to Object storage

    Every raw click is also kept for the daily batch job.

Read pathAn advertiser opens the dashboard

  1. Advertiser
  2. Dashboard API
  3. OLAP store
  1. Advertiser to Dashboard API

    The advertiser asks for clicks per hour for one campaign.

  2. Dashboard API to OLAP store

    It adds up the minute counts for that campaign and time range.

  3. 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

Event time, windows and watermarks
A click at 10:00:59 that arrives at 10:03 belongs in the 10:00 window. The processor tracks a watermark ("everything up to 10:01:30 has probably arrived") and closes windows once it passes. Events later than the allowed lateness go to a side output for correction instead of being dropped.
Hot ads
A viral ad sends most clicks to one partition. Add a random suffix to the key (ad_id plus 0–9) for a first aggregation step, then add up the partial counts in a second step.

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.

How each level is judged
What is judgedJuniorMidSeniorStaff
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

Strong answer
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.
Weak answer
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

Strong answer
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.
Weak answer
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

Strong answer
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.
Weak answer
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

Strong answer
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.
Weak answer
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

Strong answer
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.
Weak answer
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.]