Design a Proximity Service that allows users to discover other users/devices/entities within a specified geographical radius in real-time . This system should be highly scalable, handle frequent location updates, and support millions of concurrent users globally.
Use cases include:
• Ride-hailing apps (e.g., Uber/Lyft finding nearby drivers/riders)
• Social apps (e.g., dating apps showing nearby users)
• Emergency services (e.g., dispatching nearest responders)
• IoT networks (e.g., devices detecting nearby devices)
🎯 Functional Requirements:
1. Location Updates
• Users/devices send frequent location updates (every few seconds/minutes)
2. Nearby Query API
• Given a user ID and a radius r, return a list of nearby users/entities
3. Real-time Tracking
• Push updates to users/devices as nearby entities enter/exit the radius
4. Distance Filtering
• Support Haversine or Euclidean distance calculation
5. Location Expiry
• Ignore stale location updates (e.g., after 1–2 minutes of inactivity)
6. Rate Limiting
• Prevent abuse by throttling location updates and queries
⚙️ Non-Functional Requirements:
• Real-time or near-real-time responses (<100ms)
• Global scale support (millions of users/devices)
• High availability and fault tolerance
• Horizontal scalability
• Geo-distributed architecture
🧱 Core Components:
ComponentDescription
Location Update API
Accepts live GPS coordinates from users/devices
Proximity Query API
Responds with nearby users/entities within a radius
Spatial Indexing System
Efficiently indexes users on a map for fast lookup
Location Store
Stores current active locations with TTL
Notification Service
Pushes notifications/events on proximity changes
Rate Limiter
Controls abuse from clients
🌐 Spatial Indexing Techniques:
• Geohashing: Encode lat/lng into a geohash string and store in a map-based index
• Quadtrees / R-trees: Spatial tree indexing
• Grid-Based Bucketing: Divide map into cells and only check relevant cells
• Elasticsearch Geo Queries
• Redis with GEOADD/GEOSEARCH
🔁 Sample Flow: Nearby User Discovery
1. User A moves and sends POST /location with lat/lng
2. System updates spatial index and active location store
3. Another user B calls GET /nearby?user_id=A&radius=1km
4. System finds A’s location → queries spatial index for neighbors in that radius
5. Returns list of nearby users/devices sorted by distance
🗃️ Sample Location Data Schema:
Redis (GEOADD)
1 nginx
2 CopyEdit
3 GEOADD user_locations 77.5946 12.9716 "user123"
MongoDB/SQL
1 json
2 CopyEdit
3 {
4 "user_id" : "user123" ,
5 "location" : {
6 "type" : "Point" ,
7 "coordinates" : [ 77.5946 , 12.9716 ]
8 } ,
9 "updated_at" : "2025-07-23T14:55:00Z"
10 }
📦 Tech Stack:
LayerSuggested Tools
API Gateway
NGINX / Kong / Envoy
Location Ingest
gRPC / REST API
Spatial Index
Redis GEO , PostGIS, Elasticsearch, Cassandra w/ geohash
Storage
MongoDB, Cassandra, DynamoDB
Notification Layer
Kafka + WebSockets / Push
Monitoring
Prometheus + Grafana
⚠️ Challenges (Hard Level):
• Frequent writes (location updates) vs real-time reads (queries)
• Maintaining consistent spatial index across shards
• Handling movement boundaries between geohash/grid cells
• Dealing with GPS drift, accuracy errors
• Multi-region latency handling
• Push updates when nearby state changes (e.g., notify user when someone enters their radius)