Design Uber (ride hailing)
Match riders with nearby drivers in seconds while millions of drivers report their location every few seconds. The core problems: storing and searching live locations, matching by ETA, surge pricing and a trip state machine you can trust with payments.
Builds on these concepts
In one sentence
Keep live driver locations in an in-memory geo index, match by ETA in the rider's area, track every trip as a state machine in a durable database, and stream GPS pings for everything else.
A reference design: it covers what an app like this needs and follows how these companies have described their systems publicly, simplified to be learnable.
Requirements
Functional
- Rider sees a fare and requests a ride
- Nearby driver gets the offer and accepts
- Both follow each other live on the map
- Payment when the trip ends, ratings
Non-functional
- Match in seconds
- Very high write rate of GPS updates
- Never lose or double-charge a trip
- Highly available: a down region strands people
Back-of-the-envelope estimates
| Metric | Estimate |
|---|---|
| Drivers online at peak | ~5M, each pinging every 4 s → ~1.25M location writes/s |
| Trips | ~25M / day ≈ 300 trips/s created |
| Location record | ~100 B → only ~500 MB to hold every driver's current position: it fits in memory |
| History of pings | ~100 TB / year if you keep them all: a stream, not the main DB |
The striking number is the location write rate. That's why current positions live in memory and only trips go to a durable database.
API and data model
POST /fare-estimates { pickup, dropoff } → { fareId, price, surge }
POST /rides { fareId } → { rideId, status }
WS driver → location { lat, lng, heading, ts } every ~4 s
WS rider ← ride.update { driverLocation, eta, status }
POST /rides/:id/accept (driver)
trips(id, rider_id, driver_id, status, pickup, dropoff, fare, created_at)
drivers_now: driver_id → (lat, lng, status) -- in-memory geo indexArchitecture
Step by step · Request a ride
- 01Sets pickup and destination, sees the fare
- 02POST /rides
- 03Fare estimate × surge for this area
- 04Looks for drivers nearby
- 05GEOSEARCH: available drivers within 3 km
- 06Real ETA for each candidate
- 07Offer to the best driver: 15 s to accept
- 08Trip created: accepted
- 09Trip saved
- 10"Your driver is 4 min away"
At this scale · 1M users
Separate services for location, matching and trips
- ▸Location service ingests GPS pings and streams them through Kafka
- ▸Dispatch matches riders and drivers using ETAs, not straight-line distance
- ▸Pricing computes surge per area from supply and demand
Tap any box to see what it does.
Deep dive: finding drivers nearby
"Drivers within 3 km" can't scan millions of rows. You need a spatial index that divides the map into cells:
| Technique | How it works | Used for |
|---|---|---|
| Geohash | Encodes lat/lng as a string; nearby points share a prefix | Simple radius search in Redis or a DB |
| Quadtree | Recursively splits busy areas into 4 smaller squares | Adapting to dense cities vs empty countryside |
| H3 (hexagons) | Uber's hexagonal grid: every neighbour is at the same distance | Matching, surge areas and sharding by cell |
Straight-line distance lies: the closest driver might be across a river. Dispatch takes a handful of candidates from the index and asks the routing engine for real ETAs before choosing. The offer goes to one driver with a timeout; if they don't accept, the next one gets it.
How it changes with scale
Step by step · Driver location update
- 01Sends lat/lng every ~4 seconds
- 02UPDATE drivers SET location = …
- 03Spatial index updated
At this scale · 100 users
One app with a geo query
- ▸Drivers send their position; it's saved in PostgreSQL + PostGIS
- ▸Matching is a query: nearest available drivers within 3 km
- ▸The rider's app polls every few seconds for the car's position
Tap any box to see what it does.
| Users | What you run | Why it changes |
|---|---|---|
| 100 | App + PostgreSQL/PostGIS, polling | A spatial index query is fast enough for a few drivers |
| 10K | Redis geo index, WebSockets, LB | GPS writes would overload the main database |
| 1M | Location, dispatch, pricing and trip services + Kafka | Each part has a very different load and failure profile |
| 100M+ | Geo index and trips sharded by area (H3), active-active regions | Cities are independent: shard by geography and survive region failures |
Key takeaways
- 01Current locations are a high-write, in-memory problem; trips are a durable, transactional one.
- 02Use a spatial index (geohash, quadtree, H3) and refine candidates with real ETAs.
- 03Push live updates over persistent connections instead of polling.
- 04Geography is a natural shard key: cities rarely interact.
Check your understanding
1 of 3
In a ride-hailing app like Uber, where do live driver locations live?