GOAT EngineerPreparing your learning space…

System Design · Practice problem · Hard

Count Ad Clicks Exactly Once

In stream processing, exactly once means every event counts once after any crash or resend. Give each click an ID, make a repeat harmless for as long as repeats can arrive, and save your reading position with your totals.

Hard means correctness and scale at the same time. You solve it on a canvas that runs your design. The canvas needs JavaScript; everything on this page works without it.

What is the situation?

Advertisers pay per click, so your count has to be right. A count that is too high is overcharging, and one too low is lost revenue. Clicks arrive at 3,000 a second. Phones resend a click when they do not hear back, usually a second later. A phone that lost its signal sends it again when it comes back, a minute or two later. Machines fail. Design the path from POST /clicks to the reporting database, so that every click is accepted fast and every distinct click is counted exactly once. There are 50,000 ads, and the top 100 take about 45% of all clicks. Two facts about your hardware: a Stream Processor remembers an event for one minute, and a Counter Store keeps everything in memory.

What must your design do?

  • Phones are answered within 100 ms at the 99th percentile.
  • At least 99.9% of clicks are accepted.
  • No click goes uncounted, and none is counted twice, including one sent again a minute or two later.
  • The slowest click reaches the count within 15 seconds normally, 30 in a spike or a storage problem, and 35 after a crash.
  • The system costs $1,000 a month or less.

What does Run test?

Run works without an account. It checks your design against 2 tests you can read before you start:

  • Steady clicks. 3,000 clicks a second for 20 seconds.
  • Five percent sent twice. 5% of clicks were sent a second time, one second later.

What does Submit add?

Submit needs an account. It runs 7 more tests you cannot read in advance, and stops at the first one your design fails, showing what happened and why. Passing every test, or giving up, unlocks the written solution.

Which parts can you use?

Already on your canvas: Counts.

  • Load Balancer: about 50,003 a second, $18 a month each
  • App Server: about 2,000 a second, $45 a month each
  • Queue: effectively unlimited, $25 a month each
  • Worker: about 1,000 a second, $45 a month each
  • Database: about 5,000 a second, $120 a month each
  • Counter Store: about 100,003 a second, $30 a month each
  • Outbox Relay: about 5,000 a second, $40 a month each
  • Stream Processor: about 50,078 a second, $90 a month each

Every connection takes 1 ms, and the budget is $1,000 a month.

What does this problem leave out?

  • What saving costs. The processor saves what it remembers with its place for free, so the time and storage a real checkpoint takes are left out.
  • Choosing how long the processor remembers. A real one lets you set that window, and a long enough window can do the work of the unique key, at the cost of storing every ID for that long.
  • A log on disk for a Counter Store. A real one can keep a log, which slows it down.
  • A server that reads "already saved" and stops. Here a server makes every call it is connected for, so a repeat the unique key refused still goes on down the line. A change feed passes on only what was really saved.
  • A queue going down. Queues here have no copies, so we never crash one.
  • How long the unique key's rows are kept. A real table would expire them.
  • Late events and event-time windows, multiple regions, bot and fraud filtering, and reading the counts.

Where does this problem fit?

Its category is Analytics & leaderboards: Aggregate continuously. It practises queues & async, idempotency, stream processing and reliability & ops.

For the whole system, read the interview breakdown Design an Ad Click Aggregator (Google Ads).

See every practice problem.

By GOAT Engineer. Explore System Design · Privacy Policy · Contact.