Architecture & Spatial Indexing:
- Goal: Return nearby businesses within a given radius (e.g., 5km) of a user's latitude and longitude in under 50ms.
1. Spatial Indexing Technology:
- Relational bounding box queries (WHERE lat BETWEEN ... AND lon BETWEEN ...) require full table scans on large datasets.
- Geohashing: Encodes 2D coordinates into a 1D alphanumeric string. Points sharing a common prefix reside in the same geographic box. Querying the prefix retrieves nearby locations quickly.
- Uber H3 / Google S2: Hexagonal hierarchical spatial indexes. Hexagons offer uniform neighbor distances, ideal for radius searches.
2. Storage Architecture:
- Primary Datastore: PostgreSQL with PostGIS extension or DynamoDB with Geohash partition keys.
- In-Memory Spatial Cache: Redis with Geospatial indexes (GEOADD, GEORADIUS), which internally use sorted sets scored by 52-bit Geohash integers.
3. Read Query Flow:
- User sends (lat, lon, radius) -> API Gateway -> Convert coordinates to spatial cell IDs -> Query Redis spatial index -> Filter by business categories/ratings -> Return hydrated metadata.
4. Data Updates:
- Business location changes are rare (write-light, read-heavy). Updates write to DB and asynchronously update Redis cache.