Skip to content
BytePatterns

Design Ad Click Aggregation

System Design Cases: lesson 14 of 20

The same click arrives twice, late, and out of order.

Lesson 14 of 20 · 7 min

Design Ad Click Aggregation

Step 1 of 11

A hundred thousand clicks a second, and the number at the end of it is what somebody is billed.

The Idea

Count clicks per campaign per minute from a firehose that repeats itself and runs late. Assume a hundred thousand events a second, and a number that somebody is billed against.

Real-World Example

Turnstile counts at a stadium, kept per gate per hour. A ticket handed in twice must not move the number twice, and a gate that reports its tally late still has to land in the right hour.

The Tradeoff

Windowed counts over an append-only log are cheap, and answer only what the window key holds — a new question means replaying the log. The watermark sets the rest of the bargain: close a minute early and late clicks are lost, close it late and every dashboard lags behind reality.

Your turn

Put the steps in the right order.

  1. Publish the closed bucket once the watermark passes the minute
  2. Append each click to the log with its own event id
  3. Add the event to the (campaign, minute) bucket it belongs to
  4. Drop the event if its id has already been counted

Mini quiz

1 / 3

Every event carries an id so that:

New lessons land every few weeks

Leave an address and we will tell you when the next one is up. That is the only reason we will use it.

One address, stored so we can email you. Nothing else, ever.