Geohash and hierarchical cells (H3, S2)
A way of turning a latitude and longitude into a short string or integer cell id where nearby places share a prefix, so a map becomes a grid of named cells. It exists because "drivers within 2 km" is a two-dimensional question that ordinary indexes cannot answer, but "drivers in these nine cells" is a plain key lookup.
What it costs
| Operation | Cost | Note |
|---|---|---|
| Encode a point | O(precision) | Interleave the bits of latitude and longitude; about 30 bit operations. |
| Neighbours of a cell | O(1) | Arithmetic on the cell id; 8 neighbours for a geohash, 6 for an H3 hexagon. |
| Points in a cell | O(1) + members | The cell id is the key of a set or a sorted-set range. |
| Radius query | O(cells × members) | Query the centre cell and its ring, then filter by true distance. |
| Change precision | O(1) | Truncate the string for a coarser cell; a parent cell is a prefix of its children. |
Memory: The index is just the points keyed by cell: about 100 bytes per point in a Redis sorted set. 50,000 drivers in a city is 5 MB; the cell ids themselves cost nothing extra because they are derived from the coordinates.
The rider’s cell and its eight neighbours
Each box is a precision-6 cell about 1.2 km wide; the label is its geohash. A nearby query always reads all nine, because a rider near an edge has close drivers in the next cell.
How it works
- Divide the world in half by longitude: west is bit 0, east is bit 1. Then divide the chosen half by latitude: south 0, north 1. Alternate longitude and latitude, each split halving the current rectangle, and append one bit per split.
- After 30 splits you have 30 bits; group them into five-bit chunks and write each chunk as one base-32 character. That six-character string is the geohash. Two points in the same rectangle after 30 splits have the same six characters.
- Precision is the string length. Each extra character divides the cell about 32 ways, so cell size shrinks fast: 5 characters is roughly a town, 7 roughly a block (table in the example below).
- Nearby places usually share a prefix, but not always: two points a metre apart on either side of a split line have different hashes from the first differing bit onward. That is the edge effect, and it is why a nearby query always reads the centre cell plus the eight cells around it.
- H3 and S2 fix the shape problems: H3 tiles the globe with hexagons at 16 resolutions so every neighbour is the same distance away, and S2 projects the sphere onto a cube so cells do not stretch near the poles. Both give a 64-bit integer id with the same prefix-means-parent property.
- To store moving points, keep one sorted set per region with the cell id (or its 52-bit interleaved form) as the score. Redis GEOADD does exactly this, and GEOSEARCH computes the covering cells for a radius and reads them in one call.
Why it is shaped this way
- Interleaving latitude and longitude bits turns two dimensions into one sorted key, which is the only thing a key-value store or sorted set can index.
- The prefix-is-parent property means zooming out is string truncation, so coarse and fine queries use the same data.
Worked example
Driver cells for ride matching
- Precision to cell size at the equator (width × height): 5 characters ≈ 4.9 km × 4.9 km; 6 characters ≈ 1.2 km × 0.6 km; 7 characters ≈ 153 m × 153 m. Cells narrow toward the poles because longitude lines converge.
- Pick precision 6 for drivers: a 40 km × 40 km city is about 2,000 cells; 50K drivers is 25 per cell on average, a few hundred downtown. A rider’s 9-cell query covers roughly 3.6 km × 1.8 km, a good first ring for a 2 km search.
- Rider at 37.7749, -122.4194 encodes to 9q8yyk. Compute the 8 neighbours (9q8yys, 9q8yy7, 9q8yym, 9q8yyh, 9q8yyj, 9q8yyt, 9q8yye, 9q8yy5), read 9 sets, about 225 candidates, filter out drivers not updated in 15 s, sort by distance, send the nearest 10 for ETA.
- Too few candidates at 2 am: truncate to precision 5 (9q8yy) and read its 9 neighbours, covering about 15 km × 15 km in one step. Too many downtown: switch that region to precision 7 and read 9 cells of 153 m.
- Updates: 50K drivers × one GEOADD every 4 s = 12.5K writes per second, each re-encoding the position and moving the member’s score. One Redis node.
When it is the wrong choice
- Static points with wildly uneven density (every restaurant in a country): fixed cells are either too coarse downtown or too fine in the countryside. A quadtree or an R-tree adapts cell size to density.
- Queries by shape rather than radius (everything inside this delivery polygon): a spatial index in PostGIS handles polygons; cells only approximate them.
- Exact nearest-neighbour ranking: cells give candidates, not order. Always compute true distance (or road ETA) on the candidates afterwards.
Interviewer follow-ups
Answer out loud before you open each one.
Why do you query nine cells instead of one?
Cells are cut along fixed lines, so a rider one metre from a boundary has drivers one metre away in the neighbouring cell with a completely different hash. Reading the centre cell and its eight neighbours guarantees nothing within one cell width is missed; true distance filtering then trims the rest.
What precision gives roughly one-kilometre cells, and how did you pick it?
Six characters: about 1.2 km by 0.6 km at the equator. Pick the precision so a typical nine-cell query returns tens to a few hundred candidates, which for 50K drivers over a 40 km city lands on six. Dense districts can run one level finer and quiet suburbs one level coarser.
How is H3 different from a geohash, and when is it worth the dependency?
H3 uses hexagons, so all six neighbours are the same distance from the centre and ring queries are uniform; geohash rectangles have four near neighbours and four far ones. H3 also avoids the latitude stretching. Use it when you do analytics by cell (surge pricing per hexagon) or need consistent rings; a geohash is fine for simple proximity.
How do you store 50,000 moving drivers so updates and queries are both fast?
One Redis sorted set per city region with the interleaved cell id as the score: GEOADD updates a member in O(log n), GEOSEARCH reads the covering cells for a radius in one call. Expire stale members by storing an update time and filtering on read, and shard by region when a single node is saturated.
Where it shows up
- Building blocks
- Find things near a point · Ingest millions of position updates · Shard by region · Partition the data
- Problems
- Ride Matching (Uber / Lyft)