Find Nearby Friends HLD

Find Nearby Friends HLD


system-design high-level-design

Overview

The “Find Nearby Friends” feature allows users to share their real-time location and receive updates when their friends are nearby. The feature is optimized for real-time geospatial distance calculations and notifications with high scalability and low latency.

Key Components

  1. Mobile App: Sends real-time location updates using WebSocket connections.
  2. Load Balancer (LB): Distributes WebSocket connections and forwards the updates to WebSocket servers.
  3. WebSocket Server: Handles multiple user connections, processes location updates, and calculates distances between users and their friends.
  4. Location Cache (Redis): Stores the latest location data for real-time access and geospatial computations.
  5. Location History Database: Stores location history for analysis and historical tracking.
  6. Redis Pub/Sub: Facilitates real-time notifications for location updates to friends.

Detailed Architecture

1. Mobile App

The mobile app periodically sends the user’s real-time location through a WebSocket connection to the system. This connection ensures low latency and constant communication between the client and server.

  • Connection Type: WebSocket (for low-latency updates)
  • Data Sent: User ID, latitude, longitude, timestamp

2. Load Balancer (LB)

The Load Balancer distributes incoming WebSocket connections across a pool of WebSocket servers to handle a large number of users efficiently. It ensures optimal resource utilization and high availability by routing connections based on server load.

  • Responsibilities:
    • Distribute WebSocket connections
    • Handle failover and redundancy

3. WebSocket Server

The WebSocket server manages client connections and processes the incoming location data. It performs the following tasks:

  • Persist Location History: The server asynchronously stores the user’s location in the **Location History Database ** for historical analysis or tracking.

  • Update Location Cache: It updates the user’s latest location in Redis (a fast, in-memory store) to facilitate real-time distance computations and lookups. The cache holds only the most recent location for each user.

  • Publish Location Update: The server uses Redis Pub/Sub to publish the user’s location to a specific channel associated with the user’s friends. This enables friends to receive real-time location updates as soon as they are available.

  • Manage WebSocket Connections: The server maintains a connection handler in-memory for each active user, allowing efficient user-to-user communication.

Concurrency: Steps like location persistence, cache updates, and Pub/Sub publishing are handled concurrently using asynchronous processing to ensure real-time updates and high throughput.

4. Location Cache (Redis)

The Redis cache stores the most recent location of each user. Redis is also equipped with geospatial indexing capabilities, allowing quick geospatial distance calculations between users.

  • Data Stored: User ID, latitude, longitude, timestamp
  • Geospatial Support: Redis provides geospatial commands like GEOADD, GEODIST, and GEORADIUS, making it easy to perform radius-based location queries and distance calculations.

5. Location History Database

A database (e.g., MySQL, PostgreSQL) is used to persist the location history of each user. This historical data is useful for tracking users’ movements over time and for analytical purposes.

  • Schema:
    • user_id: Unique identifier for the user
    • latitude: Latitude of the location
    • longitude: Longitude of the location
    • timestamp: Time of the location update

6. Redis Pub/Sub

The Redis Pub/Sub mechanism enables real-time communication between users. When a user’s location is updated, it is published to the user’s dedicated channel in Redis, and all friends subscribed to that channel receive the update instantly.

  • Publisher: The WebSocket server, publishing user location updates
  • Subscribers: Friends of the user, receiving real-time updates

7. Subscribers (Friends)

Friends of the user subscribe to their location updates via Redis Pub/Sub. When a location update is published, the subscribing users are notified in real-time.

  • Notification Trigger: Friends will only be notified if they are within a predefined distance (radius) of the user.

8. Geospatial Distance Calculation

The WebSocket server uses geospatial data stored in Redis to calculate the distance between the user and their friends. Redis provides efficient geospatial commands that allow the server to calculate distances within a defined radius (e.g., 1 km, 5 km).

  • Redis Commands Used:
    • GEODIST: Computes the distance between two users’ locations
    • GEORADIUS: Finds all friends within a specified radius around the user

If the distance is less than the predefined threshold, the WebSocket server sends a notification to the nearby friend’s client in real time.

Optimizations

  • Asynchronous Processing: To handle the high volume of location updates and minimize latency, tasks such as location persistence, cache updates, and Pub/Sub notifications are processed concurrently.

  • Geospatial Indexing: Using Redis’s geospatial capabilities significantly speeds up distance calculations. It reduces the need for complex, custom geospatial algorithms by leveraging Redis commands optimized for this purpose.

  • Efficient Data Transfer: Using WebSocket connections minimizes overhead compared to HTTP polling, ensuring that location updates are sent in real-time with minimal delay.

  • Horizontal Scaling: The WebSocket servers and Redis can be scaled horizontally to handle millions of users and location updates in real time.

  • Caching: The latest user locations are stored in Redis, allowing fast lookups and real-time geospatial computations without the need to query the database frequently.

Component Diagram

Find Nearby Friends HLD