Bird
Raised Fist0

Design a Real-Time Location Tracking System - GPS, WebSocket, Geo-indexing

HLDhardInterview freq: medium
Choose your preparation mode4 modes available
🎯
Design a Real-Time Location Tracking System - GPS, WebSocket, Geo-indexing
45 min · senior
senior

You are building a location tracking platform for a 50 million daily active user (DAU) startup. Users' mobile devices send GPS coordinates frequently (every 5 seconds). The system must ingest, process, and deliver real-time location updates to subscribed clients (e.g., friends, fleet managers) with P99 latency under 200ms. Location data must be encrypted at rest and in transit. The system should support offline delivery of missed updates when clients reconnect. Geo-indexing is required to enable efficient spatial queries (e.g., find nearby users).

  • End-to-end encryption of location data in transit and at rest
  • Support 50M DAU with average update frequency of 1 update per 5 seconds
  • P99 end-to-end latency < 200ms for location update delivery
  • Offline delivery of missed location updates on client reconnect
  • Geo-indexing for efficient spatial queries (e.g., find nearby users)
  • WebSocket-based real-time streaming to subscribed clients
📋
Guided Steps
1/5
Step 1
Ingestion of GPS Location Updates
How will you ingest and process the high volume of GPS location updates from 50M DAU devices sending updates every 5 seconds?
💡 Hint
Consider a horizontally scalable, durable, and ordered ingestion pipeline that supports high write throughput and low latency.
Use Apache Kafka with topic partitioning keyed by user_id to ingest GPS updates - Kafka scales linearly with partitions and preserves order per user for consistent processing.
Mid-level: Names Kafka for ingestion. Senior: Explains user_id partition key ensures ordered updates per user and avoids cross-partition ordering issues.
⚠️Common Wrong Answer
Using HTTP REST endpoints with synchronous writes to a relational DB causes high latency and DB bottlenecks under 10M writes/sec.
Step 2
Real-Time Streaming to Clients
How will you deliver real-time location updates to subscribed clients with P99 latency under 200ms using WebSocket connections?
💡 Hint
Think about a scalable, stateful connection manager that can push updates efficiently and handle millions of concurrent WebSocket connections.
Use a horizontally scaled WebSocket gateway cluster (e.g., AWS API Gateway WebSocket or custom NGINX-based) fronted by Redis Streams for fan-out - Redis Streams supports consumer groups for efficient message delivery to subscribed clients.
Mid-level: Mentions WebSocket for real-time. Senior: Explains Redis Streams consumer groups enable scalable fan-out and how gateway clusters maintain sticky sessions for connection affinity.
⚠️Common Wrong Answer
Polling HTTP endpoints for updates leads to excessive load and latency spikes at 50M DAU.
Step 3
Geo-Indexing for Spatial Queries
How will you design geo-indexing to support efficient spatial queries like 'find nearby users' with low latency?
💡 Hint
Consider a geo-spatial database or index that supports fast radius or bounding box queries and scales horizontally.
Use Elasticsearch with geo_point fields and geo-distance queries for indexing user locations - Elasticsearch supports distributed geo queries with low latency and scales horizontally.
Mid-level: Names Elasticsearch for geo queries. Senior: Explains geo_point indexing, shard distribution, and how query latency is kept low with caching and pre-filtering.
⚠️Common Wrong Answer
Using relational DB with latitude/longitude columns and scanning entire tables causes high query latency and poor scalability.
Step 4
Offline Delivery and Delivery Guarantees
How will you ensure offline delivery of missed location updates when clients reconnect, guaranteeing no data loss?
💡 Hint
Think about durable message queues with offset tracking and client acknowledgments to enable replay of missed messages.
Use Kafka with consumer offset tracking per client and Redis for ephemeral session state - clients resume from last committed offset to receive missed updates on reconnect.
Mid-level: Mentions message queues for offline delivery. Senior: Explains Kafka offset management, consumer groups, and Redis TTL-based session tracking to detect offline clients.
⚠️Common Wrong Answer
Stateless WebSocket servers without offset tracking cause message loss during client disconnects.
Step 5
Security and Encryption
How will you ensure end-to-end encryption of location data both in transit and at rest?
💡 Hint
Consider TLS for transport and encryption at the storage layer with key management.
Use TLS 1.3 for all client-server WebSocket and REST communication, and encrypt data at rest using AWS KMS-managed keys in Kafka and Elasticsearch clusters.
Mid-level: Mentions TLS for transport encryption. Senior: Details TLS 1.3 benefits, envelope encryption for Kafka topics, and Elasticsearch encryption plugins with KMS integration.
⚠️Common Wrong Answer
Relying only on network-level encryption without encrypting data at rest risks data exposure if storage is compromised.
🔌
API Design Challenge
Sketch 3 core API endpoints for the system: how clients send location updates, how clients subscribe and receive real-time updates, and how clients acknowledge received updates.
  • How does the client securely send GPS coordinates to the backend?
  • How does the client establish and maintain a real-time subscription for updates?
  • How does the system track and handle delivery acknowledgments for offline support?
📊
Scoring Rubric
▼ Show
Must Have - 60%
  • Kafka ingestion with user_id partitioning for ordered writes
  • WebSocket-based real-time streaming with scalable connection management
  • Geo-indexing using Elasticsearch geo_point for spatial queries
  • Offline delivery using Kafka offset tracking and client acknowledgments
  • End-to-end encryption with TLS and encryption at rest
Good to Have - 30%
  • Redis Streams for efficient fan-out and consumer group management
  • Sticky sessions in WebSocket gateways for connection affinity
  • Client-side batching and encryption of GPS updates
Bonus - 10%
  • Use of envelope encryption with AWS KMS for key rotation
  • Metrics and monitoring for latency and delivery guarantees
Using HTTP polling for real-time updates causes excessive load and latency spikes at scale; for example, HTTP polling at 50M DAU results in 50 million reconnects per minute just to check for messages, which collapses the system under load.
Storing raw GPS updates in relational DB without partitioning leads to write bottlenecks and high latency due to lack of horizontal scalability and inefficient indexing.
Stateless WebSocket servers without offset tracking cause message loss during client disconnects because there is no mechanism to replay missed messages.
✅
Model Answer
▼ Reveal

This system ingests GPS updates from 50M DAU devices every 5 seconds using Apache Kafka with user_id partitioning to ensure ordered, scalable writes. Real-time updates are delivered via a horizontally scaled WebSocket gateway cluster fronted by Redis Streams consumer groups for efficient fan-out and sticky sessions for connection affinity. Elasticsearch with geo_point indexing supports low-latency spatial queries. Offline delivery is guaranteed by Kafka consumer offset tracking per client and Redis session state to detect offline clients and replay missed messages. End-to-end encryption is ensured with TLS 1.3 for transport and AWS KMS-managed encryption at rest in Kafka and Elasticsearch.

  • Use Kafka with user_id as partition key to maintain ordered GPS updates per user and scale ingestion linearly.
  • Deploy horizontally scaled WebSocket gateways with sticky sessions to maintain connection affinity and low latency.
  • Leverage Redis Streams consumer groups to fan out messages efficiently to subscribed clients.
  • Index user locations in Elasticsearch geo_point fields to support fast radius and bounding box queries with horizontal scalability.
  • Track Kafka consumer offsets per client and use Redis TTL keys to detect offline clients and enable replay of missed updates.
  • Encrypt all data in transit using TLS 1.3 and at rest using AWS KMS-managed keys integrated with Kafka and Elasticsearch.
  • Apache Kafka with user_id as partition key for ordered, scalable GPS update ingestion
  • WebSocket gateway cluster with Redis Streams consumer groups for real-time fan-out
  • Elasticsearch geo_point indexing for efficient distributed spatial queries
  • Kafka consumer offset tracking and Redis session state for offline delivery and replay
  • TLS 1.3 for transport encryption and AWS KMS-managed encryption at rest in Kafka and Elasticsearch
  • Client-side SDKs to batch and encrypt GPS updates before sending to backend
  • Horizontal scaling of WebSocket gateways with sticky sessions for connection affinity
  • Use of Redis TTL keys to detect offline clients and trigger delivery retries

Practice

(1/5)
1. A live streaming platform must ingest RTMP streams from millions of broadcasters with minimal latency and reliably convert them to HLS segments for viewers. Which component is best suited to buffer and order incoming RTMP packets before transcoding?
easy
A. A relational database with ACID transactions to store RTMP packets
B. An in-memory cache like Redis with TTL eviction
C. A distributed message queue like Kafka partitioned by stream ID
D. A CDN edge server caching HLS segments

Solution

  1. Step 1: Identify buffering and ordering needs

    RTMP packets must be buffered and ordered per stream before transcoding to maintain low latency and correct playback order.
  2. Step 2: Evaluate component suitability

    A relational DB (A) is too slow for high-throughput packet buffering. Redis (C) lacks strong ordering guarantees and durability. CDN edge (D) caches output, not input packets. Kafka (B) supports partitioning by stream ID, ensuring ordered, durable buffering.
  3. Final Answer:

    Option C -> Option C
  4. Quick Check:

    Kafka partitions ensure ordered, durable buffering per stream [check]
Hint: Kafka partitions per stream ensure ordered, durable buffering [check]
Common Mistakes:
2. For a globally distributed key-value store requiring low write latency and strong consistency, which replication approach is best and why?
medium
A. Single-leader replication with leader in primary region only
B. Asynchronous replication with conflict resolution on reads
C. Synchronous replication with majority quorum writes to all regions
D. Synchronous replication with local quorum writes per region and global reconciliation

Solution

  1. Step 1: Evaluate latency and consistency trade-offs

    Global synchronous quorum to all regions (A) causes high latency; asynchronous (B) sacrifices strong consistency.
  2. Step 2: Consider local quorum with global reconciliation

    Local quorum writes reduce latency while global reconciliation maintains strong consistency.
  3. Final Answer:

    Option D -> Option D
  4. Quick Check:

    Local quorum + global reconciliation balances latency and consistency [check]
Hint: Local quorum writes reduce latency, global reconciliation ensures consistency [check]
Common Mistakes:
3. At 10x scale, which component in the distributed cache system is most likely to fail first and why?
hard
A. Cache nodes because consistent hashing rings become imbalanced with many nodes
B. Backing data store because it cannot handle the increased read load
C. Cache nodes because memory pressure from replication and eviction overhead increases exponentially
D. Network because replication traffic saturates bandwidth causing high latency

Solution

  1. Step 1: Analyze consistent hashing at large scale

    With many nodes, virtual nodes and hash ring management become complex, causing imbalance and hotspots.
  2. Step 2: Compare other bottlenecks

    Memory pressure grows but is mitigated by eviction; network saturation happens later; backing store is usually scaled independently.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Consistent hashing imbalance causes cache node hotspots first [check]
Hint: Consistent hashing imbalance causes hotspots at scale [OK]
Common Mistakes:
4. At 10x scale, which component in the job portal system is most likely to become the first bottleneck, and why?
hard
A. WebSocket servers maintaining live candidate-job chat sessions, due to stateful connections limiting horizontal scaling
B. Relational database storing user profiles, due to complex joins and ACID constraints
C. Search index cluster, due to increased indexing latency from frequent job postings
D. Message queue cluster, due to partition limits causing uneven load distribution

Solution

  1. Step 1: Identify stateful components limiting scale

    WebSocket servers hold persistent connections bound to server memory, limiting resharding.
  2. Step 2: Compare other components

    Relational DB (A) can be scaled with sharding; search cluster (C) scales horizontally; message queues (D) can add partitions. Stateful WebSocket servers (B) face connection stickiness and memory limits first.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Stateful WebSocket connections -> scaling bottleneck at 10x load [check]
Hint: Stateful WebSocket servers limit horizontal scaling first [check]
Common Mistakes:
5. At 10x traffic scale, which component in a GeoDNS-based global traffic distribution system is most likely to fail first and why?
hard
A. Authoritative DNS servers, because they maintain stateful session info for each client
B. GeoDNS servers, because DNS query rate increases and caching TTLs limit cache effectiveness
C. Data center load balancers, because they cannot handle increased TCP connection counts
D. Network routers, because they cannot route Anycast IPs at scale

Solution

  1. Step 1: Identify GeoDNS scaling bottleneck

    GeoDNS servers handle DNS queries globally; at 10x scale, query volume spikes, and short TTLs reduce cache hits, increasing load.
  2. Step 2: Eliminate other options

    Authoritative DNS servers (A) are stateless per query. Data center load balancers (C) scale horizontally and are not the first bottleneck. Network routers (D) handle Anycast well at scale.
  3. Final Answer:

    Option B -> Option B
  4. Quick Check:

    GeoDNS query load spikes with limited caching cause first failure [check]
Hint: GeoDNS query load spikes first due to limited caching [check]
Common Mistakes:
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard