Design Maps and Navigation

How to design a maps app: map tiles on a CDN, fast routes on a huge road graph, live traffic from phone locations, and rerouting.

Case study23 minLesson 4 of 8
On this page
  1. Clarify the problem
  2. Estimate the scale
  3. Define the API (optional)
  4. Model the data
  5. High-level design
  6. Deep dives
  7. Bottlenecks and failures
  8. What a strong answer sounds like
  9. Follow-up questions

A maps and navigation service shows a map, finds the fastest driving route between two places with an estimated arrival time, and guides the driver while adjusting to live traffic. It is hard to design because it combines three different workloads: serving a very large amount of static map data, running shortest-path searches on a graph with hundreds of millions of junctions, and turning location reports from moving phones into traffic data.

A strong answer keeps the three apart and goes deep on one or two. The steps below follow A Framework for Any Design. Try each "Your turn" box before you read the section after it.

Clarify the problem

The first five minutes
Interviewer

Design a maps and navigation service.

You

I see three features: the map, a route with an arrival time, and guidance that uses live traffic. Are place search, walking and public transport in scope?

Interviewer

Driving only, and you can assume address search already exists and gives you coordinates.

You

What scale should I plan for: daily users, routes per day and phones navigating at peak?

Interviewer

Assume 100 million daily users, 100 million routes a day including reroutes, and 5 million phones navigating at peak.

You

Then the map is static content that a CDN can serve, routing has to be fast on a graph the size of a continent, and traffic is a stream of about a million location batches a second. If traffic fails, routing should fall back to typical speeds.

  • Functional requirements: a map the user can pan and zoom; the fastest driving route with an estimated arrival time (ETA); live ETA updates and faster-route offers while driving; and a new route when the driver leaves the planned one.
  • Non-functional requirements: tiles arrive in under 100 ms; a route is computed in under 200 ms; a new jam shows in ETAs within 2 to 3 minutes; routing works without live traffic; and location data is anonymised and kept briefly.
  • Out of scope: place search, walking and public transport, map editing tools and street-level photos.

Estimate the scale

  • Tiles. At 100 tiles per user per day, 10 billion tiles a day is about 116,000 a second, or 350,000 at a 3x peak. If the CDN answers 95%, the origin sees about 17,000 a second.
  • Routes. 100 million routes a day is about 1,200 a second, or about 3,500 a second at peak.
  • Location pings. Each of the 5 million navigating phones sends a batch of about 100 bytes every 5 seconds, which is 1 million batches and about 100 MB/s. The data is useful for only a few minutes, so it belongs in a stream, not in database rows.
  • Road graph. Assume 500 million junctions and 1.2 billion one-way road segments. A junction takes 8 bytes (two packed coordinates) and a segment takes 12 bytes (target, travel time and id), so the graph needs 4 GB + 14.4 GB, about 18 GB, which fits in one large server's memory.
Estimate: Vector tiles for the whole world

The world has 4z tiles at zoom level z. You store vector tiles for zoom 0 to 14, and the phone draws closer zoom levels itself. How much storage do the tiles need?

You know
  • 4z tiles at zoom z, for z = 0 to 14
  • About 10 KB per tile on average, because most ocean tiles are nearly empty

Show the working
  1. Zoom 14 alone has 414 ≈ 268 million tiles
  2. All levels together have (415 − 1) ÷ 3 ≈ 358 million tiles, because each level adds a third more than the sum of the levels above it
  3. 358 × 106 × 10 KB ≈ 3.6 TB

Estimate 3.6 TB (anything from 1 TB to 15 TB is a good answer)

Your turn: Define the API

Write the calls for loading the map, asking for a route and sending location while driving. How does the phone find out that a faster route exists?

Compare with a model answer

A tile is a cacheable GET addressed by zoom level, x and y. A route request is a POST with an origin and a destination. Pings are a POST with a batch of points, and since the phone sends one every 5 seconds, the reply can say that a better route is ready.

Define the API Optional

Optional in the interview. Sketch the API only if the interviewer asks, if an API choice shapes the design, or if time permits. Otherwise say "I'll skip the detailed API unless you want it" and go to the data model.

HTTP API
GET  /tiles/v812/{z}/{x}/{y}.mvt           served by the CDN, cached for days
  200  vector tile (roads, labels, areas)
  404  outside the map
 
POST /routes
  body: { "from": [52.37, 4.89], "to": [52.09, 5.12], "depart": "now" }
  200  { "route_id": "r_51x", "polyline": "...", "eta_s": 600,
         "steps": [ { "turn": "left", "road": "...", "at_m": 1200 }, ... ],
         "graph_version": 812 }
  404  no road connects the two points
  503  routing is overloaded; the app retries with backoff
 
POST /nav/{route_id}/pings
  body: { "points": [ [t, lat, lng, speed, heading], ... ] }   5 points, one a second
  200  { "eta_s": 540, "better_route": null }
  200  { "eta_s": 1020, "better_route": { "route_id": "r_52k", "saves_s": 300 } }

The decision worth arguing is how the phone hears about a faster route. A WebSocket per driver would mean 5 million open connections for a message that is rarely sent. Putting the news in the reply to the next ping costs nothing extra and arrives at most 5 seconds late, so it is the better fit here; realtime communication covers when push is worth its cost.

The tile URL contains the map version (v812), so a new map has new URLs, no CDN purge is needed, and a phone never mixes tiles from two builds.

Your turn: Model the data

For tiles, the road graph, live speeds and location pings, say where each one is stored and how it is read.

Compare with a model answer

Tiles are immutable files in a blob store behind a CDN. The road graph is built offline and loaded into each routing server. Live speeds are a map from segment to speed, each navigating phone has an active route record, and pings go into a short-lived stream.

Model the data

Graph, speeds and pings
-- road graph: built offline from map data, shipped as a file, loaded into RAM
junction   id INT, lat INT, lng INT                 -- 8 B packed coordinates (id is the array position)
segment    from INT, to INT, segment_id INT,
           length_m INT, road_class TINYINT, turn_rules ...   -- one row per direction
 
-- live speeds: key-value, overwritten every minute
live_speed       segment_id -> (speed_kmh, samples, updated_at)   -- about 16 B each
 
-- typical speeds: a weekly curve of 672 slots of 15 minutes. Segments share
-- a few thousand curve shapes, so each segment stores a curve id, not 672 numbers
typical_curve    segment_id -> curve_id                      -- 2 B each
curve            (curve_id, slot 0..671) -> speed_kmh
 
-- active routes: one per navigating phone, deleted when the trip ends
active_route     route_id -> (segments, eta_s, better_route)  -- a few KB
 
-- pings: a partitioned stream, kept for a few hours, no user id
ping       trip_token, t, lat, lng, speed, heading

Tiles never change and are read by key, so they are stored in blob storage behind a CDN.

The road graph must not be read from a database during a search: at 0.5 ms per round trip, 50,000 lookups take 25 seconds. The graph is read-only between builds, so each routing server keeps it in memory as compact arrays.

Only main roads receive enough pings to have a live speed: 100 million segments at 16 bytes is 1.6 GB, and the curve ids for all 1.2 billion segments add 2.4 GB. Every routing server holds both.

Pings are written once and read once, so they go into a stream. Each trip uses a random token instead of a user id, which keeps the data anonymous.

Your turn: High-level design

Sketch three flows: drawing the map, asking for a route, and turning pings into traffic that the next route uses.

Compare with a model answer

Tiles go from the phone to the CDN and, on a miss, to the tile store. Routes go through the gateway to routing servers that hold the graph and live speeds, and each route is saved as active. Pings go through the gateway into a stream, and an aggregator turns them into segment speeds.

High-level design

The tile flow is independent of the other two, and routing depends on traffic only through the live speeds, so a failure in one flow does not stop the others.

Tiles never touch the API servers. Routing servers hold the graph and live speeds in memory, and pings flow through a stream into the aggregator that produces the speeds.Phone app to CDN (GET tile); CDN to Tile store (on miss); Phone app to API gateway (route, pings); API gateway to Routing servers (POST /routes); API gateway to Active routes (better route?); Routing servers to Active routes (save, re-price); API gateway to Ping stream (ping batch), asynchronous; Ping stream to Traffic aggregator, asynchronous; Traffic aggregator to Live speeds (speed per segment); Routing servers to Live speeds (read each minute).Phone appCDNAPI gatewayTile storeRouting serversActive routesPing streamLive speedsTrafficaggregatorGET tileon missroute, pingsPOST /routesbetter route?save, re-priceping batchspeed per segmentread each minute

request and replyasynchronous message

Tiles never touch the API servers. Routing servers hold the graph and live speeds in memory, and pings flow through a stream into the aggregator that produces the speeds.
Map, route, traffic and rerouteUse the arrows or ← → keys
  1. Map, route, traffic and reroute, step 1Phone app to CDN; CDN to Tile store; Phone app to API gateway; API gateway to Routing servers; API gateway to Active routes; Routing servers to Active routes; API gateway to Ping stream, asynchronous; Ping stream to Traffic aggregator, asynchronous; Traffic aggregator to Live speeds; Routing servers to Live speeds.Phone appCDN19 hitsAPI gatewayTile storeRouting serversActive routesPing streamLive speedsTrafficaggregator

    1Mia opens the app. About 20 tiles fill the screen, and almost all of them come from the phone's cache or the CDN.

  2. Map, route, traffic and reroute, step 2Phone app to CDN; CDN to Tile store; Phone app to API gateway; API gateway to Routing servers; API gateway to Active routes; Routing servers to Active routes; API gateway to Ping stream, asynchronous; Ping stream to Traffic aggregator, asynchronous; Traffic aggregator to Live speeds; Routing servers to Live speeds.Phone appCDNAPI gatewayTile storeRouting serversActive routesPing streamLive speedsTrafficaggregator

    2She asks for a route. Any routing server can answer, because each one holds the whole graph and the latest speeds.

  3. Map, route, traffic and reroute, step 3Phone app to CDN; CDN to Tile store; Phone app to API gateway; API gateway to Routing servers; API gateway to Active routes; Routing servers to Active routes; API gateway to Ping stream, asynchronous; Ping stream to Traffic aggregator, asynchronous; Traffic aggregator to Live speeds; Routing servers to Live speeds.Phone appCDNAPI gatewayTile storeRouting servers50 msActive routesPing streamLive speedsTrafficaggregator
    trip time
    10 min

    3Precomputed shortcuts let the search settle about 50,000 junctions instead of 20 million, which takes about 50 ms.

  4. Map, route, traffic and reroute, step 4Phone app to CDN; CDN to Tile store; Phone app to API gateway; API gateway to Routing servers; API gateway to Active routes; Routing servers to Active routes; API gateway to Ping stream, asynchronous; Ping stream to Traffic aggregator, asynchronous; Traffic aggregator to Live speeds; Routing servers to Live speeds.Phone appCDNAPI gatewayTile storeRouting serversActive routesPing streamLive speedsTrafficaggregator
    trip time
    10 min

    4The route goes back to the phone with its turn steps and is saved as an active route.

  5. Map, route, traffic and reroute, step 5Phone app to CDN; CDN to Tile store; Phone app to API gateway; API gateway to Routing servers; API gateway to Active routes; Routing servers to Active routes; API gateway to Ping stream, asynchronous; Ping stream to Traffic aggregator, asynchronous; Traffic aggregator to Live speeds; Routing servers to Live speeds.Phone appCDNAPI gatewayTile storeRouting serversActive routesPing streamLive speedsTrafficaggregator
    trip time
    10 min
    ping batches per second
    1,000,000

    5While she drives, the phone sends 5 GPS points every 5 seconds. The gateway streams each batch, finds no better route, and replies.

  6. Map, route, traffic and reroute, step 6Phone app to CDN; CDN to Tile store; Phone app to API gateway; API gateway to Routing servers; API gateway to Active routes; Routing servers to Active routes; API gateway to Ping stream, asynchronous; Ping stream to Traffic aggregator, asynchronous; Traffic aggregator to Live speeds; Routing servers to Live speeds.Phone appCDNAPI gatewayTile storeRouting serversActive routesPing streamLive speedsTrafficaggregatorseg 7731: 20
    trip time
    10 min

    6A crash blocks the road ahead, and phones on that 3 km stretch report 20 km/h instead of 90. The aggregator matches their points to segments and averages each speed.

  7. Map, route, traffic and reroute, step 7Phone app to CDN; CDN to Tile store; Phone app to API gateway; API gateway to Routing servers; API gateway to Active routes; Routing servers to Active routes; API gateway to Ping stream, asynchronous; Ping stream to Traffic aggregator, asynchronous; Traffic aggregator to Live speeds; Routing servers to Live speeds.Phone appCDNAPI gatewayTile storeRouting serversActive routesPing streamLive speedsseg 7731: 20Trafficaggregator
    trip time
    10 min

    7Once enough phones agree, the aggregator writes the new speed. Each minute the routing servers load the changed speeds and recompute the affected shortcuts.

  8. Map, route, traffic and reroute, step 8Phone app to CDN; CDN to Tile store; Phone app to API gateway; API gateway to Routing servers; API gateway to Active routes; Routing servers to Active routes; API gateway to Ping stream, asynchronous; Ping stream to Traffic aggregator, asynchronous; Traffic aggregator to Live speeds; Routing servers to Live speeds.Phone appCDNAPI gatewayTile storeRouting serversActive routesbetter: 12 minPing streamLive speedsTrafficaggregator
    trip time
    17 min

    8The routing servers re-price only the active routes that cross a changed segment. Mia's route now takes 17 minutes, and a route around the crash takes 12, so the better route is saved.

  9. Map, route, traffic and reroute, step 9Phone app to CDN; CDN to Tile store; Phone app to API gateway; API gateway to Routing servers; API gateway to Active routes; Routing servers to Active routes; API gateway to Ping stream, asynchronous; Ping stream to Traffic aggregator, asynchronous; Traffic aggregator to Live speeds; Routing servers to Live speeds.Phone appsaves 5 minCDNAPI gatewayTile storeRouting serversActive routesPing streamLive speedsTrafficaggregator
    trip time
    12 min

    9Her next ping, at most 5 seconds later, receives the better route in its reply, so no push channel is needed.

  10. Map, route, traffic and reroute, step 10Phone app to CDN; CDN to Tile store; Phone app to API gateway; API gateway to Routing servers; API gateway to Active routes; Routing servers to Active routes; API gateway to Ping stream, asynchronous; Ping stream to Traffic aggregator, asynchronous; Traffic aggregator to Live speeds; Routing servers to Live speeds.Phone appCDNAPI gatewayTile storeRouting serversActive routesPing streamLive speedsstale: 6 minTrafficaggregatordown
    speed source
    typical

    10Now the aggregator fails. Speeds older than 5 minutes are ignored and routing uses the typical speeds for that hour, so routes still work but ETAs are less accurate.

1 / 10

Deep dives

Serving the map as tiles

The map is too large to send as one file, so it is cut into square tiles. At zoom level 0 a single tile covers the whole world, and each further zoom level splits every tile into four. A tile is named z/x/y, so the phone can work out for itself which tiles cover its screen and ask for them by name.

A pre-rendered image tile is a finished picture. The phone only has to show it, so it works on any device, and it is the right choice for a satellite photo layer. The drawbacks are that images for every zoom level down to street level take far more storage than vector data, labels cannot rotate because they are part of the picture, and a new map style means rendering every tile again.

A vector tile contains the geometry and labels as data, and the phone draws it. Vector tiles are stored only down to zoom 14, which is about 3.6 TB, because the phone can draw closer zoom levels from the same data. The phone can also rotate the map or switch to night colours without new data. The costs are that the phone does the drawing work and that the tile builder must simplify shapes for each zoom level. Vector tiles are the usual choice for a modern app.

Routing on a huge road graph

A route is a shortest path in which the weight of each road segment is its travel time. Dijkstra's algorithm gives the correct answer, but it settles junctions in a growing circle around the start until it reaches the destination. On a long trip that circle can hold 20 million junctions. At about 1 µs per junction, for a heap operation and a memory miss, one query takes 20 seconds, while the goal is 200 ms.

Searching from both ends or using A* shrinks the circle only slightly, so the work has to be done before anyone asks for a route. There are two well-known ways to do that. Contraction hierarchies rank every junction by importance and add shortcut edges that skip the unimportant junctions, so a query climbs up to the important ones, crosses the country and comes back down. Queries are extremely fast, but the shortcuts are computed from the travel times, so in the basic form every speed change means a rebuild. That suits a router whose weights rarely change.

Cells with border shortcuts split the graph into cells along natural borders such as rivers and motorways, so that each cell has only a few border junctions. The builder precomputes the fastest time between every pair of border junctions in a cell, and these become shortcut edges. A query runs Dijkstra on real roads inside the start and end cells and uses only shortcuts everywhere else. Using several levels of cells, a long query settles tens of thousands of junctions and the result is still exact for the current speeds. Queries are somewhat slower than with a full hierarchy, but when a speed changes, only the shortcuts of that segment's cell are recomputed.

start cell cells in between end cell shortcut: border to border start destination
The search follows real roads only in the start and end cells and uses precomputed border-to-border shortcuts in the cells between them.
Ways to find a route
OptionGood atBad atPick it when
Contraction hierarchiesVery fast queries, because shortcuts skip the unimportant junctions.In the basic form the shortcuts depend on the travel times, so new speeds mean a rebuild.Travel times rarely change, as in a walking or cycling router.
Cells with border shortcuts Usual defaultFast queries, and a speed change recomputes only the shortcuts of the cells it touches.Slower than contraction hierarchies, and the graph needs careful partitioning.Live traffic changes the weights every minute. This is the usual choice here.

A query that settles 50,000 junctions at 1 µs takes about 50 ms of one core, so 3,500 routes a second need 3,500 × 0.05 s = 175 busy cores, or about 20 servers with 16 cores at roughly half load. Each server holds the full 18 GB graph, because splitting it would turn each search into many network calls.

From pings to traffic and ETAs

A GPS point says where a phone is but not which road it is on, so the aggregator map-matches each run of points to the most likely chain of road segments, using heading and speed to tell a motorway from the road under it, and then keeps the recent speeds of each segment.

The stream is partitioned by map tile, so one aggregator instance owns all the pings of an area. A speed is published only when several phones agree, because one car stopped for fuel is not a traffic jam. A segment with few samples blends in its typical speed for that 15-minute slot of the week.

The ETA is the sum of the travel times of the route's segments. On Mia's 11 km route, the 3 km crash segment takes 2 minutes at 90 km/h and 9 minutes at 20 km/h, which moves the total from 10 to 17 minutes.

Rerouting

There are two reasons to compute a new route. The first is that the driver leaves the planned route. The phone detects this itself, when several points in a row are more than about 50 m from the route, and asks the server for a new route, which is fast and works on a poor connection.

The second reason is that traffic has made another route faster. Re-pricing all 5 million active routes every 90 seconds would take about 55,000 searches a second, roughly 15 times the normal route traffic, so the routing servers re-price only routes that cross a changed segment. They offer a switch only when it saves, for example, 2 minutes and 10% of the remaining time, so drivers are not told to switch back and forth.

If every driver near the crash receives the same faster route at once, the side road fills up within minutes and the offer becomes wrong. The fix is to offer the new route to only a share of the affected drivers at a time, so that the next minute of pings shows the effect on the side road.

Bottlenecks and failures

At 10x the load, there are 10 million ping batches a second and 35,000 routes a second. The stream and aggregator add partitions, and routing adds replicas: 35,000 × 0.05 s is 1,750 busy cores.

Component that failsWhat happensWhat you do
The tile storeTiles missing from the CDN fail to load, while cached areas still draw.Replicate the store across zones; tiles never change, so any copy is correct.
A routing serverThe requests it was handling fail.The gateway retries them on another server. Loading the graph takes a minute or two, so keep spares loaded.
The traffic aggregatorLive speeds stop updating and become stale.After 5 minutes, routing falls back to typical speeds. A restarted aggregator skips old pings.
A phone's network connectionThat phone's pings and reroute requests stop.The phone keeps guiding the driver along the route it already stored.

What a strong answer sounds like

LevelWhat the answer does
JuniorDraws a map service and a route service, names Dijkstra, and stores pings in a database.
Mid-levelServes tiles from a CDN, sees that plain Dijkstra is too slow, and streams pings into segment speeds.
SeniorSeparates the three systems, chooses cell shortcuts because traffic changes the weights, falls back to typical speeds, and handles flip-flopping and herding when rerouting.
Routing at scaleStrong answer
Interviewer

How do you compute routes at 3,500 requests a second?

You

Plain Dijkstra would settle tens of millions of junctions on a long trip, so I precompute the travel times between the border junctions of each cell. A query then settles about 50,000 junctions, which takes about 50 ms and needs about 175 busy cores at peak.

Interviewer

Why use cells instead of a full hierarchy, which gives faster queries?

You

Speeds change every minute. With cells, I recompute only the shortcuts of the cells where a speed changed, while a basic hierarchy would need a full rebuild.

Follow-up questions

How do you release a new version of the map data?

Load the new graph next to the old one on each routing server, and switch to it only when it is fully loaded. Each route records its graph version, so a reroute during the switch uses the same version.

How do you protect users' privacy?

Pings carry a random token for each trip instead of a user id, and the start and end of every trip are trimmed so that home addresses stay hidden. Raw pings are deleted after a few hours.

How would you support offline maps?

The phone downloads the vector tiles, a compact road graph and the typical speed curves for a region. It can then draw the map and compute routes locally, without live traffic.

How do you predict traffic for a trip that starts later or lasts an hour?

Live speeds are reliable only for the next few minutes, so the router blends them with the typical speed, and the further ahead a segment is, the more weight the typical speed gets.

Self-review: maps and navigation0 of 10

After your attempt, tick what you really did. Be strict: "I thought about it" is not "I said it out loud".

Before drawing

Numbers and interfaces

Design and depth

Failure