BOOK DEEP READ · System Design Interview Vol 2 · Ch 6

Ad Click Event Aggregation

System Design Interview Vol 2 · Ch 6 · Alex Xu & Sahn Lam · 2022

中文 →

What is this chapter about?

An ad scrolls past on your phone and you tap it. For that one tap, an advertiser pays the platform. Worldwide there are a billion such taps a day. This chapter is about one thing: how to count those billion taps into a bill nobody can poke a hole in.

Something odd first

It sounds like counting — tap, add one, a child could do it. But the moment the number turns into money, its nature changes completely. It is no longer a reading on a pedometer; it is the till in a cash register. Nobody cares if a pedometer skips a step. If the till comes up short, the month doesn't balance. Everything hard about this chapter comes from that switch of identity.

Why the old world found this hard

Three things. Too many: ten thousand taps a second on average, fifty thousand at peak — no single machine can count that, so dozens of machines count in parallel and someone has to add it all up. They arrive late: you tap an ad on the subway, the signal drops, and your phone only sends the record once you're back above ground. It may be three minutes late, or three hours. Does that tap belong to "that minute back then" or "this minute now"? They arrive twice: the network hiccups, the phone never sees an acknowledgement, so it sends again — count the same tap twice and the advertiser has been overcharged.

How it pulls this off

Keep the running receipts. An incoming tap isn't counted right away; it is first parked, untouched, in one long queue — the way a shop keeps the stub of every receipt. If you later get the sum wrong or change the rules, you just run the stubs through again.

One drawer per minute. Instead of maintaining a single "total" that keeps changing, give every minute its own drawer, and let each tap fall into the minute it was born in. The drawers sit end to end and never overlap, so no tap can land in two of them.

Give latecomers a grace period. When a minute ends, don't seal the drawer immediately — wait another three minutes, the way the last bus waits three more minutes before it pulls out. Almost all the stragglers make it. The ones that truly don't get settled overnight.

Number every receipt. Each tap is born carrying a globally unique number. Before accepting it, check whether that number has been seen before; if it has, throw it away. Send the same one a hundred times and it still counts once.

So what do you choose?

Fast and accurate pull against each other here. A longer grace period recovers more late taps and gives a truer bill — but the answer arrives later, and advertisers watching a live dashboard to adjust their bids can't use a number that shows up too late. So the industry usually walks on two legs: publish a "good enough, fast" number within minutes for people to look at, then overnight recount from the full pile of stubs to get the "accurate" number that actually settles the bill — and compare the two, raising an alarm if they drift too far apart.

The cost is worth stating plainly: from then on this system permanently carries two sets of numbers and a reconciliation line, which is an order of magnitude more to operate than "counting one number".

One line to remember

Counting clicks isn't counting, it's bookkeeping. So the system stores every tap verbatim first, counts into per-minute drawers, gives latecomers a grace period, numbers every receipt against duplicates, and overnight recounts from the original stubs to reconcile — the fast number is for people to look at, the accurate one is what gets paid.

Want the mechanisms, the notation and the diagrams? → Switch to the deep read