SD-25 · Back to the write-up
Design a Surge Pricing
Count drivers and riders per map cell as events stream in, publish a smoothed multiplier every 30 seconds, and price quotes from memory.
Follow a request
A driver moves cells
Price a ride
Recompute surge
Book with a locked quote
The pipeline stalls
Apps
Edge
Pipeline
History
Driver appPings every 4 s
Rider appAsks for quotes
Ingest gatewaysPoint → cell
Pricing serviceTable in memory
Event log64 partitions by cell
Pricing rulesCaps per city
Ride serviceChecks the token
Stream processor2-min windows
Surge storeCurrent multipliers
Audit archiveEvery version
The numbers, at a glance
1 quote ≈ 40,000/s9 pings ≈ 360,000/s
400,000Raw
80,000Cell events
6,700Changed cells
32 MB table1.15 TB of events a day
A quote, in order
The multiplier is read from memory; the demand event is written without waiting.
Rider app
Pricing service
In-memory table
Event log
1 POST /quotes
2 7 cells near pickup
3 1.3×–1.5×, v812
4 quote event
5 fare + signed token
RequestResponseAsync, does not block
How surge is computed
The decision this question is really about.
INSERT ping
cron every 5 min
surge 5 min old
+ Plain SQL.− Every ping a write; 5 minutes stale.
POST /quotes
geo query, 1 km
price jumps per quote
+ Exact for that moment.− 40,000 geo queries/s; no demand counted.
event by cell
2-min windowsmooth + cap
table in memory
+ Fresh in 40 s; reads from memory.− A pipeline to run.
How it grows
Add a box only when a number forces you to. Highlighted boxes are new at each stage.
Apps
App server
DatabaseBatch job
Apps
GatewaysPricing
Event log
Stream processorSurge store
Apps
GatewaysPricing + memory table
Log × 64 partitionsPricing rules
Stream processorSurge store
Apps + heat map
GatewaysPricing
Event logRulesForecast
Stream processorSurge storeArchive
Was this walkthrough useful?Your vote tells us what to improve next.