Library
38 min read

Design Uber

Two hundred thousand drivers are moving around a city, and every one of their phones reports where it is every four seconds. A rider opens the app and wants to see the nearest cars. Thirty seconds later they want exactly one of those cars, promised to them and to nobody else.

Driver appDriver gatewayGeo indexOffer timerRider appRider APIPricingMatching serviceTrip store

Report. Every four seconds each driver app sends its position over a connection it keeps open.

Look at the two halves of that picture. They are two different systems sharing a screen:

  • The driver half (driver app, gateway, geo index) takes fifty thousand location updates a second, and it is allowed to lose all of them, because a fresh one arrives four seconds later.
  • The rider half (rider API, matching, trip store) handles about a thousand ride requests a second, and it is never allowed to lose one, because a trip is money and a legal record.

Most candidates design the driver half and hand wave the rider half. The rider half is where the interview is.

The real Uber

This page builds the core of a ride hailing app like Uber: tracking moving drivers, finding the nearest ones, and giving exactly one of them to a rider. The numbers and choices below are interview assumptions, not Uber's own. Where the page says what Uber really does, it says so by name.

Step 1: Understand the problem#

Ask about the matching before you ask about the map. Finding nearby points on a map is a solved problem with a known answer. Deciding which driver gets which rider is where the real decisions are. Each row below settles one line of the design.

You askThey sayWhat it settles
How many drivers are online, and how often do they report position?A couple of hundred thousand in a large city, every four seconds.Fifty thousand writes a second into whatever holds locations. That number alone rules out a relational table with a spatial index and pushes you to an in memory grid.
How fresh does a driver location have to be for matching?Within a few seconds.Location is soft state. It can live in memory, be lost on restart and rebuild itself in four seconds, which removes durability from the hottest write path in the system.
Can two riders be matched to the same driver?Never.A serialisation point per driver, and an offer that locks the driver while they decide. This is the hard requirement, and it is not solved by the geospatial index.
What happens if a driver ignores the request?Move to the next one, within seconds.Offers with a short timeout and automatic reassignment, so the matching state machine has a timer in it. It also means a rider is waiting while you try three drivers in sequence.
Does the price change while the rider is looking?It is quoted up front and honoured.A pricing service that reads supply and demand per area on a slow loop, and a quote that is frozen into the trip. Recomputing at accept time is how you get a support ticket.
Is a trip record allowed to be lost?Absolutely not. It is money and it is a legal record.Two different storage systems with two different guarantees. Locations are disposable, trips are durable and transactional, and confusing the two is the most common mistake in this design.

What you are building, and what you cut#

Functional requirements
What the system does.
  • Drivers report location continuously. Fifty thousand writes a second, and none of them individually matter.
  • Riders see nearby drivers and request a ride. A read of the same index the writes go into.
  • Match one rider to exactly one driver. With offers, timeouts and reassignment. This is the actual system.
  • Track the trip to completion and record it. Durable, transactional, and the source of truth for payment.
Non-functional requirements
How well it has to do it. The numbers you commit to.
  • 200,000 concurrent drivers per city, 50,000 location updates a second.
  • Nearby drivers returned in under 200ms.
  • A driver is never offered to two riders at once.
  • A trip record survives any single component failing.
Out of scope
What you cut, and say so out loud.
  • Routing and ETA computation. It is a graph problem with its own literature, and here it is a call to a maps service.
  • Payments. The trip ends with a call to a payment rail, which has its own design page: Design UPI payments.
  • Pool and shared rides. A different matching problem entirely, and adding it doubles the page.
  • Fraud, incentives and driver supply positioning. Real, enormous, and not what is being asked.

Back of the envelope#

Location write pressure
200 thousand
4 seconds
1000
Location writes per second200k / 4s = 50,000
Ride requests per second1,000
Write to match ratio50,000 / 1,000 = 50 to 1
Live location state at ~120 bytes24 MB
Location writes a day if you stored them4.3 billion
50,000 writes/sec
against a working set that fits in the memory of one laptop

Those two numbers together are the design. 24MB of live state means the whole city fits in memory, and 50,000 writes a second means you must never touch a disk to record one. The moment somebody suggests persisting every location update, ask what reads it.

Read the last two rows together. Fifty thousand writes a second sounds huge, but the whole city's live state is about 24 megabytes, smaller than a few phone photos. That pairing is the design: everything fits in memory, and nothing on the hot path ever touches a disk.

The number people get wrong

Candidates size storage for location history and end up designing a time series database. Nothing in the matching path ever reads a location older than a few seconds. Keep the live position in memory, and if the business wants trip traces for analytics, publish them to a log the matching system never queries (the difference between a queue and a log is in queue vs pub/sub vs event stream). Two systems, two lifetimes.