18. Geospatial Indexing
1
The picture to keep in your head2
18.1 Why does a normal latitude/longitude index fail?3
18.2 What are spatial indexes?4
18.3 How do "nearby within radius" queries work?5
18.4 How do you update moving drivers?6
18.5 How do you shard by region?7
Memory card and where the analogy breaks8
Practice: five levels9
Interview questions10
Solutions11
Sources12
Code lab18. Geospatial Indexing
18.5 How do you shard by region?
3 min read+10 XP
On the map: One map desk can't serve the whole country. So there's a desk per city. A question about Bengaluru goes to the Bengaluru desk. The Bengaluru desk is the busiest, so it gets more clerks, each covering part of the city.
In the system: When one server can't hold all riders or handle all pings, shard (Chapter 15) by location:
Figure: a nearby query almost always stays inside one city, so one shard answers it.
- By city or region: the natural choice. A "riders near this store" query almost never crosses a city, so one shard answers it.
- By cell prefix: split a busy city further by geohash or S2 cell ranges (shard 1a covers some prefixes, 1b others). This is how you handle a hot city.
- Boundaries: a store near the edge of a shard's area needs riders from both sides. Query both shards and merge, or let shards overlap slightly at their edges.
- Hot spots move. Evenings in a busy area can swamp one shard. Watch load per shard and split the busiest cells, the same idea as hot partitions in Chapter 15.
Memory code: "Shard by place, split the busy places, and ask both sides at a border."