Designing a Nearby Service (Yelp / Google Maps Places)
Difficulty: Intermediate Topics: Geohash, Quadtree, Spatial Indexing, Location Updates, Radius Queries Asked at: Google, Uber, Amazon, Swiggy, Zomato, PhonePe, Grab · Prerequisites: Caching, Database Indexing, and Scalability
1. Understanding the Problem
A proximity service answers the question “what’s near me?” — restaurants within 2km, friends within 500m, EV chargers within 5km. The core challenge: indexing millions of locations (some static like restaurants, some moving like drivers) so that a radius query returns results in under 100ms without scanning the entire dataset. Bonus: efficiently updating the index when entities move.
Real examples: Yelp, Google Maps “Explore Nearby,” Uber driver matching, Swiggy restaurant discovery, Bumble/Hinge proximity matching.
2. Naive First Cut
flowchart LR
USER["User with GPS"]
API["API Server"]
DB[("SQL DB<br/>lat and lng columns")]
USER -->|"GET nearby?lat=X&lng=Y&radius=2km"| API
API -->|"SELECT WHERE distance... < 2000"| DB
Store all places with lat/lng. For each query, compute haversine distance to every row and filter.
💡 Haversine is the formula for the distance between two points on a sphere - the correct way to measure “how far apart” on a globe. It involves trigonometry, which matters later because no database index can evaluate it.
Why this breaks:
- Computing distance to every row in a table of 200M places is a full table scan
- B-tree index on lat or lng alone doesn’t help (you need 2D range)
💡 A B-tree is the ordinary database index: a sorted structure that is superb at “values between X and Y” in one dimension. Proximity is two dimensions at once, and that mismatch is the central problem of this whole design. Learn more → - Can’t scale to 100K QPS each doing a full scan
💡 QPS = queries per second. A full table scan might be tolerable once; 100,000 times a second it is not, which is why the fix has to be a different index rather than a bigger machine. Learn more → - No support for moving entities (drivers, friends) that update every 5 seconds
- Radius “2km” varies by latitude (longitude degree spacing shrinks at poles)
- No ranking within results (closest first, highest rated first)
The rest of the doc evolves this into a geospatial index with Geohash-based sharding and efficient range queries.
💡 A geospatial index is a structure built specifically for “what is near this point” rather than “what equals this value”. The trick they all share is turning two coordinates into one sortable key so an ordinary sorted index can answer a map question. Learn more →
3. Prior Art We’re Drawing From
- Uber H3 Hexagonal Grid - Uses a hierarchical hexagonal grid system (H3) to partition the earth into cells at multiple resolutions. Each hexagon has roughly equal area (unlike Geohash rectangles which distort at poles). Used for surge pricing, supply/demand balancing, and driver matching. (Uber Engineering)
- Google S2 Geometry Library - Hierarchical decomposition of the sphere into cells using a Hilbert curve. Enables efficient spatial indexing where a region query becomes a set of contiguous cell range scans. Used in Google Maps and Pokémon Go. (S2 Geometry)
- Redis Geo Commands - Redis GEOADD/GEOSEARCH uses a sorted set with Geohash-encoded scores. O(N+log(M)) for radius queries where N = results and M = total entries. Simple, fast, but single-machine memory-limited. (Redis Docs)
- Foursquare Pilgrim SDK - Background location tracking with adaptive precision: high-frequency GPS in cities, low-frequency cell-tower in rural areas. Balances battery life vs accuracy for proximity features.
4. Functional Requirements
Core (Top 3)
- Find nearby places within a radius - given user’s location and a radius, return relevant places sorted by distance/rating
- Update location for moving entities - accept GPS pings from millions of devices every few seconds and keep the spatial index fresh
- Search with filters - combine proximity with attributes (cuisine type, price range, open now, rating > 4.0)
Below the Line
- Place details, reviews, and photos
- Route/ETA to each result
- Saved/favorited places
- Business owner place management
- Check-ins and visit history
5. Non-Functional Requirements
Core
- Query latency: P99 < 100ms for radius queries
💡 P99 = the figure 99% of queries stay under, so it is a promise about the unluckiest 1 in 100 rather than the average. Learn more → - Update throughput: Handle 2M+ location updates per second (for dynamic entities like drivers)
- Freshness: Moving entity locations reflected in queries within 3-5 seconds of the update
- Scale: 200M static places indexed; 10M concurrent moving entities
Discussion
Newest first