SD-30 · Back to the write-up
Design a Price Alerts
React to each price change on a stream, find the alerts it meets with one range read, mark them fired, and let a queue of senders deliver each one once.
Follow a request
Create an alert
A price drops
A price bounces
A drop with 400K watchers
A sender crashes
Sources
API and stream
Matching
Data and delivery
ShopperApp or web
Pricing serviceEvery change
Alert APICreate, list
Price streamKeyed by product
MatchersRange read, fire
Notify queueJobs to send
Alert DB8 shards by product
Send logDedupe, caps
SendersPush and email
Push + emailProviders
The numbers, at a glance
1 fires ≈ 10,000/s4 fire nothing
2,300/sAverage
5,000/sPeak
50,000/sSale day
1% of alerts fire99% would be read for nothing
A price drop, in order
The matcher commits its stream offset only after the jobs are on the queue, so a crash means a retry, never a lost alert.
Pricing service
Price stream
Matcher
Alert shard
Notify queue
Sender
Provider
1 p_77: 349 → 299, v=812
2 poll partition 12
3 change
4 UPDATE armed → fired, target ≥ 299
5 3 alerts
6 3 jobs
7 commit offset
8 receive job
9 push, collapse key a_91
10 accepted
11 delete job
RequestResponseAsync, does not block
How a change finds its alerts
The decision this question is really about.
every 5 min
scan 200M alerts
up to 5 min late
+ Simple.− Reads 200 million rows to find a few.
p_77 → €299
index: product 77, armed, target ≥ 299
fire 3, enqueue 3
+ Work grows with changes, not alerts.− Big drops return many rows: page them.
sorted targets per product
binary search
still fire through the shard
+ Microseconds per change.− Rebuild 200M alerts on every restart.
How it grows
Add a box only when a number forces you to. Highlighted boxes are new at each stage.
ShopperPricing service
Alert APIPoller job
Alert DBEmail provider
ShopperPricing service
Alert APIPrice stream
Matchers
Alert DB + indexPush + email
ShopperPricing service
Alert APIPrice stream
MatchersNotify queueSenders
8 alert shardsPush + email
ShopperPricing service
Alert APIPrice stream
MatchersNotify queueSenders
8 alert shardsSend logPush + email
Was this walkthrough useful?Your vote tells us what to improve next.