Case Study: Uber Ka Live Map

With a video: matching riders to nearby drivers using geohashing, real-time location updates, and state machines.

Advanced10 min read
Language
Theme

EP 03: Uber ko kaise pata CABs Kahan Hai?

Map = grid of cells (geohash / H3). Nearby = same cell prefix, in RedisTrip state machinerequested -> matched -> done
Key idea at a glance

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:

  1. 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.
  2. 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.
  3. 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.
  4. 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

  1. Driver app sends a ping every ~4 seconds over a persistent connection (or HTTP/2) to the nearest edge gateway.
  2. 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.
  3. 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).
  4. 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.
  5. 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.

Key takeaways

Interview pointer

Lead with the geohash insight: converting a 2D proximity problem into a string-prefix problem. Then describe the state machine. That ordering signals you understand what is actually hard here.