Lesson 6 of 12
18. 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:

Bengaluru, Hyderabad and Pune each on their own shard; smaller cities share one.

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."