Skip to content
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

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
1log → proc · window closesEvery 30 s, 10 s after the window end, each worker closes the 2-minute window for its cells: 9 riders, 4 free drivers in cell 8a20…1 / 6

The numbers, at a glance

For every quote……there are about 9 driver pings
1 quote ≈ 40,000/s9 pings ≈ 360,000/s
Events shrink on the way inPer second, at the global peak
The live state is tinyMultiplier table vs. one day of events
So pricing servers keep the whole table in memory, and history goes to the archive.

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.

Batch job
Too slow
INSERT ping
cron every 5 min
surge 5 min old
+ Plain SQL.− Every ping a write; 5 minutes stale.
Count on each quote
Wrong shape
POST /quotes
geo query, 1 km
price jumps per quote
+ Exact for that moment.− 40,000 geo queries/s; no demand counted.
Streaming windows
Our pick
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.

Stage 1 · ≈ 5,000 events/sMake it work
Apps
App server
DatabaseBatch job
One server, pings in a database, a batch job every 5 minutes.
Stage 2 · ≈ 25,000 events/sStream it
Apps
GatewaysPricing
Event log
Stream processorSurge store
Events into a log, windowed counts per cell, quotes on their own servers.
Stage 3 · ≈ 400,000 events/sScale and steady
Apps
GatewaysPricing + memory table
Log × 64 partitionsPricing rules
Stream processorSurge store
64 partitions, cell changes only, table in memory, smoothing and caps.
Stage 4 · 400,000 events/s, run for realRun it for real
Apps + heat map
GatewaysPricing
Event logRulesForecast
Stream processorSurge storeArchive
Forecast, audit archive and a driver heat map.
Was this walkthrough useful?Your vote tells us what to improve next.