Practice this topic in a realistic system design interview
Global systems often need to group, search, and count locations across the whole Earth.
S2, from Google, and H3, from Uber, both do this by dividing the Earth's surface into cells and giving each cell a compact numeric ID. That ID can then be used as an index key, a grouping key for analytics, or a shard key.
In this chapter, we'll explore Google's S2 and Uber's H3, two systems that divide the Earth's surface into uniform geographic cells. We'll look at how they work, how they differ, and when to use each one.
Many spatial indexing systems, like Geohash, represent the Earth using a flat grid of latitude and longitude. Longitude runs horizontally, latitude runs vertically, and the grid divides the map into smaller cells.
This works well for searches within a city. But it introduces some problems when we start working at a global scale.
For global applications like ride-hailing services, shipping trackers, and mapping platforms, these differences can make location searches more complicated. To handle them, we need a way to divide the Earth's surface into cells that account for its spherical shape.
S2 and H3 take a similar approach. Both divide the Earth's surface into cells and assign each cell a compact numeric ID. They also support multiple levels of resolution, so we can represent large geographic regions or zoom in to much smaller areas.
However, they organize these cells differently and were designed with different use cases in mind.
| S2 | H3 | |
|---|---|---|
| Built by | Uber | |
| Main purpose | Indexing geographic data and location-based searches | Analyzing trips, traffic, and demand across cities |
| Cell shape | Roughly square | Mostly hexagonal |
| Hierarchy | Each cell splits into 4 children that fit exactly inside it | Each cell has 7 children that only roughly fit |
| Strength | Representing regions as ranges of IDs | Working with neighboring areas and distances |
Let's start by understanding how S2 works.