Why you can't just index latitude and longitude, and how geohash, H3 hexagons and quadtrees chop the map into boxes.
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.
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?
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.
So how do we squash two numbers into one sortable thing? Boxes.
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.
Now an ordinary index works: "find everyone whose code starts with tsj".
Click Next to follow one ride request through the boxes.
Rider sends lat and lng
Notice step 4: the distance maths now runs on a handful of drivers, not a million. Why "boxes around it", though?
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.
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.
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.
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.