Hard · AI, ads and search
Ad serving
Each page view asks you to pick the best ad in about a quarter of a second: check who is allowed to see what, score the candidates with a model, run a quick auction and answer. Every impression and click must be counted exactly because money depends on it. Practise this hard system design problem for free with a Google or GitHub account.
The brief
Each page view asks you to pick the best ad in about a quarter of a second: check who is allowed to see what, score the candidates with a model, run a quick auction and answer. Every impression and click must be counted exactly because money depends on it.
Think of ad auctions on a news site.
Users, scale and requirements
- Who uses it
- 60 million people a month
- Audience in plain numbers
- 60,000,000 people per month
- Normal peak
- ≈ 50,000 requests per second
- Request mix
- Read: 85.95%; Analytics event: 14%; Report: 0.05%
- Speed for 99 of 100 requests
- Read: ≈ 250 ms; Report: ≈ 18,000 ms
- Successful requests
- 99.9%
- Data safety
- At most 1 minute of writes may be lost.
- Monthly budget
- ≈ $140,000
- Data at rest
- ≈ 40 TB
- Where users are
- North America: 60%; Europe: 40%
- Home region
- North America
- Waiting deadlines
- Read: 900 seconds; Analytics event: 3,600 seconds; Report: 3,600 seconds
- Required proofs
- Consent records
≈ marks an approximation, just as in the game. Waiting deadlines measure the age of queued work and are separate from interactive response time.
- Model scoring takes about 20 ms per ad request (approx.).
- Impressions and clicks are billing records.
Questions that shape the design
How long do we have?
About 250 ms from the page asking to the ad being chosen, counting the trip from far-away readers (approx.). A slower answer means a blank space, which earns nothing.
What if the scoring model is slow?
Fall back to a simpler ranking. A decent ad on time beats a perfect ad too late.
Do we need the person's permission?
Yes. Personalised ads need consent. Check it on every request.
How exact must billing counts be?
Very. Lose no more than a minute of events, and never count one twice.
The traffic day
Consumer traffic, busiest in the evening. The clock covers 00:00 to 24:00 in the home region. Events add pressure on top of that normal traffic.
11:00 · Viral spike
A post blows up. Traffic climbs to several times normal within minutes.
Traffic reaches 4 times the scheduled load; rises over 600 seconds, holds for 3,600 seconds and eases over 3,600 seconds.
14:00 · Slow dependency
An outside service slows down. Your servers fill with requests waiting on it.
Added response time: ≈ 3,000 ms. Duration: ≈ 1,800 seconds.
16:00 · Cost blowout
A crawler hits uncached pages all day, and every pay-per-use part of the bill multiplies.
Traffic for Read, Search reaches 1.5 times the scheduled load; rises over 600 seconds, holds for 21,600 seconds and eases over 600 seconds. Other request types keep their scheduled load. Billed usage multiplier: 3. Duration: ≈ 21,600 seconds.
20:00 · Compliance audit
An auditor checks your design: who did what, encryption keys, personal data, backups and consent.
What a strong design has to handle
- You have about a quarter of a second. Which steps must happen in that time, and which can happen afterwards?
- Bound auction latency and account for billable events exactly once.
- A bigger model picks better ads but takes longer and costs more. A smaller one on time earns more than a perfect one late.
- Longer cache times for campaign data save money but let a paused campaign keep showing for a minute.
Make a prediction, run a full traffic day, then compare the result. The in-game editorial opens after a finished day and includes measured reference designs.
Concepts to practise
Related problems
- Medium
Recommendations
Every page shows a row of things the visitor might like. A ranking model builds each row, but the model is slow and costly. The row must appear instantly, even when the model is having a bad day.
- Hard
AI chatbot serving
Millions of people ask your assistant questions. Each reply takes a GPU several seconds, and GPUs are expensive and sometimes simply unavailable. Keep replies flowing, protect your wallet from abuse, and have a plan for when the GPUs run out.
- Easy
Semantic search
A help center lets people type a question in plain words and finds the right articles even when the words differ. Each question is turned into a list of numbers (a vector) and matched to similar articles. Many people ask the same few questions.
- Easy
URL shortener
People paste a long web address and get back a short one. Millions of other people then tap that short link, so reading is almost everything. Keep every tap fast, and never lose a link.