CodingNodes Space play · ←→ step · R restart

The problem

'What is near me?' sounds like a simple distance question, but at scale it is a data-structure problem. Naively, answering it means comparing the query point against every place in the system — and doing that for every request. The standard escape is to give up on pure geometry and trade it for a grid: chop the world into cells, file every place into the cell it falls in, and answer 'near me' by reading one cell and its neighbors instead of scanning the whole map.

That grid is the geohash, and almost everything in the design hangs off it. A query geohashes the user's point, reads the center cell and the eight around it, and filters those candidates by the real radius. Places are filed into cells as they are created, and a user crossing a boundary is detected as an enter and an exit event. We will build the grid, the read path, the write path, the geofencing, and the sharding, then close the loop with one 'near me' query that lands a ranked list of places in a couple of hops.

Requirements

Functional — what it must do

  • Nearby: given a point and a radius, return the places inside that radius, ordered by distance or relevance.
  • Index: a place with coordinates is filed so that a later nearby query can find it without scanning the whole system.
  • Geofence: detect when a moving user enters or leaves a named zone, and raise an event for each crossing.

Non-functional — how well it must do it

  • A nearby query answers in a few milliseconds: the candidate set comes from a handful of cells, not a full-corpus scan.
  • A user's position updates are cheap and idempotent — a new location replaces the last one, and a move only matters when it crosses a boundary.
  • The cell space is large enough to shard, so the index scales out and a query still lands on a small, fixed set of cells.

Scale — how much it must take

  • On the order of tens of millions of places, a few hundred thousand concurrent users issuing nearby queries at peak, and continuous location updates from a moving fleet.

Assumptions — the numbers this design is sized for

A rate limit means nothing without a scale. These are the figures every later part relies on: how much traffic arrives, how much work the limiter has to do per second, and how much latency and memory it may spend doing it.

Places
~50M
The points of interest to index. Each is filed into exactly one cell.
Queries
~300K / s (peak)
Concurrent 'near me' lookups at peak. Each must touch only a handful of cells.
Radius
~1–10 km
The typical query radius. Small enough that one cell plus its neighbors covers it at the right precision.
Cell precision
geohash depth ~5
A depth-5 geohash cell is roughly a square kilometre — the grain at which center+neighbors covers a few-kilometre radius.
Updates
a moving fleet
Users reporting location continuously. Most updates are noise; only boundary crossings are events.
Fences
named zones
A geofence is a region with an owner and an action. Entering or leaving it is the trigger.
Shards
by cell hash
The cell space is cut so each cell always lives in one shard — a query reads a fixed, small set of them.

Takeaways

Part 1

What 'near me' actually is

The query is a point, a radius, and a ranking. Fixing those three — and accepting that a radius is an approximation over a grid — makes the rest a lookup problem.

  1. 1
    Point in, ranked places out

    The input is a location — the user's current coordinates — and a radius, how far 'near' reaches. The output is the set of places inside that circle, ordered by distance or by a relevance score. There is no free text and no keyword; it is purely spatial.

  2. 2
    The naive answer does not scale

    The brute-force answer compares the query point against every place and keeps the ones inside the radius. That is correct, but it is a full scan per query — and at tens of millions of places and hundreds of thousands of queries a second, the scan is the whole problem. The grid exists to avoid it.

  3. 3
    A radius is an approximation over a grid

    A circle does not line up with square cells, so the grid answer is an approximation: we over-fetch a square neighborhood of cells and then apply the exact circle as a filter. We deliberately grab a few too many candidates and throw out the ones outside the true radius. That over-fetch-then-filter is the whole trick.

Part 2

Chop the world into cells

A geohash is a string that names a square cell of the Earth. It is how a (lat, lng) becomes a key, and how 'near me' becomes 'which cell and its neighbors'.

  1. 1
    Interleave, and a point becomes a string

    A geohash encodes latitude and longitude by interleaving their bits: the most significant bits pick the big half of the world, and each further bit halves the region again. The result is a short string that names a square cell, and the cell's size shrinks as the string grows longer.

  2. 2
    Cells tile the map

    Because the encoding halves both axes at every step, the cells tile the whole map with no gaps and no overlaps. A point falls in exactly one cell at a given precision, and that cell is a square whose four edges line up with its neighbors — which is what makes 'the eight cells around it' a well-defined set.

  3. 3
    Nearby is center plus eight

    The key spatial fact: if the cell size is right for the radius, then every place within the radius is in the query's own cell or in one of the eight cells around it. 'Near me' is therefore a read of nine cells — a fixed, tiny number — rather than a scan of the whole map.

Part 3

The read path: nine cells, then a filter

A query geohashes the point, reads the center cell and its eight neighbors from the cell index, and filters the combined candidates to the true radius. Three hops, a bounded set.

  1. 1
    Geohash the user's point

    The proximity service takes the user's coordinates and geohashes them to the working precision, landing on a specific cell. From that string it derives the eight neighbor strings for free — they are arithmetic on the geohash, not a search.

  2. 2
    Read the nine cells

    The service reads the center cell and its eight neighbors from the cell index. Each cell holds the places filed in it, so nine reads return a small, bounded candidate set — the same handful of places whether the map holds a million or a hundred million.

  3. 3
    Filter to the circle, then rank

    The candidates are now filtered against the true radius — the ones outside the circle are dropped — and the rest are ordered by distance or relevance. The over-fetched square has become the exact circle, and the user gets a ranked list of places.

Part 4

File places into cells

A place is indexed once: its coordinates are geohashed and the place is appended to that cell's list. The write is a single, idempotent insert into a known cell.

  1. 1
    Geohash the place's coordinates

    When a place is created, the index service geohashes its coordinates to the working precision, exactly as the read path geohashes a query point. The same function that finds a cell for a query finds the cell for a place — one code path, two directions.

  2. 2
    Append to the cell's list

    The place is appended to the list of places stored under that cell id. That is the whole write — one insert into a known key. A place that straddles two cells is filed in both, so a query that reads either one finds it.

  3. 3
    Idempotent, so re-indexing is safe

    Because a place's cell is determined purely by its coordinates, re-filing it — after a move or a rebuild — lands it in the same place. The write is idempotent: doing it twice does not duplicate the place, so the index can be rebuilt or a place updated without a cleanup pass.

Part 5

Geofencing: enter and exit

A geofence is a named zone with an owner and an action. The service detects the moment a moving user crosses its boundary and raises an event — the crossing, not the position, is what matters.

  1. 1
    A fence is a zone with a rule

    A geofence is a region — a circle or a polygon — attached to a rule: who owns it, and what happens on entry and on exit. The rule is stored once; the detection that follows runs continuously for every user who moves through the zone.

  2. 2
    Detect the crossing, not the position

    As a user reports new locations, the service checks the cell they are in against the fences that cover that cell. A position inside a fence is not an event; the event is the transition — inside to outside is an exit, outside to inside is an enter. Comparing consecutive positions is what turns a stream of coordinates into a stream of events.

  3. 3
    Debounce the jitter

    A location fix near a boundary will jitter in and out with the noise of the GPS. A raw crossing check would fire an enter and an exit for every wobble. The service debounces: a crossing only counts if the user stays on one side for a minimum time or distance, so a fence fires once, not per fix.

Part 6

Shard the cell space

Tens of millions of places across millions of cells will not fit hot on one machine. The cell space is cut into shards so each cell always lives in one place and a query reads a fixed, small set of shards.

  1. 1
    One index is not enough

    The full cell index is too large to hold in fast memory on one box, and one box has a hard throughput ceiling. The index must be split, and the split must be chosen so that a given cell always lives in exactly one shard — a query that reads nine cells must not scatter across the whole fleet.

  2. 2
    Hash the cell, land on one shard

    A router hashes each cell id and sends it to the shard that owns it. Because the same cell always hashes to the same shard, the nine cells of a query land on at most nine shards — a small, bounded fan-out rather than a scatter. Most of the time they are fewer, since neighboring cells often share a shard.

  3. 3
    Rebalance by cell range

    Shards are ranges of the cell hash space, so adding a shard reassigns only the slice of cells between it and its predecessor. Scale-out is a steady trickle of cell-range moves, and because a query reads a bounded set of cells, it keeps working correctly through the hand-off.

★ Finale

End to end

One 'near me' query and one crossing: the point is geohashed, the nine cells are read, the candidates are filtered to the circle, and a boundary crossing raises a single clean event.

  1. 1
    The whole system

    Every piece in one frame. The user's point is geohashed, the center cell and its neighbors are read from the cell index, the candidates are filtered to the true radius and ranked. Places are filed into cells on the write path, and the geofence service watches for crossings to raise events.

  2. 2
    The query, nine cells deep

    The user's point is geohashed to a center cell, and the service reads that cell plus its eight neighbors. Nine reads return a small candidate set, the radius filter keeps only the places inside the circle, and the ranked list goes back. A query that once meant scanning tens of millions of places is now a handful of bounded reads.

  3. 3
    A crossing, one clean event

    As the user moves, their new position is checked against the fences covering the cell they are in. Crossing a boundary — and sustaining it past the debounce — raises exactly one event: an enter or an exit, delivered to the fence's owner. The position stream has become an event stream.

  4. 4
    Near me, and who crossed what

    Both jobs in one machine: a 'near me' query answers from a bounded set of cells, and a boundary crossing raises a single clean event. The geohash grid is the load-bearing piece — it is what turns two unbounded scans into a handful of reads and a stream of well-defined events.

🌐 In the wild

In the wild

Geohash-based proximity is the backbone of location services everywhere. The boxes below are anchored to the classes of well-known systems that run each piece — an illustrative mapping, not a claim about any one product's internals.

  1. 1
    The pattern, piece by piece

    Each box is a standard building block of a proximity service. Anchoring them to the classes of production systems that run that piece turns the abstract design into 'the shape behind every map and every 'near me'.

  2. 2
    Why the same grid keeps winning

    Independent location services converge on the same pieces because each one removes a real cost: the geohash removes the full-corpus scan, the nine-cell read bounds the fan-out, the radius filter makes the answer exact, the debounce removes the GPS jitter, and the cell-hash shard removes the single-box ceiling. That convergence is the evidence the design is load-bearing.