Ch.33: Google Maps System Design: The Reroute Race
Transcript
0:00 Take a Google Maps driver 90 seconds from a closed ramp. Her phone still says, “Keep right.” That is a navigation failure. Why is the instruction still on her screen? Because the road map and active trip are separate records. The ramp can be closed while route 17 still owns her next instruction. So the closure can be true while the phone is wrong. Our fictional 10-million-session design has one job: find the exposed trip, let one revision win, and never move backward. That one-job contract opens the complete map.
0:33 Hold onto it for a moment. The top path runs from the phone and session gateway through the route service and cache to a versioned road graph—intersections as nodes, directed road segments as edges. Below, road-event validation, impact lookup, reroute workers, a regional commit shard, and the push gateway complete the loop. We'll unpack those regions, then return to this exact map at the end. Okay, so the client, road-state path, and reroute path are on the board. Before choosing what each box does, what does the driver actually need?
1:05 Exactly. This is the destination, not the argument. The driver sees one instruction, while the backend has road state, active-trip state, and delivery state. Requirements must tell us which races can never leak to her. Then freeze that driver failure first. Which outcomes are non-negotiable? Now the ramp-race functional requirements are: accept the closure, find sessions still using the ramp, replace only a route that is still current, and never let the phone move backward. The closure worker starts from route 17.
1:36 Before it finishes, a missed-turn worker installs 18. Then the stale closure result arrives. Oof. Disconnecting doesn't end the stale-state problem; reconnect creates a second race. Reconnect is not replaying every missed update. It means: tell me the truth now. That reconnect race leads to the non-functional requirements, and they put a clock on correctness: accepted closures reach online affected drivers within 5 seconds at the 95th percentile. That covers loading current session state, verifying impact, searching for a detour, and delivery. Mm-hmm. 5 seconds across the whole circuit, not per service. An outage cannot erase guidance already on the phone, and ended trips must leave the live index.
2:20 A correct reroute after the split is still late. Yeah. A perfect route to the ramp you just passed is still wrong. 90 seconds is the danger window. That latency budget narrows the problem: hit the deadline while finding only the trips still heading toward that ramp. With those requirements, we tend to draw the blue line as rendered output. It feels finished, not queryable. But then how could the backend tell whether the current route still hits the ramp? The line itself has to survive as queryable state.
2:52 Store the next few dozen road-segment IDs in order, route revision 17, its expiry, and two or three precomputed alternatives. The active session points to that bounded record, so one changed segment can be checked against what still lies ahead. Oh, right—the route is not just pixels anymore. It becomes data we can interrogate. One segment changed. What can answer which live routes still depend on it without scanning everything? That dependency question leads to a deliberately imprecise index. On a whiteboard, we usually reach for one row per segment because exact looks safe.
3:26 Instead, the session service writes temporary entries in coarse geographic tiles—map regions—with session ID, route revision, and expiry. Hold on. I can hear a storage engineer in an architecture review already: “Why accept false positives instead of an exact edge-to-session index?” Yeah. I get why exact feels safer on the whiteboard, but honestly, that proposed exact reverse index moves maintenance cost onto every position update. Coarse tiles keep location writes cheap; exact impact verification happens only when a road changes.
3:59 For example, one tile might wake 10,000 sessions, then exact checks cut that to 200 instead of scanning 10 million. The coarse-index layer moves precision to road changes, not each position update. Tiles still create false positives. For example, route 17 and another trip might share a tile on different streets. Yeah. The tile lookup produces candidates, never truth. Each worker loads the latest session revision and checks its remaining road segments. For example, if the ramp is behind the car, or a later revision removed it, the worker stops. Nice. Fire alarm by floor: wake an area, check the rooms.
4:36 The alarm produces candidates; the authoritative session row decides. But what if the road changes after verification? Then the room-check analogy hits the next race: verification itself can expire while a responder computes. Walk me through it. Imagine a worker computing 18 from the current road revision. Before commit, another event blocks its detour and advances the revision. The session row is unchanged, so a session-only check accepts a stale plan and sends the wrong instruction. Yeah, that's nasty.
5:06 Our fictional authority boundary is that regional decision shard: the session-store partition for her current lookahead. Trusted road events and route-commit commands enter one ordered log. Its local transaction accepts 18 only if the route remains 17 and the checked map revision still matches. So if the closure is ordered first, 18 fails and the worker reloads. If the proposal wins, the later closure triggers the next reroute. Authority order, not worker finish time, chooses. But what stops the network from delivering 18 after 19?
5:38 So the network-delivery question leads straight to a second guard: a full route snapshot carries its revision. Google publicly documents an opaque route token that lets its Navigation SDK reconstruct a route while preserving the original routing intent. Our fictional snapshot adds ordering: a phone on 19 discards 18, while a phone on 16 can install 18 directly. Right—the network can lose the same race. Letting arrival order choose is a navigation lottery. The phone acknowledges revisions; on reconnect, the gateway sends the current snapshot first.
6:14 Source links are in the description. Exactly. The phone needs its own vote. The database rejects stale decisions; the client rejects stale deliveries. Ordering rejects old routes; it cannot decide whether slower and closed mean the same thing. That contrast opens validity versus optimization. A closure makes the segment unusable; speed samples only alter travel time. Google's Routes API exposes the cost: its exhaustive traffic-aware mode has higher latency than its optimized mode. A live deadline cannot buy unlimited search.
6:43 That's the distinction. A closure is a locked door; traffic is a slower checkout line. The road still works, but it might cost two extra minutes. Honestly, I think traffic should trigger a switch only when the gain clears our fictional 2-minute threshold and the first maneuver stays safe. Smaller estimate changes would just flip the route back and forth. Right. You cannot optimize a road back into existence. I think I over-focus on freshness, but a tiny ETA gain is not worth interrupting a safe maneuver.
7:15 So after a hard closure, do we search the whole city again? Not immediately. Cached is a head start, not permission to trust yesterday's route. First test the alternatives saved with 17 against the newest graph. And if the cache has nothing safe? Then search outward from the closed ramp through nearby roads within the remaining latency budget. If that finds nothing safe, run the full city-wide search. The shortcut saves work; the fallback protects correctness. Now run it on one driver. For example, she stays on 17 when the closure advances map state.
7:49 Tile seven wakes her session, and an exact check proves the ramp is still ahead. Right. A worker validates a saved alternative; before the write, a second closure invalidates it, so road authority moves forward. That stale proposal loses. What happens next? It reads the updated road connections, chooses a clear detour, and the shard atomically moves the session from 17 to route 18 against the newest accepted map state. Then the phone disconnects before delivery. And reconnect reads the winner, not the push history?
8:20 Exactly. The gateway reads committed snapshot 18, and the phone on 17 installs it. If an off-route worker had already committed 19, the older write would fail; its reconnect snapshot would be 19. Exactly. Stale road state loses before commit; stale delivery loses at the phone. She hears “Continue straight” before the split. With the driver past the ramp, the returning architecture has three guardrails. Validation protects road truth. Exact verification protects the target trip. Revision checks keep storage and phone moving forward.
8:51 Road truth, trip truth, route order. Yeah. Three backend fights, one safe instruction. From the driver's seat, it is just a detour. Thanks for listening to Learning Podcasts.