Most counters in this course are allowed to be "about right". A like count that's off by three bothers nobody. This one is different. Every click on an ad is money: the advertiser pays for it and the publisher gets a share. Count one click twice and you overcharge a customer. Drop one and you give money away. Do either at scale and you'll hear from the finance team, then from lawyers.
So this lab is about one hard question: how do you count a billion events a day exactly once, quickly, when every part of the pipeline can crash, retry or deliver late?
The brief
You're building the click-counting system for an ad network. Ads show up on websites and apps. When someone clicks one, your click server records it and sends the user on to the advertiser's page. Advertisers watch a dashboard of clicks per ad, per minute. At the end of each day, the billing system charges them for the clicks.
Who uses it?
- People clicking ads. They want the landing page fast and don't know you exist.
- Advertisers watching campaigns live and adjusting budgets.
- The billing system, which needs final, correct numbers.
- The budget system, which stops showing a campaign's ads once its daily budget is spent.
In scope: receiving clicks, deduplicating them, aggregating per ad per minute, serving the dashboard, and producing final billing counts.
Out of scope: choosing which ad to show (the ad server), sophisticated click-fraud detection (we'll leave a hook for it), and invoicing.
Requirements
Functional
- Record each click and redirect the user to the advertiser's URL.
- Show clicks per ad, per campaign and per advertiser, per minute, for any range up to 90 days.
- Produce final daily counts per ad for billing.
- Feed near-real-time spend to the budget system.
Non-functional
- Exactly-once counting. Each valid click is counted once in the final billing numbers. No double-counts, no drops.
- Redirects are fast: p99 under 50 ms at the server. A slow redirect loses the advertiser their visitor.
- Dashboard is fresh: counts within about a minute of real time.
- Budget data within seconds, so a campaign doesn't overspend by much.
- Raw clicks are kept for at least 90 days, for audits and disputes.
Notice that "fresh" and "exact" pull against each other. The trick is to give each consumer the one it actually needs.