System design · Geospatial indexing, matching
How to design Uber
Keep each driver's latest position in an in-memory geospatial index fed by a stream of location writes, and assign a trip with an atomic claim so exactly one driver gets each rider. 2 million drivers reporting every 4 seconds is about 500,000 location writes a second against about 700 trip requests a second at peak.
Updated · 5 min read
A teaching design of a system like Uber, not Uber's real architecture.
The short version
- Location writes outnumber ride requests almost a thousand to one. Keep a fast, lossy location path apart from a strict, durable trip path.
- Current driver positions fit in memory, indexed by map cell, with a short TTL. Search the rider's cell and its neighbours.
- A conditional write on the driver (set current_trip_id only if null) guarantees one driver, one trip.
- Rides are local, so partition location, dispatch and trips by region.
Clarifying questions to ask first
- Which flows are in scope? (Assume location updates, nearby search, request, matching, trip tracking and a price quote with surge. No payments processing, fraud or support.)
- How fresh must a driver's position be? (Assume a few seconds: an update every 4 seconds, and a lost one is replaced by the next.)
- What scale? (Assume 2M drivers online and 20M trips a day at peak.)
- Do rides cross regions? (Assume no, so location data, dispatch and trips partition by region.)
- What must never happen? (Assume: a driver on two trips at once, or a rider charged for a trip that didn't happen.)
Numbers that shape the design
These are assumptions, stated out loud, not real figures for Uber.
| Quantity | Assumption | Result |
|---|---|---|
| Location updates | 2M drivers online at peak, one update every 4 seconds | ≈ 500,000 writes/s at peak |
| Ride requests | 20M trips a day, 3× peak | ≈ 230/s average, ≈ 700/s at peak |
| Nearby searches | 1M riders with the app open, refreshing every 5 seconds | ≈ 200,000 reads/s at peak |
| Location history | ≈ 100 bytes per update, 1M drivers online on average | ≈ 25 MB/s ≈ 2 TB a day, if kept |
All current positions take a few hundred megabytes and fit in memory. History is large but only for analytics and disputes, so it goes to cheap append-only storage off the hot path. Writing every update to the main database pays for durability nobody needs on a row overwritten four seconds later.
API
WebSocket /driver/ws
→ { "type": "location", "lat": 37.7749, "lng": -122.4194, "heading": 90, "ts": 1791273600 }
← { "type": "offer", "trip_id": "t_881", "pickup": { ... }, "expires_in": 15 }
→ { "type": "accept", "trip_id": "t_881" }
GET /api/drivers/nearby?lat=37.77&lng=-122.42
→ 200 OK { "drivers": [ { "lat": ..., "lng": ..., "heading": ... } ] }
POST /api/trips
{ "pickup": { ... }, "dropoff": { ... }, "quote_id": "q_52", "idempotency_key": "r-7c1a" }
→ 201 Created { "trip_id": "t_881", "status": "matching" }Drivers send so often that they use a long-lived connection. quote_id pins the quoted price against later surge. idempotency_key stops a retry creating two trips.
Data model
driver_locations (in memory, e.g. Redis)
cell:{cell_id} set of driver_ids in that cell
driver:{id} lat, lng, heading, status, cell_id, updated_at (TTL ≈ 30s)
trips (durable, sharded by region)
trip_id bigint primary key
rider_id bigint
driver_id bigint null until matched
status enum requested | matching | accepted | arrived | in_progress | completed | cancelled
quote_id bigint price locked at request time
version int for conditional updates
created_at timestamp
drivers
driver_id bigint primary key
current_trip_id bigint null when freeA driver whose phone loses signal drops out of search when the TTL expires, with no cleanup job.
High-level design
- Gateways hold driver connections and put updates on a queue partitioned by region, so a burst in one city doesn't slow another.
- The location service computes each driver's cell, moves them between cell sets on a boundary crossing and overwrites the position.
- Search reads the rider's cell and its neighbours from the in-memory index.
- Dispatch ranks candidates by ETA and offers the trip to one driver at a time. The trip service owns the trip state machine and durable record.
Geospatial indexing: geohash, S2 and H3
A latitude and longitude index can't answer "within 2 km" efficiently. Divide the map into cells instead and index drivers by cell.
- Geohash interleaves latitude and longitude bits into a base-32 string; a shared prefix means a shared containing cell, so any key-value store works. Nearby points across a cell edge can have very different hashes, so always search neighbouring cells.
- Google's S2 projects the sphere onto a cube's six faces and orders 64-bit cell IDs along a Hilbert curve, so nearby cells tend to have nearby IDs and range scans are efficient.
- H3, which Uber open-sourced, tiles the earth with hexagons at 16 resolutions. All six neighbours are equidistant from the centre, which makes rings around a rider and per-cell surge counts simple.
- Size cells so a typical one holds tens of drivers. If the rider's cell and neighbours return too few, widen by one ring.
Matching, surge and ETA
Two dispatchers can pick the same free driver. A conditional write that sets current_trip_id only if it is still null lets exactly one win; the loser moves to the next candidate. Offers go to one driver at a time with a short timeout, then to the next. Trip state moves by versioned conditional updates, so a late accept can't revive a cancelled trip. Keeping a region's dispatch on one set of nodes means no cross-region lock.
Surge is a per-cell ratio of open requests to free drivers over the last few minutes, smoothed so prices don't flicker, computed a few seconds behind from the same streams. ETA comes from a road-graph routing engine with live traffic: a driver 500 m away across a river may be the worst choice.
Common traps
- Adding a driver to a new cell without removing the old membership. Search returns the same driver in stale places.
- Overwriting a position with a delayed older update. Compare timestamp or sequence so the car never moves backwards.
- Partitioning nearby search only by driver ID. A rider doesn't know which IDs are nearby; keep a region-and-cell access path.
- Accepting a late driver response after the trip was cancelled. Trip state and version must still match the offer.
Follow-up questions interviewers ask
- "A node holding live locations dies." Searches there briefly see fewer drivers. Drivers resend within seconds and the TTL clears stale entries, so the index rebuilds itself. Trips stay durable.
Frequently asked questions
How does Uber find nearby drivers quickly?
+
Divide the map into cells with geohash, S2 or H3 and keep an in-memory index of drivers per cell. A search reads the rider's cell and neighbours, then ranks by estimated pickup time.
Should driver locations be stored in a database?
+
Not the hot copy. It's overwritten every few seconds, so it belongs in memory with a short expiry. Keep history in cheap append-only storage if you need it.
What is the difference between geohash, S2 and H3?
+
Geohash suits any key-value store. S2 suits range scans on cell IDs. H3 hexagons have equidistant neighbours, which simplifies radius searches and per-area counts.