Lesson 4 · Minutes 30-45

Ad-Click Aggregation: Deep Dives and Curveballs

Problem

An invoice is the reason this problem is hard, and minute thirty is where you defend one. It sits between two debts: first the clocks behind a window, with the stuck partition you named, and after it the top list you have treated as a sort since the first design. Four curveballs take what is left of the fifteen minutes.

Windows and the Stuck Partition

Three clocks can stamp a click. The phone's may be wrong by any amount. The click server's is close and in step with its fleet. The aggregator's is what processing time means: when the click reached the counting, as against event time, when it happened. The choice is the click server, with a batched click's age subtracted. The raw record keeps the arrival time too, so a bad clock can be found later, since clocks drift and step, as Chapter 5's Clocks and Ordering says. A phone can report an age because an age needs no agreement about what time it is.

The watermark is a dial. At five minutes, nearly every click makes the final total, and finals wait five minutes. At one minute, finals are quick, and more goes to the side log. Measured lateness sets it, and the trigger for moving it is the share past the mark. Five minutes holds while that share stays at the 0.1 percent Scope assumed.

Now the stuck partition, as promised. Each aggregator's watermark moves on its own partition, so one ad's total is final on its own clock. The top list merges all 20, so it is final only when the slowest has passed the minute. One stuck partition holds every minute's list open.

So the step that merges the lists watches all 20 watermarks. At about 5,000 clicks a second on average, a healthy partition never goes quiet, and a restart takes seconds. A partition with no new records past a stated time, assume two minutes, is treated as idle: the merge goes on without it, and an alarm goes to whoever runs the log. When the partition returns, its rows take the corrections and the lists it missed are written again by key. What it costs: a list marked final that can still change, in this one case.

Exactly Once, for an Invoice

The stream now has three catches: an ID at the origin, one checkpoint, and a write by key. They hold unless a bug, a skipped partition or a resend past a day gets in. So the invoice gets a second witness. A batch recount runs the same counting code over the raw archive each night, and compares per ad per day.

The stream left the side log's clicks out on purpose, so the comparison adds them back: finals plus side log, against the recount. A difference past a stated tolerance, assume one click in ten thousand or ten clicks, whichever is larger, pages someone and blocks that ad's invoice until a rerun agrees or that person signs it off. The billed number is the recount's, one row per ad per day, written by key, beside the minute totals. It runs about one click in a thousand above that day's finals, and the side log says which clicks those are.

One counting code on both paths means a fix lands once. Two codes drift apart, and then the difference is noise.

The recount reaches further than the stream. It works on disk, so it can check IDs further back: assume a week, about 1 terabyte, which catches a resend days late. That week is also the limit. The click server refuses an age past it, because an older copy would be billed again, and it counts a negative age as zero, because one forged future stamp would drag a partition's watermark forward. A click that turns up after its day was invoiced stays in the archive and goes on the next invoice as a correction, because a closed day does not reopen. What the recount cannot see is a click that never reached the log, such as one in the buffer of a server that died. Both witnesses read one log.

What it costs: raw clicks kept at 2 TB a day, an invoice that waits for the night, and a nightly compute run. Assume 20 workers at 20,000 a second each, and ten billion clicks take about seven hours.

Two numbers, one record
The chart
the fast number: seconds behind, may move for five minutes
The final total
stops moving after five minutes
The recount
the right number: nightly, checked against the stream, on the invoice
The raw archive
every click, the record the rest are rebuilt from

When the interviewer asks which number goes on the bill, answer in one breath: "The chart may move for five minutes, the final total stops, and the invoice uses the nightly recount, which I compare with the stream per ad per day."

Chapter 13 also rolls numbers up into windows, but its background job does that over blocks already stored, and it owns the write path and retention of numbers nobody is billed on. A repeated reading there writes what is already stored. A repeated click here is money, so the question here is what a billed total means.

The Top List

Here is the top list you parked as a sort. Each aggregator keeps its own top 100 per minute, and a small step merges the 20 lists into one ranking, stored by minute beside the totals. Because the log is keyed by ad, each ad's whole count sits in one partition. So an ad in the true top 100 must be in its own partition's top 100, and the merge is exact.

Say that condition, because it is the whole argument. Lists from partitions keyed by anything else can miss an ad spread thinly over all of them. The same trap waits in time, if the interviewer asks for the hour's top ads: sixty minute lists cannot be summed into an hour's, because an ad that sits 101st every minute is in none of them. That list needs its own window, at most 5 million counters and 500 megabytes across the fleet, with the same merge.

Merging top lists
Keyed by adeach ad in one partition
Exact. A true top 100 ad is in its own partition's top 100, so the merge cannot miss it.
Keyed by anything elseeach ad across many partitions
An ad spread thinly is missed. It can be 101st on every partition, in no list, and first overall.

A count-min sketch, a fixed grid of counters that answers approximately, earns its place only when exact does not fit. Here exact fits: a minute's counts for a million ads at 100 bytes is 100 megabytes across the fleet, 5 on each partition on average. So the ranking is exact, and the sketch waits for a far larger key space.

Exact Counts vs Count-Min Sketch

Exact counts keep one counter per key, so memory grows with the keys and the numbers can be billed. Use them whenever the keys fit in memory, as a million clicked ads a minute do.

A count-min sketch keeps a fixed grid of counters, updated through several hash functions, and answers with the smallest it finds. It can overcount and never undercounts, and memory stays the same however many keys arrive. Use it for a ranking over a key space too large to hold, such as every search term, and never for a number on an invoice.

Curveballs

"Filter out bot clicks." Keep the raw record and filter on read. A simple rule at the click server only marks the click, and a slower check over a day of behaviour marks it later, in the archive. The recount applies the marks before the invoice, so a billed total differs from the chart for a reason that can be shown. Its comparison with the stream runs before the marks, so a mark never trips the tolerance. Never delete the click: the bot list will change, and the advertiser will ask. What it costs: a mark on every click, and dashboards that fall when marks arrive.

"We found a bug. Recompute last week." The raw archive exists for this. Seven days is 14 terabytes and 70 billion clicks, run by the fixed code on a separate fleet into a new version of the totals. Assume 100 aggregators at 20,000 a second. That is 2 million a second, under ten hours, and live counting is untouched. The writes are the bigger bill: 10 billion minute rows in ten hours is 280,000 a second, far past the live store, so the new version gets its own 560 partitions at half their limit for those hours. Compare, then switch readers with one pointer. A strong candidate also asks who gets told, because corrected invoices are a business process.

"Show the top 100 per country." It multiplies keys, not partitions. Assume a clicked ad reaches 5 countries in a minute. Five million counters at 100 bytes is 500 megabytes across the fleet, 25 on each partition. Each ad is still in one partition, so the merge per country stays exact, and only the lists reach the store. The trigger for a sketch is enough dimensions that the keys pass what an aggregator's memory holds, assumed at a cache node's 100 gigabytes.

"One ad during a championship match takes a tenth of all clicks." At peak that is 20,000 a second, one aggregator's full limit. Salt the key for that ad only, as in Chapter 3's Hot Partitions: the ad plus a suffix from 0 to 9, picked from the click ID so a resend lands on the same sub-key, about 2,000 a second each. A match is known ahead, so salt before kick-off. Otherwise count per key and salt past a stated rate, assume 5,000 a second, from a short list the click servers read. What it costs: a step that sums sub-keys per minute, and a merge that must sum before ranking, because the exact-merge condition no longer holds for that ad.

How Each Level Answers

A mid-level answer writes each click with its unique key, moves clicks onto a log when the rate is raised, and adds per-minute totals. It knows late clicks exist and comes to event time with a hint. It reaches for exactly once as a feature of the tool until the interviewer asks what a replay does to the total.

A senior answer asks about billing first and designs two numbers. It puts sizes on them: 200,000 a second, 20 aggregators, 3 million clicks in a replay. It raises the late click and the crash without being asked, tells the two duplicates apart, and says when a published total stops moving. It states the condition that makes the top list's merge exact.

A staff answer treats the invoice as the design's contract and asks who settles a dispute, because that answer sets how long raw clicks are kept and what tolerance pages someone. It brings the recount in unasked and uses it to take pressure off the stream. It plans the bot filter and the recompute as changes to money already billed, and asks who tells the advertiser.

Interview Pitfalls
  • Reaching for a count-min sketch because the problem says top. The interviewer asks how big the exact version would be, and the sum ends the argument.
  • Deleting bot clicks from the log, then having nothing to show an advertiser who disputes the bill.
  • Salting every ad to survive one hot one, which turns every minute total into a gather across ten keys.
What Interviewers Listen For
  • The candidate states what the watermark costs in both directions, and what sets it.
  • The candidate says which number the invoice uses, and how the two numbers are compared.
  • The candidate salts only the hot key, says how it was found, and names the extra step.

Knowledge Check

"Filter out bot clicks." What is your best move?

  • Keep every click, mark the bots, and apply the marks on read
  • Drop flagged clicks at the click server, before the log sees them
  • Delete flagged clicks from the archive after each night's recount
  • Subtract an estimated bot share from every total on the invoice

"We found a bug. Recompute last week." Why does that need the raw archive?

  • The log keeps a week, but aggregators cannot read its clicks twice
  • A minute total has thrown away the clicks that the fix has to read
  • Checkpoints hold only the seen IDs, and those have since expired
  • The live store refuses all writes while a recompute is under way

"Show the top 100 per country." What does that multiply?

  • The log partitions, since each country wants a partition of its own
  • The aggregators, since each country's list needs its own process
  • The rows in the store, since every counter is now written out
  • The counters in memory, from 1 million a minute up to 5 million

You salt one hot ad into ten sub-keys. What does that do to the top list?

  • Nothing, since the merge goes on ranking the 20 lists as before
  • The ad falls off the list, as no sub-key can rank high enough
  • The merge has to sum that ad's sub-keys before it ranks them
  • Every ad on the list now has to be read from ten different keys

You got correct