SXStudio

System Design Reading Notes 16: Nearby Friends

This is my reading notes for Chapter 2 in book “System Design Interview – An insider’s guide (Vol. 2)”.

Core Objective

To design a backend that enables a “Nearby Friends” feature, which allows users to see friends within a configurable radius (e.g., 5 miles), with low latency and real-time location updates. The design is targeted at handling scalability to serve up to 10 million concurrent users and over 13 million updates per second.

Nearby Friends

Problem Scope and Requirements

Functional Requirements

Non-Functional Requirements


System Design Components and Examples

  1. Mobile Client: Sends periodic location updates to the backend using a WebSocket connection, typically every 30 seconds.
  2. Load Balancer: Sits in front of WebSocket and API servers, distributing incoming location updates to manage load efficiently.
  3. WebSocket Servers: Maintains real-time connections with users, handling bi-directional communication for sending and receiving location updates.
    • Example: When User A sends a location update, the WebSocket server forwards it to friends of User A, subscribing only active friends to prevent excessive load.
  4. Redis Location Cache: Stores the most recent location of each active user with a Time-to-Live (TTL) expiry.
    • TTL Functionality: When User A goes offline, their location expires in Redis after TTL, keeping the cache manageable.
    • Example Key-Value: { user_id: { latitude: 37.7749, longitude: -122.4194, timestamp: 1600000000 }} with TTL ensuring User A’s location is purged if inactive.
  5. Redis Pub/Sub: Manages real-time distribution of location updates to subscribed users. Each active user has a dedicated channel that friends can subscribe to for receiving updates.
    • Example Workflow:
      • User A’s Location Update: Published to User_A_channel in Redis.
      • Subscribers: All active friends of User A (e.g., User B and User C) receive location updates if within the 5-mile radius.
  6. Location History Database: Archives location data for analytics and future recommendation features, like commonly visited places or route optimizations.

Location Update Flow: Example Walkthrough

  1. User A moves and sends a location update.
  2. WebSocket Server updates Redis Location Cache, refreshing the TTL and publishing the new location to User_A_channel.
  3. Redis Pub/Sub broadcasts the location update to active friends who are subscribed.
  4. WebSocket Servers for each friend (e.g., User B) calculate the distance between User B and User A. If within 5 miles, User B receives the update; otherwise, it’s dropped.

Detailed Component Insights

Redis Location Cache

Redis Pub/Sub Design

WebSocket Servers


Optimization Techniques and Advanced Features

Users with High Friend Counts

Adding/Removing Friends

Random Nearby Users (Advanced Feature)


Key API Design Considerations

  1. WebSocket APIs:
    • Periodic Location Update:
      • Request: { latitude, longitude, timestamp }
    • Receive Friend Updates: Friends’ current locations within the radius are pushed as updates arrive.
    • Friend Subscribe/Unsubscribe: When a friend is added/removed, the WebSocket server updates subscriptions accordingly.
  2. REST APIs (Supporting Functions):
    • Friend Management: Allows adding/removing friends, indirectly affecting nearby friend updates.
    • User Profile Management: Enables user details updates which may influence location sharing settings.

Key Points Summary

This expanded design incorporates efficient caching, real-time messaging, and load distribution for a highly scalable Nearby Friends feature capable of serving millions of users. The architecture leverages Redis and WebSocket for real-time updates and includes advanced optimizations for handling high-user scenarios.

Exit mobile version