The problem, stated properly
Design the system behind Uber's live map: millions of drivers continuously reporting GPS positions, riders watching cars move on a map in near real time, and a matching engine that answers "which drivers are within 2 km of this pickup point right now?" hundreds of thousands of times per second. Two workloads with opposite shapes share one pipeline: a write-heavy location ingestion firehose, and a read-heavy geo-query layer that must always answer about the present, never the past.
Scale first
- Location writes: ~5 million active drivers sending a GPS ping every 4 seconds is roughly 1.25 million writes/sec. Each ping is small (~100 bytes: driver ID, lat/lon, timestamp, heading), so ~125 MB/sec of raw ingest.
- Geo queries: riders opening the app, requesting rides, and the matching engine re-scanning generate ~500K nearby-driver queries/sec at peak.
- Live map fan-out: an active rider watching the map needs updated positions every few seconds for ~20 nearby drivers. With millions of concurrent viewers, that is tens of millions of tiny pushes per minute, which is why this flows over persistent connections, not polling.
The core indexing decision: how do you query "near this point" fast?
Latitude/longitude are two-dimensional, and a B-tree indexes one dimension well. Four real answers:
- Geohash: encode lat/lon into a string where shared prefixes mean shared areas (a 6-char geohash is a ~1.2 km x 0.6 km cell). "Drivers near me" becomes a string-prefix lookup, which any key-value store does in microseconds. Boundary problem: two points 5 meters apart can sit in different cells, so you must also query the 8 neighboring cells.
- Quadtree: recursively split the map into 4 quadrants, subdividing only dense regions. Great for uneven density (Manhattan gets tiny cells, farmland gets huge ones), but rebalancing as drivers move is expensive, so quadtrees live in memory, rebuilt periodically.
- Google S2: maps the sphere to a Hilbert curve, preserving proximity better than geohash at edges and poles. Used in production at Google and in many geo databases.
- What Uber actually built (H3): a hexagonal grid. Hexagons have a unique property: every neighbor is equidistant from the center (squares have diagonal neighbors that are farther, triangles are worse). Distance rings around a cell are uniform, which makes "expand the search radius" clean. H3 has ~122 billion cells at its finest resolution, and Uber's marketplace runs on it: surge pricing, dispatch, and ETA all aggregate demand per H3 cell.
The pipeline, step by step
- Driver app sends a ping every ~4 seconds over a persistent connection (or HTTP/2) to the nearest edge gateway.
- The gateway publishes the ping to a streaming pipeline (Kafka). Nothing is written to the trip database on the hot path; the live map is ephemeral state.
- A stream-processing layer updates the in-memory geo index: driver ID mapped to current H3 cell, held in a sharded Redis-style store keyed by cell. Old position is deleted, new one written, with a TTL (no ping for ~30 seconds means the driver vanished from the map automatically).
- Rider opens the app: the client sends its location, the geo service computes the rider's H3 cell plus neighbors, fetches driver IDs in those cells, and returns positions. The map updates over WebSocket every few seconds while the rider watches.
- The matching engine is a consumer of the same index: on a ride request it queries expanding rings of H3 cells (k=0, then ring 1, then ring 2) until it finds enough drivers, filters by car type and driver state, and dispatches.
ETAs: the second hardest problem on the screen
The "3 minutes away" number is not geometry. It is a model: road-network graph routing (contraction hierarchies make cross-city shortest paths answer in microseconds), adjusted by live traffic features (current speeds per road segment, computed from those same driver pings), plus learned corrections from historical trips. Raw GPS distance lies constantly: a driver 500 meters away across a river is 12 minutes away. This is why the ping firehose serves two masters: the live map you see, and the traffic model that prices and times everything.
Tradeoffs worth naming
- Accuracy vs battery and bandwidth: a 1-second ping interval doubles map smoothness and quadruples ingest cost and driver battery drain vs 4 seconds. 3-5 seconds is the industry sweet spot.
- Live state is in-memory, not in Postgres: writing 1.25M pings/sec to a relational DB would melt it for zero benefit, since a location is worthless after 30 seconds. Ephemeral in-memory stores with TTLs are the correct home; the trip DB only records pickups and dropoffs.
- Push vs pull for the rider map: push (WebSocket) gives smooth motion at high server cost per viewer; pull (client refetches every few seconds) is simpler and cheaper. Most systems push during an active trip and pull while browsing.
Production failure modes
- GPS noise and teleporting cars: urban canyons make GPS jump 50+ meters. Raw pings need a Kalman filter / map-matching pass (snap the point to the road graph) before display, or cars drive through buildings on the map.
- Hot cells: Times Square on New Year's Eve concentrates drivers and queries into a handful of cells. The geo index must shard by cell and spread load, or the busiest neighborhood melts the busiest shard.
- Clock skew: a driver phone with a wrong clock injects future timestamps. Servers must timestamp on arrival and treat client time as a hint.
- Ring-expansion misses: matching only k=0 cells fails riders at cell boundaries; always expand to neighbor rings, and expand wider in low-density areas (this is exactly why H3's uniform neighbors matter).
Common interview traps
- Proposing "SELECT * WHERE lat BETWEEN ..." on the trip database. Two-dimensional range scans on a B-tree at this write rate are the canonical wrong answer.
- Storing live locations in the durable database instead of an ephemeral in-memory store with TTLs.
- Forgetting the neighbor-cell problem for geohashes. It is the first follow-up every interviewer asks.
- Designing matching as a rider-side query. Matching is a marketplace process with its own state machine (offer, accept, timeout, next driver), not a read.
Interview Q&A
Q: Why does Uber use hexagons (H3) instead of geohash squares?
A: Neighbor uniformity. A square has 8 neighbors at two different distances (edge neighbors at 1x, diagonal at about 1.41x), so "everything within distance r of this cell" has no clean answer. Every hexagon neighbor is equidistant, so expanding a search ring by one hex adds a uniform band of area. That makes ring-expansion matching, surge polygons, and demand aggregation clean. Hexagons also approximate circles better, so radius queries waste less area. The cost: hexagons cannot be subdivided perfectly into smaller hexagons, which is why H3 resolutions are approximate and Uber keeps a fixed hierarchy.
Q: How do you keep the rider's map smooth without melting your servers?
A: Separate the write firehose from the read view. Ingest accepts every ping, but the rider's client receives updates only for its viewport's cells, every 2-4 seconds, over one WebSocket, with client-side interpolation animating between updates (the car glides even though data arrives in steps). Server-side, per-viewport cell subscriptions mean one ping update fans out only to viewers whose maps actually contain that cell.
Problem ko sahi se samjho
Uber ke live map ke peeche ka system design karo: lakhs of drivers lagatar apni GPS location bhej rahe hain, riders map pe gaadiyan chalti hui near real time mein dekh rahe hain, aur ek matching engine hai jo "is pickup point ke 2 km ke andar kaunse drivers hain abhi?" ka jawab lakhs of baar per second deta hai. Ek hi pipeline pe do opposite workloads: write-heavy location ingestion firehose, aur read-heavy geo-query layer jo hamesha present ke baare mein jawab de, past ke baare mein kabhi nahi.
Pehle scale
- Location writes: ~5 million active drivers har 4 second mein GPS ping bhejein to roughly 1.25 million writes/sec. Har ping chhota hai (~100 bytes: driver ID, lat/lon, timestamp, heading), matlab ~125 MB/sec raw ingest.
- Geo queries: riders app kholna, ride request karna, aur matching engine ka re-scan karna milke peak pe ~500K nearby-driver queries/sec banate hain.
- Live map fan-out: ek active rider ko map pe ~20 paas ke drivers ki updated positions har kuch second mein chahiye. Millions of concurrent viewers ke saath ye tens of millions of chhote pushes per minute hai, isliye ye persistent connections pe behta hai, polling pe nahi.
Core indexing decision: "mere paas kaun hai?" fast kaise poochho?
Latitude/longitude 2-dimensional hain, aur B-tree ek dimension achhe se index karta hai. 4 real answers:
- Geohash: lat/lon ko aise string mein encode karo jahan shared prefix ka matlab shared area (6-char geohash ~1.2 km x 0.6 km cell). "Mere paas ke drivers" ek string-prefix lookup ban jaata hai, jo koi bhi key-value store microseconds mein karta hai. Boundary problem: 5 meter door do points alag cells mein ho sakte hain, isliye 8 neighboring cells bhi query karne padte hain.
- Quadtree: map ko recursively 4 quadrants mein baanto, sirf dense regions ko subdivide karo. Uneven density ke liye badhiya (Manhattan ko chhote cells, kheton ko bade), par drivers ke move karne pe rebalancing mehngi hai, isliye quadtrees memory mein rehte hain, periodically rebuild.
- Google S2: sphere ko Hilbert curve pe map karta hai, edges aur poles pe geohash se better proximity. Google aur kai geo databases mein production mein used.
- Jo Uber ne asal mein banaya (H3): hexagonal grid. Hexagons ki unique property: har neighbor center se equidistant (squares ke diagonal neighbors door hote hain, triangles aur bure). Cell ke around distance rings uniform hain, matlab "search radius badhao" clean ho jaata hai. H3 ke finest resolution pe ~122 billion cells hain, aur Uber ka poora marketplace isi pe chalta hai: surge pricing, dispatch, ETA sab per H3 cell demand aggregate karte hain.
Pipeline, step by step
- Driver app har ~4 second mein persistent connection (ya HTTP/2) pe nearest edge gateway ko ping bhejti hai.
- Gateway ping ko streaming pipeline (Kafka) mein daalta hai. Hot path pe trip database mein kuch nahi likha jaata; live map ephemeral state hai.
- Stream-processing layer in-memory geo index update karta hai: driver ID se current H3 cell, sharded Redis-style store mein cell se keyed. Purani position delete, nayi write, TTL ke saath (30 second tak ping nahi to driver map se apne aap gayab).
- Rider app kholta hai: client apni location bhejta hai, geo service rider ke H3 cell plus neighbors compute karti hai, un cells ke driver IDs nikalti hai, positions return karti hai. Rider dekhte rahe to map WebSocket pe har kuch second mein update hota hai.
- Matching engine isi index ka consumer hai: ride request pe H3 cells ke expanding rings query karta hai (k=0, phir ring 1, phir ring 2) jab tak kaafi drivers na mil jayein, car type aur driver state se filter karta hai, aur dispatch karta hai.
ETA: screen ka doosra sabse mushkil problem
"3 minute door" number geometry nahi hai. Ye ek model hai: road-network graph routing (contraction hierarchies se cross-city shortest paths microseconds mein), live traffic features se adjust (har road segment ki current speeds, unhi driver pings se computed), plus historical trips se learned corrections. Raw GPS distance lagatar jhoot bolta hai: 500 meter door driver nadi ke us paar ho to 12 minute door hai. Isliye ping firehose do maalik serve karta hai: jo live map tum dekhte ho, aur wo traffic model jo sab kuch price aur time karta hai.
Tradeoffs jo naam dene layak hain
- Accuracy vs battery aur bandwidth: 1-second ping interval map smoothness double karta hai par 4 seconds ke muqable ingest cost aur driver battery 4x. 3-5 second industry ka sweet spot hai.
- Live state in-memory, Postgres mein nahi: 1.25M pings/sec relational DB mein likhna use bina fayde ke melt kar dega, kyunki location 30 second baad bekaar hai. Ephemeral in-memory stores with TTLs sahi ghar hain; trip DB sirf pickups aur dropoffs record karti hai.
- Push vs pull rider map ke liye: push (WebSocket) smooth motion deta hai par per viewer server cost zyada; pull (client har kuch second mein refetch) simple aur sasta. Zyadatar systems active trip mein push aur browsing mein pull karte hain.
Production mein kya tootta hai
- GPS noise aur teleporting cars: urban canyons mein GPS 50+ meter jump karta hai. Raw pings ko display se pehle Kalman filter / map-matching pass chahiye (point ko road graph pe snap karo), warna map pe gaadiyan buildings ke through chalti dikhengi.
- Hot cells: New Year's Eve pe Times Square drivers aur queries ko kuch hi cells mein ghusa deta hai. Geo index ko cell se shard karke load spread karna hoga, warna sabse busy neighborhood sabse busy shard ko melt karega.
- Clock skew: galat clock wala driver phone future timestamps inject karta hai. Servers ko arrival pe timestamp karna chahiye aur client time ko sirf hint maanna chahiye.
- Ring-expansion misses: sirf k=0 cells pe matching cell boundaries pe riders ko fail karti hai; hamesha neighbor rings tak expand karo, aur low-density areas mein aur wide (yahi wajah hai ki H3 ke uniform neighbors matter karte hain).
Common interview traps
- Trip database pe "SELECT * WHERE lat BETWEEN ..." propose karna. Is write rate pe B-tree pe 2-dimensional range scans canonical galat jawab hai.
- Live locations ko durable database mein store karna, ephemeral in-memory store with TTLs ke bajaye.
- Geohash ka neighbor-cell problem bhoolna. Har interviewer ka pehla follow-up yahi hai.
- Matching ko rider-side query samajhna. Matching ek marketplace process hai apni state machine ke saath (offer, accept, timeout, next driver), ek read nahi.
Interview Q&A
Q: Uber geohash squares ke bajaye hexagons (H3) kyun use karta hai?
A: Neighbor uniformity. Square ke 8 neighbors do alag distances pe hain (edge neighbors 1x, diagonal ~1.41x), to "is cell se distance r ke andar sab kuch" ka clean jawab nahi. Hexagon ke har neighbor equidistant hai, to search ring ek hex badhaane pe area ki uniform band judti hai. Isse ring-expansion matching, surge polygons aur demand aggregation clean ho jaate hain. Hexagons circle ko bhi better approximate karte hain, to radius queries mein kam area waste hota hai. Cost: hexagons chhote hexagons mein perfectly subdivide nahi hote, isliye H3 resolutions approximate hain aur Uber fixed hierarchy rakhta hai.
Q: Servers ko melt kiye bina rider ka map smooth kaise rakhte ho?
A: Write firehose ko read view se alag karo. Ingest har ping accept karta hai, par rider ke client ko sirf uske viewport ke cells ke updates milte hain, har 2-4 second mein, ek WebSocket pe, client-side interpolation updates ke beech animate karti hai (gaadi glide karti hai chahe data steps mein aaye). Server-side, per-viewport cell subscriptions ka matlab ek ping update sirf un viewers tak fan-out hota hai jinki maps mein wo cell hai.