Skip to content
derpx06.dev
WritingExplainersNotesAbout
works on my machineit's always DNSship itLGTMTODO: sleepoff by oneread the docsgit push --force-with-lease404: sleep not foundhave you tried turning it off and on againrubber duck says: explain itpremature optimisationworks on my machineit's always DNSship itLGTMTODO: sleepoff by oneread the docsgit push --force-with-lease404: sleep not foundhave you tried turning it off and on againrubber duck says: explain itpremature optimisation

Learning in public.

A student's notebook on system design, distributed systems and databases. Shared as I learn.

ReadWritingExplainersNotesAbout
ElsewhereGitHubLinkedInHugging FaceLeetCode
Say hellomanaskhairnar1511@gmail.com
derpx06
NORMALmain© 2026 Derpx06Pune, Indiatop ↑
Blog/System Design

How does a ride app find the nearest driver among millions?

Why you can't just index latitude and longitude, and how geohash, H3 hexagons and quadtrees chop the map into boxes.

Derpx0618 Sept · 4 min read
Contents
Why not just check every driver?Why can't I just add an index?What is a geohash?Why check the neighbouring boxes?How do Redis and PostGIS ask?Why does Uber use hexagons?What is a quadtree?Your turnSources
0% read · 4 min leftBack to top ↑
On this page0%
Contents
Why not just check every driver?Why can't I just add an index?What is a geohash?Why check the neighbouring boxes?How do Redis and PostGIS ask?Why does Uber use hexagons?What is a quadtree?Your turnSources

You open a ride app and two seconds later it knows which driver is closest. Out of millions.

Checking every driver one by one would be like scrolling your whole contact list each time you call someone. The trick: turn the map into lots of small boxes, give each box a name, and only look inside the boxes near you.

Why not just check every driver?

Build it the simple way first: a table with driver_id, lat, lng. A rider appears, you measure the distance to every row and pick the smallest.

50 drivers? Perfect. A million? Here's an estimate (my arithmetic, not a measured number): 1,000 riders asking per second x 1,000,000 drivers = 1,000,000,000 distance checks per second. No computer can keep up with that.

Fine, so add an index. Why doesn't that save us?

Why can't I just add an index?

An index is a sorted shortcut, like the one at the back of a textbook. A normal index sorts by one thing, like lat.

Sorting by lat gets you everyone at your latitude, which is a whole ring around the planet. Sort by lng and it's the same problem sideways. Location has two directions, and a sorted list only has one.

One numberIndex on lat alone: thousands of drivers share your latitude across the whole world.
→
Box namesIndex on a box name: only the drivers in your box and its neighbours.

So how do we squash two numbers into one sortable thing? Boxes.

What is a geohash?

A geohash turns a latitude and longitude into a short text code. Keep splitting the map in half, again and again, and write down which half you landed in. The Wikipedia article on geohash explains that each step halves the range and gives one bit, with the longitude and latitude bits interleaved.

Every 5 bits become one character. More characters, smaller box, and nearby points usually share the start of their code.

Length 378 km
Length 420 km
Length 52.4 km
Length 60.61 km
Geohash error by code length, from the table on Wikipedia (approximate).

Now an ordinary index works: "find everyone whose code starts with tsj".

Click Next to follow one ride request through the boxes.

Rider app
Box name
Nearby boxes
Closest driver

Rider sends lat and lng

Step 1 of 5
Only a few boxes are searched, not every driver.

Notice step 4: the distance maths now runs on a handful of drivers, not a million. Why "boxes around it", though?

Why check the neighbouring boxes?

Sharing a prefix means you're close. The reverse is not true. Two points can be metres apart and still share almost nothing in their codes.

Wikipedia notes that points across the equator or the Greenwich meridian can share little or no prefix. So you search your box and its neighbours. Redis does this: its docs state that a radius query checks "the 1+8 areas needed to cover the whole shape", your box plus the eight around it.

Why this matters: box edges are the one place this scheme can mislead you. Now, real tools.

How do Redis and PostGIS ask?

Redis keeps geo data in a normal sorted set (a list sorted by a number called a score). Per its GEOADD docs, it interleaves the latitude and longitude bits into a 52-bit integer and stores that as the score. Then GEOSEARCH finds members in a radius or box, nearest first.

PostGIS (a geography add-on for PostgreSQL) takes another road. Its docs say it uses an R-tree index built on GiST, which stores the bounding box of each shape. Its <-> operator, placed in ORDER BY, gives "index-assisted nearest-neighbor result sets" per the distance operator docs.

Redis GEO. Points in a sorted set. Its docs treat the earth as a sphere, with error up to 0.5 percent in the worst case.

PostGIS. A real database with shapes and a spatial index.

Both use boxes of some kind. But square boxes have a flaw.

Why does Uber use hexagons?

Uber's H3 post explains that squares have two kinds of neighbour (sharing an edge, or only a corner), while in a hexagon the centre is the same distance from all six neighbours. It adds that this simplifies analysis, and that hexagons suit a world where "people in a city are often in motion".

H3 has 16 resolutions (0 to 15), and each finer one has one-seventh the area. The post notes that twelve pentagons are unavoidable, placed to fall in water.

kRing, per the post, returns the cells within grid distance k of a starting cell, which roughly makes a circle.

I'm not claiming how Uber's matching works step by step. The post covers H3 as a grid system.

One more box idea, and then it's your turn.

What is a quadtree?

A quadtree cuts a square map into four, then cuts any crowded square into four again. Busy city centres get many tiny squares, empty deserts stay as one big one. Same "boxes" idea, but adaptive. Idea level only, I'm not citing any app's use of it.

Whole map
Four quarters
Crowded quarter splits again
Tiny boxes in busy places
More detail only where drivers actually are.

Your turn

  1. Drivers move constantly. What happens to your index when a driver changes box?
  2. If a rider's box is empty, what do you do? Widen to neighbours? Go one box bigger?
  3. Would a small project with 500 drivers even need Redis or PostGIS?
Key takeaways
  • A plain index on latitude or longitude alone can't answer "nearest", because location has two directions.
  • Geohash and H3 turn the map into named boxes, so nearby drivers share a box name you can search.
  • Boxes have edges, so always check the neighbouring boxes too.
  • Hexagons give every neighbour the same distance, which is why H3 uses them.

Sources

  • Uber Engineering, H3: Uber's Hexagonal Hierarchical Spatial Index
  • H3 documentation, core library overview
  • Geohash, Wikipedia
  • Redis docs, GEOADD and GEOSEARCH
  • PostGIS manual, managing the database (spatial indexes) and the <-> operator
ShareXLinkedInEmail
Written by Derpx06A developer who builds AI search tools and the code around them. I explain what I got wrong first, so you can skip it. Spotted a mistake? Tell me — corrections get credited.
System Design · 4 minHow does video streaming work? (Why movies play on slow Wi-Fi)System Design · 5 minHow does a search engine find one page out of billions so fast?System Design · 5 minHow to scale a website from 1 user to 1 million users
Start here
01Why is 0.1 + 0.2 not 0.3 in code?02How does Git work inside? (Snapshots, not changes)03How does your computer know the time? (And why it can be wrong)