AlgoViz

Design Uber/Lyft

hard 55 min

Build a service like Uber or Lyft: a rider asks, a nearby driver comes, both watch the map, and the fare is paid.

Video lesson · Chapter 1 of 5

Two riders can see the same nearby driver

0:00 / 3:53

Theory

Read or listen first. About 2 min.

What it is

A ride hailing service connects people who want to travel with drivers who are nearby and free. It follows each trip from the request to the receipt.

This page designs a service like Uber or Lyft as an exercise. It is our own design. It does not claim to describe how those companies' systems work.

Why it exists

A failed post on a social network is annoying. A person left standing on a dark street is a safety problem. So the part that finds and keeps a driver must survive failures that other products could ignore.

The problem mixes very different kinds of data. Positions change every few seconds and may be lost. Trips and payments change rarely and may never be lost. Seeing that difference early is most of the design.

A short history

  1. 1955

    Harold Kuhn publishes the Hungarian method, which finds the best pairing between two groups. Matching many riders with many drivers is the same question.

  2. 1984

    Antonin Guttman describes the R-tree, an index of nested rectangles used by spatial databases.

  3. 2009

    Uber is founded in San Francisco, first as Ubercab.

  4. 2012

    Lyft starts as a service of Zimride.

  5. 2017

    Uber creates the public repository of H3, a grid of hexagons over the whole earth.

Words to know

Dispatch
Choosing a driver for a ride and offering it to them.
Offer
A ride shown to one driver for a few seconds. It is accepted, declined or expires.
Claim
The step that takes a driver from free to offered, which only one request can win.
Cell
One small piece of the map. Drivers are listed by cell.
ETA
Estimated time of arrival, along real roads.
Surge multiplier
A number that raises prices in a zone while requests exceed free drivers.
Authorization hold
Money reserved on a card. It is captured later, or released.
Trace
The list of points a car passed during a trip.

Solve it like an interview

New to it? Start with Approach. Revising? Go straight to the mock.

  1. 1ApproachWhat this question tests, where it gets hard, and how to spend the time.
  2. 2ScopeAsk what changes the design, then commit to a short list.
  3. 3NumbersWork out a number when the answer changes what you build.
  4. 4API and dataThe contract first, then the data that serves it.
  5. 5DesignStart with the smallest system that works. Change it only when something breaks.
  6. 6Deep divesCompare the options before you open them, then say which you would pick.
  7. 7Explain your choicesAnswer first. Then compare your reasoning.
  8. 8By levelThe same question is judged differently for mid, senior and staff.
  9. 9MockAnswer against the clock, then score yourself.

How it works

Step through the idea, or press Play.

Step 1 of 3

Locate. Drivers report a position every four seconds

Understand it

Rickshaws on the town square
ABCDEFGH123451234empty slateWho is near?

The slate helps to find. The flag decides.

Scope

What are we building?

A rider enters a destination, sees a price, and asks for a ride. We find a nearby driver within a minute. Both see each other on a map until the trip ends. The card is charged and both give a rating.

  • Find a driver

    Free, close by road, right kind of car, and only for one rider.

  • Follow the trip

    From request to receipt, through tunnels and dead phones.

  • Price it

    A quote before booking that holds for five minutes.

  • Pay once

    A hold first, one charge at the end, even after retries.

The hard part

A map that moves, and a driver who cannot be shared

Two million drivers report every four seconds, which is 500,000 changes a second. By the time you read a position, it is old. That is fine for finding candidates. It is not fine for deciding who gets the driver. So the design has two halves: a fast, forgetful index that suggests, and a small, careful database that decides.

Suggest, then decide

If the claim is lost or the offer is refused, the next candidate is tried.

The finished system

Where the design ends up

This is the last version for one region. The Design tab starts from one service and one database, and shows what forces each extra box.

The whole system in one region

Riders reach an API gateway and drivers keep a connection to a live gateway. The live gateway sends positions to the location service, which writes Redis. The API gateway calls the ride service, which writes PostgreSQL. The ride service asks matching for a driver. Matching reads candidates from the location service and travel times from the ETA service, and the claim is made in PostgreSQL. Pricing makes quotes. Changes flow through Kafka to workers for payment, notices and traces in object storage.
Blue is the ride, pink is positions, orange is the stream of events. Dashed arrows are not waited for.

Summary

The whole topic in one minute.

Positions are many, short-lived and may be slightly wrong, so they live in memory by city. They only suggest drivers. Claiming a driver is a conditional change in the ride database, and only that change decides.

Everything after the claim follows a state machine. Each change is saved first and announced second, so payment, messages and traces can be late, repeated and rebuilt without hurting the ride.

  • Positions suggest. The database decides.
  • Claim a driver with one conditional change, and give the claim an end time.
  • Compare drivers by time on the road, not by distance.
  • Save each state change before announcing it, and make every handler safe to repeat.
  • One city lives in one region, so a ride never needs an agreement across oceans.

Sources