Ad-Click Aggregation: First Design
One table holds every click, and a query counts them. From minute ten to minute twenty that is your whole answer, and it is more exact than anything you build after it. The rest of the round works hard to get back to what this table does for free.
The interviewer pushes four times once it is on the board, and those pushes are this page's quiz. Before them you owe one click and one dashboard load, both traced to that table, and the limit named in your own words.
A Clicks Table and a Query
A click reaches a stateless tier of click servers behind a load balancer. The server inserts one row into a clicks table in one relational database, then answers with the redirect. A dashboard server runs a query that groups by ad and minute and counts. The billed total is the same count over a day.
The top ads are that minute query sorted, with a limit. Treat it as a sort for now and say so, calling it the top list, because you come back to it by that name in the deep dives.
One Click, One Dashboard Load
A user taps an ad. The ad's script makes a click ID and sends it with the ad. The load balancer picks any click server, which stamps the time and inserts one row: ID, ad, time, country. A unique key sits on the click ID. Only when the insert commits does the server send the redirect, and that order matters. The other way round, a crash after the redirect loses a click nobody knows about.
Now the resend. The phone never hears the answer, so it sends the same request with the same ID. The second insert hits the unique key, no row is added, and the server answers with the redirect anyway. The user moves on, and the count has not changed.
From the other side, an advertiser opens the dashboard. A dashboard server asks the same table for that ad's rows over the last hour, grouped by minute, and counts them. What appears on screen is every click that has landed so far, each one once.
What the Table Gets Right
Every click is one row with a unique key, so a resend hits the constraint, and a count is exact. Any question the advertiser thinks of later is a new query, because nothing has been thrown away. One million clicks a day is about 12 a second, and at that size this serves the first advertisers as it stands. Grant all of that before any limit comes up. Every later design has to match the first claim, and each one you draw gives up some of the second.
Where It Gives Out
Every click is a write on the path that sends a user onward. Every dashboard load scans an hour of rows. A click that arrives late just lands in a row, so what a published minute means is never defined. And a redirect waits on the database, so whatever slows the database slows the user. Name all four and fix none of them yet.
Hand over the first yourself, in about ten seconds: "One database takes every click and every dashboard query here. At 100,000 inserts a second, inserts would slow, then redirects with them, so the write comes off the redirect."
Notice what that line leaves out: any figure for what one database can take. No published run supports one, and the partition sum from Scope already says what a row per click would need.
- Opening with a stream processor and a log before one click and one dashboard load have been walked. The interviewer cannot tell what each box is answering.
- Calling this design wrong because it is slow. It is exact, and that is what the later designs must still be.
- Writing the row after the redirect has gone out, so a crash loses a click nobody knows about.
- Forgetting the unique key on the click ID, which is what makes the first design correct on duplicates.
- A click and a dashboard load traced to the same table, one from each side.
- The candidate says what is good about the first design: exact, and open to any question.
- The redirect slowing down, named as the first symptom, with the insert rate beside it.
Knowledge Check
"Ten billion clicks a day is about 100,000 inserts a second, and twice that at peak. Where do you put them?"
- "Shard the clicks table by ad, and let every shard absorb its share of the inserts."
- "Buffer inserts on each click server, and flush one large batch every few seconds."
- "Append each click to a partitioned log, and let readers do all the counting later."
- "Sample one click in a hundred, then multiply the totals by a hundred on display."
"Every time an advertiser opens the dashboard, that GROUP BY runs over the last hour. What does that cost you?"
- "Index on ad and minute, so that the query no longer scans the whole of the hour."
- "Cache each result for a minute, so most dashboard loads never reach the database."
- "Copy the clicks into a column store, and run the same query there on each dashboard load."
- "Count per ad and minute in a stream job, and serve the dashboard from those rows."
"A click arrives four minutes late, after its minute was already published. What do you do with it?"
- "Count it by its own time, hold each minute open past a mark, and update its total."
- "Drop it, since clicks that late are rare and a published minute should be closed."
- "Count it in the minute it arrived, since that is the time this system first saw it."
- "Hold back every minute for five minutes, and publish nothing until it is complete."
"The aggregator crashes and replays the last thirty seconds. What happens to your totals?"
- "They double-count, so subtract thirty seconds' worth of clicks after each restart."
- "Commit the log offset before counting, so a replay can never see those clicks again."
- "Restore counts and offset from one checkpoint, and write each total by its key."
- "Keep click IDs in a shared cache, and skip each click that the cache has seen."
You got correct