Skip to content
BytePatterns

Design Geo Proximity Search

System Design Cases: lesson 12 of 20

Two numbers, one index: fold a map down into a sorted string.

Lesson 12 of 20 · 7 min

Design Geo Proximity Search

Step 1 of 11

Fifty million places, and a position is two numbers — which no single sorted index can order by.

The Idea

Return the twenty nearest places to a point, from fifty million of them. A position is two numbers, and no single sorted index can order by both at once.

Real-World Example

Postcodes. Shared leading characters put neighbours next to each other in a list, which is what lets a clerk open one drawer instead of reading every address in the country.

The Tradeoff

A geohash makes proximity a prefix match on a plain index, and inherits the grid's lie: two points either side of a cell edge can share a much shorter prefix, or none at all, so the surrounding cells are read too. A quadtree follows density instead, shallow in empty regions and deep downtown, and costs a live tree to maintain rather than a string.

Your turn

Put the steps in the right order.

  1. Read the candidate places in that cell and its eight neighbours
  2. Encode each place's position as a geohash string and index it
  3. Sort the candidates by true distance and cut to twenty
  4. Encode the search point and trim the prefix to the radius asked for

Mini quiz

1 / 3

A geohash turns proximity into a prefix match, which matters because:

New lessons land every few weeks

Leave an address and we will tell you when the next one is up. That is the only reason we will use it.

One address, stored so we can email you. Nothing else, ever.