BOOK DEEP READ · System Design Interview Vol 2 · Ch 3

Design Google Maps

System Design Interview Vol 2 · Ch 3 · Alex Xu & Sahn Lam · 2022

中文 →

What is this chapter about?

You pull out your phone at an intersection, open the map, and within two seconds the streets fill the screen. Tap navigate and it says "23 minutes, and the freeway saves you 6." This chapter builds that from nothing: where the map comes from, how the route is found, and where that "23 minutes" actually comes from.

A strange thing first

A system used by a billion people a day, and most of what it shows you is not computed when you ask. The map on your screen is a pile of small square pictures drawn long ago and parked on a disk — they have nothing to do with who you are or when you opened the app. Exactly one thing genuinely has to be worked out live: how bad the traffic is right now.

Why the old way breaks

Two things that do not fit. First, if the world map were a single image, it would be far too large for any phone to download. Second, all the roads on Earth form one network with hundreds of millions of junctions, and to find a shortest path through it, that network will not fit in one machine's memory. The difficulty is not that the computing is slow. It is that the thing is too big to move.

The core idea: cut it up, then layer it

Cut the map into squares. Slice the world map like a sheet cake and store each square as its own picture. Your screen is only so big, so you only ever need the few squares around you — a few hundred kilobytes — and the millions of other squares are none of your business. Each square is also kept at several levels of detail: looking at a whole province gets you the coarse set (freeways and big cities only), and only when you zoom into a block does the fine set arrive.

Finding a route works the same way: big map first, small map later. It is how people give directions. Work out how to get onto the freeway (that needs detail), drive the freeway all the way to the target city (for that stretch you only need a very coarse map — nobody cares about the side streets you pass), then exit and look at streets again. A machine does exactly this: for that long middle stretch it loads only "the map with nothing but freeways on it", so it never has to haul the whole road network into memory.

Whether it's jammed is something everyone's phones say together. Every phone with the map open reports, anonymously, "here's how fast I'm moving right now." Thousands of those dots, stitched together, are a live traffic picture. Google described this publicly back in 2009, and their own wording was that the more people take part, the better the traffic reports get for everybody.

What it buys you, and how to choose

One line: draw the things that don't change ahead of time and move them to the data centre nearest you; only compute the things that do change. A map barely changes in a year, so it can ship as static pictures for almost nothing. Traffic changes every minute, so it must be computed live — and it is the only part worth that money. The cost deserves saying plainly too: that useful live traffic is paid for with everyone's location data. It is anonymised, which is not the same as free.

One line to remember

A map service = a pile of pre-cut square pictures + a road network layered coarse-to-fine and loaded on demand + a traffic picture everyone contributes to. The hard part was never the algorithm. It is how you cut down something too big to move.

Want the mechanisms, the numbers and the diagrams? → switch to the deep read