Skip to content
SD-54 · Back to the write-up

Design a Ads Management and Delivery

Choose ads from an in-memory index with two-stage ranking and a second-price auction, spend leased budget slices, and bill each impression once from a deduplicated stream.

Follow a request

Clients
Services
Models, money and data
AdvertiserCampaigns
Phone appFeed + beacons
Event collectorChecks tokens
Ads managerCampaign API
Feed service80 ms timeout
User featuresFeatures + caps
Event logBy campaign
Campaign DBSource of truth
Ad serversIndex + auction
Budget serviceLeases + pacing
Stream processorDedupe + spend
Index builderDelta every 5 s
Ranking modelGPUs
Billing ledgerAppend-only
ReportsColumn store
1phone → feed · GET feedA user scrolls. The app asks the feed service for the next page of posts.1 / 7

The numbers, at a glance

For every 10 ads retrieved……only 1 reaches the heavy model
200 scored on GPUs1,800 cut by the light model
Where the 80 ms goesTypical ad decision, in milliseconds
Events that must not be billedRepeats and fakes before dedupe
≈ $1.4M a day if billed. Dedupe by impression id stops it.

An ad decision, in order

The beacon comes later, from the phone, through the collector and the log.

Feed service
Ad server
User features
Ranking model
Budget service
1 ad request, 2 slots
2 GET u_81
3 features + caps
4 score 200
5 pCTR × 200
6 2 ads + tokens
7 lease when slice low
RequestResponseAsync, does not block

How budgets are enforced

The decision this question is really about.

Counter per impression
Hot keys
Serve ad
INCR spend:c_19
Big campaign = hot key
+ Exact.− 420,000 updates/s on the path.
Count later
Overspends
Serve ad
Stream sums spend
Pause after $10,000 over
+ Nothing on the path.− Stops seconds too late.
Leased slices
Our pick
Lease $5 slice
Spend locallyStream counts views
Refuse leases at budget
+ ≈ 420 calls/s; under 1% over.− Leased and spent to reconcile.

How it grows

Add a box only when a number forces you to. Highlighted boxes are new at each stage.

Stage 1 · ≈ 1,000 ad requests/sMake it work
AdvertiserPhone + feed
Ads managerAd servers
Campaign DBBudget counter
Query the database per request; one counter per campaign.
Stage 2 · ≈ 50,000 ad requests/sServe from memory
AdvertiserPhone + feed
Ads managerAd servers + index
Campaign DBIndex builderFeaturesRanking model
Targeting index on every server; two-stage ranking on GPUs.
Stage 3 · ≈ 300,000 ad requests/sCount every event
Phone + feedEvent collector
Ad servers + indexEvent log
Budget leasesStream processorLedger
Beacons to a log; spend summed in a stream; budgets leased in slices.
Stage 4 · 300,000 req/s + money you can trustTrust and reports
Phone + feedEvent collectorAd review
Ad servers + indexEvent log
Budget leasesProcessor + dedupeLedgerReports warehouse
Dedupe and fraud filtering before billing; ad review; reports warehouse.
Was this walkthrough useful?Your vote tells us what to improve next.