skip to content
jaeaeichsysdes

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

GPS INGEST & LIVE TRACKINGMATCHING, LOCKING & DISPATCH4s GPSGEOADD + aliveGEOPOS O(1)SSE coordsPOST /rideGEOSEARCH + MGETSET NX EX 15pending -> acceptofferSSE 15s offerDriver AppGPS every 4sSSE offersRider AppPOST /rideSSE live maplocation svcingest & SSE track250K/s · O(1) GEOPOSRedis GEO (regional)GEOADD + user:{id}:alive12s TTL · single-shardbooking svcGEOSEARCH + 15s lock1 driver at a timeevent handlerSSE offer push15s accept / rejectRedis Driver LockSET driver:{id}:lock {ride}NX EX 15 · Lua releaseRides PostgresWHERE status = 'pending'sharded by ride_id

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 coordinates
SSE /events -> push 15s ride offers to drivers

How it works

1. Geo index & liveness (location -> regional Redis GEO)

  • Memory beats disk: Idle GPS pings are ephemeral overwrites (~100 MB RAM for 1M drivers), so skip durable disk writes and shard Redis by city/region for single-shard GEOSEARCH and blast-radius isolation.
  • Separate alive keys: Redis GEO is backed by a ZSET with no per-member TTL, so pair GEOADD with user:{id}:alive (12s TTL over the 4s heartbeat); filter GEOSEARCH candidates with one batched MGET and prune dead members via background ZREM.
  • 8-neighbor boundary rule: Adjacent points across a geohash grid line share no common prefix, so GEOSEARCH always checks the target cell plus all 8 neighbors.

2. Sequential 15s locking & conditional accept (booking -> Redis + Postgres)

  • One candidate at a time: Insert a pending row in Postgres, then acquire a 15-second lease on one driver via SET 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 DELs driver:{driver_id}:lock only if its value equals ride_id so 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, location switches from GEOSEARCH to O(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?

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?

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?

esc
⌘B sidebar[ ] prev/next1–5 flowsc copyt theme