Case study demo
Maps routing
Find a shortest path across a small road graph.
An interactive demo of how this system behaves. It runs in your browser.
Design walkthrough: Maps routing
A maps app finds the fastest way to drive from A to B. Roads are stored as a graph. Each junction is a point, and each road between two junctions is a link with a travel time.
Requirements
What it must do
- Return the fastest route between two points.
- Use live traffic, so the route avoids a jam.
- Give an arrival time and update it during the trip.
How well it must do it
- Answer in under a second, even for a route across a country.
- Traffic data may be a minute or two old.
Rough numbers
- A country's road graph has tens of millions of junctions. A plain search could visit most of them for one long route.
- If 1 million phones report every 5 seconds, that is 200,000 speed reports a second.
How a request flows
Read pathFind a route
- Maps app
- Routing API
- Route planner
- Road graph
- Live traffic
Maps app to Routing API
The app sends the start, the end and the travel mode.
Routing API to Route planner
The planner moves both points onto the nearest road.
Route planner to Road graph
It searches outward from the start, always extending the cheapest path so far. This is Dijkstra's algorithm.
Route planner to Live traffic
Each road's cost is its current travel time, so a jammed road costs more.
Route planner to Maps app
It returns the route, the distance and the arrival time.
Write pathLearn live traffic
- Phones on roads
- Live traffic
- Route planner
- Maps app
Phones on roads to Live traffic
Phones that are moving report their speed and position every few seconds.
Live traffic to Route planner
A stream job matches each report to a road. It averages the speed on each road over the last few minutes.
Route planner to Maps app
If a trip in progress now has a faster route, the app offers it.
Scaling it
- Work out shortcuts between major junctions ahead of time. A long route then uses a few shortcut links, not millions of small roads. One method for this is called contraction hierarchies.
- Split the map into regions. A route inside one city only loads that city's part of the graph.
- Cache popular routes for a minute or two, such as airport to city centre.
What breaks, and what happens then
The traffic feed stops
- What users see
- Live speeds go stale.
- What the design does
- Fall back to the typical speed for that hour and day. Routes are still good, only less exact.
A road closes suddenly
- What users see
- Routes still send drivers down it.
- What the design does
- Many phones slowing down or turning around is itself a signal. Mark the road as blocked until speeds recover.
Long routes overload a planner server
- What users see
- Short routes wait behind slow ones.
- What the design does
- Give each search a time limit, and send long routes to a separate pool of servers.
Trade-offs
- The exact shortest path, or a faster search that is close to it
- A good default
- Use shortcuts worked out ahead of time. Answers arrive in milliseconds.
- What it costs
- Shortcuts must be rebuilt when roads change, and live traffic is harder to mix in.
- Send everyone down the fastest road, or spread them out
- A good default
- Spread drivers across routes that are nearly as fast.
- What it costs
- Some drivers get a route that is slightly slower on paper.
Common mistakes
- Using distance as the cost. The shortest road is often not the fastest.
- Running a full search over the whole graph for every request.
Questions an interviewer might ask
How do you estimate the arrival time?
Add up the current travel time of each road on the route. Better systems also predict how traffic will change before you reach each road.
How would you support walking and cycling?
Use the same graph. Each mode has its own allowed roads and its own cost per link.
How are the map pictures served?
The map is cut into small tiles for each zoom level and served from a CDN. Routing is separate: it needs the graph, not the pictures.