Stage 3 · Distributed data · Medium · 15 min
Geospatial indexes: finding what is nearby
How an app finds the drivers, shops or friends near you without checking every place on the map: grid cells, geohash codes and quadtrees.
What each interview level is expected to know
Learn first: Indexes and storage engines, Replication and sharding
After this lesson you can:
- Find nearby places by reading a cell and its neighbours, not the whole map.
- Compare a fixed grid, geohash codes and quadtrees.
- Choose a cell size, and keep moving drivers up to date.
Start here
See it in 60 seconds
Finding places nearby with a quadtree
A quadtree cuts the map into four squares, and cuts again only where places crowd, so a nearby search checks a few small squares.
Scene 1 of 6 · Find coffee near me
A user in the busy city centre asks for the coffee shops nearest to where they stand.
- You, in the centre — healthy
- Quadtree index — healthy — 76 places on a 16 km map
- Your square — healthy
- Squares next door — healthy
- Countryside square — healthy
- Levels at the centre
- not shown yet
- Smallest square (m)
- not shown yet
- Find coffee near me. A user in the busy city centre asks for the coffee shops nearest to where they stand.
- Split into four. The whole map holds more than 4 places, so it splits into four squares, each 8 km wide.
- Crowded squares split again. The square holding the busy centre splits again, and again, wherever more than 4 shops crowd.
- Quiet squares stay big. The south-west square has only 3 farms, so it stays one 8 km square, and a search there is quick.
- Check next door too. The search reads the user's small square, then squares next door until it has enough shops.
- Geohash, a related idea. Geohash is a fixed grid instead: each cell gets a code like 9q8y, and nearby cells usually share its start.
- Now you try. How many shops crowd into the 1 km city centre? For example: The centre goes 7 levels deep, down to squares 125 m wide, while the countryside stays one 8 km square. A square splits only when it holds more than 4 places, so the tree grows deep only where places crowd.
Scene 1 of 6: Find coffee near me. A user in the busy city centre asks for the coffee shops nearest to where they stand.
Key points
- A square with more than 4 places splits into four smaller squares.
- Crowded city squares split deep, while empty countryside stays one big square.
- A search also reads the squares next door, since a shop can sit across a line.
- Geohash is a related idea: a fixed grid where each cell gets a short code.
In plain words
A rider opens a ride app and asks for drivers within 1 km. The app could measure the distance to all 200,000 drivers in the city, but that is far too slow to do for every rider. A normal (An extra structure, kept sorted or grouped, that lets a database find matching items without reading all of them.) sorts by one value, and a place has two: how far east and how far north. A (An index that finds what is near a place on a map. Most cut the map into cells, and each cell lists what is inside it.) solves this by cutting the map into cells. Each cell keeps a list of what is inside it, so a search reads a few cells near the rider and skips the rest. A (A short code that names one cell of a fixed grid over the map. Every place inside the cell starts with the same code.) is a short code for one cell of a fixed grid. A (A way to cut a map into squares. A square that holds too many places splits into four smaller ones, so crowded areas get small squares.) uses big squares in empty places and small squares in crowded ones.
For the rider asking for drivers within 1 km, the search runs in three steps. First, it finds the cell the rider stands in. Then it adds the cells around it, because a driver 50 m away may sit across a cell edge. Last, it measures the real distance only to the drivers in those few cells, and drops any that are over 1 km. When a driver moves into a new cell, the app moves them to that cell's list.
The problem it solves
An index on "how far north" finds everyone in a thin band that runs right round the Earth. An index on "how far east" finds another band. Neither can answer "within 1 km of me" without checking a huge number of rows. And drivers move, so the index must also take thousands of updates a second.
Why it matters
"Find the drivers near me" is one of the most common interview problems, and food delivery, maps and dating apps ask the same question. A normal database index cannot answer it quickly, so you need to know what can.
A worked example
Say a city has 200,000 drivers, and a grid with cells 1 km wide has 10,000 cells. That is about 20 drivers in each cell. A search reads 9 cells, so it checks about 180 drivers, not 200,000. That is over 1,000 times less work.
Think of it like…
Think of a paper street atlas. To find a café near you, you open the page for your part of town, not every page. If you stand at the edge of the page, you also check the next one.
Where this analogy stops working
Looking only at nearby pages is the useful part. But an atlas is printed once. Drivers move every few seconds, so their cell lists change all the time. The index has to take thousands of updates a second, which is why it is often kept in memory.
Try it
See it working
Also try: Sharding
Mechanics
How it works
A geospatial index answers "what is near here?" by reading a few cells of a grid instead of measuring every place. The picture beside step 2 shows why the neighbour cells matter.
Cut the map into cells
Lay a grid over the map, with cells 2 km wide, for example. Store each driver under the cell they are in. To search, work out which cell the rider is in, and read that cell's list.
Read the neighbour cells too
Pick a case to follow belowWith this case: Reading one cell found D1 at 900 m and missed D3 at 300 m, which was in the next cell.
North-west, 0 drivers North-west 0 drivers North, 1 driver North 1 driver North-east, 1 driver North-east 1 driver West, 0 drivers West 0 drivers Rider's cell, 2 drivers, NOW Rider's cell 2 drivers rider here NOW East, 1 driver East 1 driver South-west, 0 drivers South-west 0 drivers South, 1 driver South 1 driver South-east, 0 drivers South-east 0 drivers - North-west — 0 drivers — healthy
- North — 1 driver — healthy
- North-east — 1 driver — healthy
- West — 0 drivers — healthy
- Rider's cell — 2 drivers — healthy — NOW — rider here
- East — 1 driver — healthy
- South-west — 0 drivers — healthy
- South — 1 driver — healthy
- South-east — 0 drivers — healthy
Step 1 of 3Step 1 of 3 · Rider's cell
Done: Find the rider's cell
- What happens
- The rider stands in the middle cell, 200 m from its east edge. Each cell is 2 km wide.
- Why
- Working out the cell from a place is one quick sum, with no search.
- What changes
- The search will read this cell only.
- Next
- Read the rider's cell
Cells read0the rider's cell onlyDrivers measured0Nearest driver found–not yetStep 1 of 3: Rider's cell. Find the rider's cell. The rider stands in the middle cell, 200 m from its east edge. Each cell is 2 km wide.
What these pictures simplify. This picture uses 2 km cells and a few stated drivers. A real city has thousands of drivers per cell in busy areas, and cell sizes are chosen to match the search radius.
A rider 200 m from the edge of a cell has nearby drivers in the next cell. So read the rider's cell and the 8 cells round it: 9 lookups. Then measure the real distance to each driver in those lists, and keep the ones inside the circle.
In the picture, the rider stands 200 m from the east edge of their cell. Reading only that cell finds D1, 900 m away, and misses D3, which is 300 m away in the next cell.
Reading the 9 cells finds D3. The real distance then decides who is inside the circle: a driver in a near cell can still be too far away.
Give each cell a short code: geohash
A geohash turns a place into a short string, such as tdr1y. Each extra character cuts a cell into 32 smaller ones. So 5 characters name a cell about 5 km wide, and 6 characters one about 1.2 km wide. Every place in a cell starts with that cell's code, so an ordinary index on the string finds them.
Use a quadtree where places crowd
One cell size is too big for a city centre and too small for a desert. A quadtree starts with one square and splits a square into four when it holds too many places, such as more than 4. Crowded areas end up with small squares, and empty areas stay as one big square.
Keep moving things up to date
A driver's phone sends its place every few seconds. Each update takes the driver out of the old cell's list and puts them in the new one. So moving things are often kept in memory, in a store such as (A widely used data store that keeps its data in memory, so it answers much faster than a database on disk. It is often used as a cache.). A driver who has sent nothing for about 30 seconds is removed.
Split the index by area
The cell code is a natural (One slice of a database. Each shard holds part of the data on its own machine.) key: one city's cells can live on one shard. A search near the border between two shards asks both, the same way it reads neighbour cells.
Use it when
- Any "near me" question: drivers, restaurants, friends, delivery riders.
- A fixed grid or geohash when places are spread fairly evenly, or when your database only has ordinary indexes.
- A quadtree when crowding differs a lot from place to place, such as a city and the land round it.
Skip it when
- A handful of places, such as 50 shops: checking all of them is simpler, and fast enough.
- Reading one cell only: it misses places that are across the cell's edge.
- Trusting the first characters of a geohash to mean "nearest". Two places 100 m apart can sit on either side of a cell edge and share no starting characters.
Check yourself
Judgement
Trade-offs and how it fails
What you give up
- Fixed-grid
- Simple, and works with any database index, but one cell size cannot suit both a city centre and empty land.
- Quadtree
- Fits itself to crowding, but it is a tree to keep in memory and to change as places are added and removed.
Failure modes
- A cell that is too big: one crowded cell holds most of the drivers, and every search there checks them all.
- A cell that is too small: a wide search reads hundreds of cells, and most of them are empty.
- Stale places: a driver who went offline still shows as nearby until the entry is removed.
- A hot city on one shard: a concert sends all its traffic to the shard that owns that area.
In the real world
- Redis stores places with GEOADD and searches them with GEOSEARCH. Inside, each place is kept as a geohash-style number, in a list sorted by that number. Redis cannot expire one place in that list, so old ones must be removed by their last-seen time.
- Uber built H3, an open-source grid of six-sided cells, to group riders and drivers by area.
- Google's S2 library cuts the globe into cells and numbers them, so cells that are close on the map usually have close numbers. Source: The S2 Geometry library (checked 14 October 2026)
- PostGIS adds geospatial indexes to PostgreSQL. They are built on a tree of boxes drawn round groups of nearby places, called an R-tree.
Trade-offs to be ready to discuss: Fixed grid vs quadtree · Cell size large vs small
Check yourself
Interview
What interviewers look for
- For "find nearby drivers", name the index first: "I'd store drivers by geohash cell, and read the rider's cell and its 8 neighbours."
- Say how you pick the cell size: about as wide as the search radius, so 9 cells cover the circle.
- Mention updates: drivers move, so the cell lists live in memory, and a driver who stops reporting is removed.
Say it out loud: Why does a nearby search read 9 cells and not 1?
Expected depth by interview level
Levels describe the depth interviewers probe, not years of experience. Choose a level on the learning path to highlight it.
- JuniorUseful
- Why a normal index cannot answer "near me" quickly, and the idea of cells.
- MidExpected
- Geohash cells, reading the neighbour cells, and how to pick a cell size.
- SeniorExpected
- A fixed grid against a quadtree, moving drivers, and splitting the index by area.
- StaffUseful
- Hot cities, borders between shards, and what stale places cost the product.
Practice
Apply it
Three harder questions
The first two use the idea on a new case. The last asks you to judge a design.
Apply it
Apply it
Judge a design
Go deeper
Sources and further reading
First-hand sources to check this page against and to go further. They are standards, official docs, original papers, and guides from engineering and security teams. Each link was opened on 8 November 2026.
- Official docsRedis geospatialRedis documentationStoring points and searching by radius, built on a geohash inside a sorted set.
- Official docsS2 GeometryS2 Geometry project (Google)Covering the sphere with cells so nearby places share a key prefix.
- Official docsH3 documentationH3 project (Uber)Hexagon cells and why hexagons give every neighbour the same distance.