Design Nearby Friends Real-Time Location Service
1. Problem Statement & Scope Clarification
System Mission
Design a real-time, high-throughput location sharing and proximity alerting service (similar to Apple Find My, Snapchat Snap Map, and Zenly) capable of continuously ingesting live GPS telemetry from tens of millions of concurrent mobile devices, computing pairwise proximity () across complex social graphs, and delivering real-time map updates with minimal mobile battery consumption.
Functional Requirements
- Periodic Location Broadcast (
UpdateLocation): Ingest mobile GPS coordinates every 30 seconds when the app is active in foreground/background. - Nearby Friends Query (
GetNearbyFriends): Retrieve a real-time list of mutual friends within a radius, along with distance, bearing, and last-updated timestamp. - Proximity Push Notifications: Alert users when a mutual friend enters their designated proximity zone ().
- Privacy & Ghost Mode Controls: Support granular privacy toggles (Precise Location, Blurred/Fuzzy City-Level, Ghost Mode / Invisible).
- Battery-Aware Movement Gating: Dynamically adjust GPS sampling rates based on device motion states (Stationary, Walking, Driving).
Non-Functional Requirements (SLAs & SLOs)
- High Ingestion Throughput: Ingest peak of .
- Propagation Latency: Friend location updates delivered via WebSocket in .
- Availability: uptime SLA.
- Data Ephemerality: Live GPS coordinates are purely ephemeral (10-minute TTL in RAM); zero long-term location breadcrumbs stored on disk.
2. Capacity & Scale Estimation (Back-of-the-Envelope Math)
User Scale & Ingestion QPS
- Daily Active Users (DAU): ().
- Concurrent Active Broadcasters: ().
- Average Broadcast Frequency: 1 ping every 30 seconds.
- Average Ingest QPS:
- Peak Ingest QPS ( multiplier):
The Quadratic Fan-Out Disaster Proof
If an average user has , publishing every GPS update to all friends yields: Broadcasting 133 Million WebSocket messages per second is economically and computationally infeasible.
Mitigation via Mutual Cell Pub/Sub Filtering
Instead of blind fan-out, we only propagate location updates to friends who are:
- Currently Online and Connected to WebSockets ().
- Located in the Same or Adjacent Uber H3 Hexagonal Cells (). This reduction makes real-time delivery completely tractable on a modest Redis Pub/Sub cluster.
Memory Sizing for Live Geospatial State
- Record size per user in Redis:
user_id(16B),lat(8B),lon(8B),h3_index(8B),timestamp(8B) . Even with 3x replication and social graph caching, the entire live geospatial index requires .
3. High-Level Architecture & AWS Component Mapping
Interactive Architecture DiagramSynthesizing vector architecture diagram...
4. API Interface Design & Wire Protocols
1. Client Location Update Frame (WebSocket JSON)
json{ "action": "UPDATE_LOCATION", "latitude": 37.774929, "longitude": -122.419416, "accuracy_meters": 8.5, "altitude_meters": 42.0, "speed_mps": 1.4, "privacy_mode": "PRECISE", // "PRECISE", "FUZZY_CITY", "GHOST" "timestamp_client_epoch_ms": 1767225600120 }
2. Server Downlink Location Broadcast Frame
json{ "event": "FRIEND_LOCATION_UPDATE", "friend_id": "usr_alice_101", "latitude": 37.774929, "longitude": -122.419416, "distance_meters": 1240, "bearing_degrees": 45.2, "last_updated_epoch_ms": 1767225600120 }
5. Geospatial Indexing & Uber H3 Hexagonal Grid Architecture
The earth's surface is partitioned into Uber H3 Hexagons at Resolution 7 (Average edge length , Area ):
textH3 Resolution 7 Cell Hierarchy: Center Hexagon (Cell 0) surrounded by 6 Immediate Neighbor Hexagons (k-ring radius = 1) Total 7 Hexagons cover a ~5 km Proximity Radius seamlessly
Interactive Architecture DiagramSynthesizing vector architecture diagram...
Unlock Complete Architecture & Production Runbooks
You have explored the free architectural preview (~44%). Spend 1 Coin to unlock the remaining 6 production deep-dive sections for a full 24 hours.