Design Ride-Hailing (Uber / Lyft)
Geospatial matching at 1M active drivers (250K GPS writes/sec): region-sharded Redis GEO indexes, separate alive-TTL filtering, sequential 15-second driver locks, conditional Postgres accepts, and O(1) SSE tracking.
The whiteboard
Trace GPS ingest, match & lock, and live tracking below — built from Distributed Locks & Fencing Tokens, Concurrency & Failure Modes, and Sharding & Hotkeys.
whiteboard · press 1–4 to trace flows
Three decoupled paths: ephemeral 250K/s GPS overwrites in regional Redis, sequential 15s locking with conditional Postgres accept, and O(1) GEOPOS tracking.1M active drivers fit in ~100 MB RAM — keep hot coordinates in memory and reserve Postgres strictly for durable ride state transitions.
Scale & APIs
250K / s
1M active drivers ÷ 4s heartbeat
~100 MB
ephemeral RAM for 1M driver coords
< 1 s
end-to-end match & dispatch
Constraints: < 1s matching · zero double-dispatch · regional fault isolation (city outages isolated)Data: User (sharded by user_id) | Ride (sharded by ride_id, Postgres)
POST /driver/heartbeat { lat, long } -> 200 OK (every 4s, ephemeral overwrite)POST /ride?lat=&long= -> 200 OK { ride_id }POST /ride/:id -> { decision: "accepted" | "rejected" }GET /live/:driver_id -> SSE stream of live coordinatesSSE /events -> push 15s ride offers to driversHow it works
1. Geo index & liveness (location -> regional Redis GEO)
- Memory beats disk: Idle GPS pings are ephemeral overwrites (
~100 MBRAM for 1M drivers), so skip durable disk writes and shard Redis by city/region for single-shardGEOSEARCHand blast-radius isolation. - Separate alive keys: Redis
GEOis backed by aZSETwith no per-member TTL, so pairGEOADDwithuser:{id}:alive(12s TTLover the 4s heartbeat); filterGEOSEARCHcandidates with one batchedMGETand prune dead members via backgroundZREM. - 8-neighbor boundary rule: Adjacent points across a geohash grid line share no common prefix, so
GEOSEARCHalways checks the target cell plus all 8 neighbors.
2. Sequential 15s locking & conditional accept (booking -> Redis + Postgres)
- One candidate at a time: Insert a
pendingrow in Postgres, then acquire a 15-second lease on one driver viaSET driver:{driver_id}:lock {ride_id} NX EX 15— broadcast-locking 5 drivers per rider starves peak-hour supply. - Owner-checked release: On reject, run an atomic Lua script that
DELsdriver:{driver_id}:lockonly if its value equalsride_idso a late reject never drops another ride’s lock; on timeout, the TTL auto-expires and advances to the next candidate. - Zero TOCTOU on accept: Seal the match with
UPDATE rides SET status = 'accepted', driver_id = $1 WHERE id = $2 AND status = 'pending'so a concurrent rider cancel or offer expiry cannot double-book.
3. Live trip tracking (O(1) lookup)
- Stop scanning radii: Once matched,
locationswitches fromGEOSEARCHtoO(1)GEOPOS(driver_id)on the regional Redis shard and streams coordinates to the rider over SSE.
lesson
The #1 Uber interview bug is putting ride_id in the lock key (driver:{id}:{ride_id}:lock), which lets two rides lock the same driver at once. Keep driver:{id}:lock as the key and ride_id as the value for mutual exclusion plus safe Lua release.
Check yourself
interactive · stress-test this ride-hailing design against classic Uber interview probes
01 · Lock key design bug
two concurrent rides · same driver · SET NX
A teammate writes SET driver:{driver_id}:{ride_id}:lock 1 NX EX 15 instead of SET driver:{driver_id}:lock {ride_id} NX EX 15. What breaks when two riders request a cab next to Driver 42 at the same millisecond?
Select an option to check the trade-off.
02 · Why sequential 15s locking beats broadcast locking
5 nearby drivers · high demand area · supply starvation
There are 5 nearby drivers. Why lock and offer to 1 driver at a time (15s TTL) instead of locking all 5 drivers at once and giving the ride to whoever taps Accept first?
Select an option to check the trade-off.
03 · Why GEOADD needs a separate alive key
Redis ZSET · per-member TTL · stale drivers
Drivers send GPS heartbeats every 4s. If a driver's phone dies in a tunnel, why isn't Redis GEOADD alone enough to remove them from GEOSEARCH results after 12 seconds?
Select an option to check the trade-off.
leasecoordination
Time-bounded exclusive ownership of a resource (like a machine ID or lock) that expires automatically if the holder crashes.