Bird
Raised Fist0

Design a Distributed Cache - Consistent Hashing, Eviction, Replication

HLDhardInterview freq: medium
Choose your preparation mode4 modes available
▶
Request Trace
Step 1/5
Client SDK

Sends cache GET request

Client requests cached item key123

Protocol:WebSocket
BeforeClient has key123 to fetch, no cached data locally
AfterRequest sent to Cache Coordinator
Cache Coordinator

Performs consistent hashing

Maps key123 to Cache Node 3

Protocol:Consistent Hashing Lookup
BeforeCache Coordinator received key123 request
AfterCache Node 3 identified as responsible node
Cache Nodes (Redis)

Checks cache for key123

Cache Node 3 executes Redis GET for key123

Protocol:Redis GET
BeforeCache Node 3 has key123 in cache or not
AfterCache Node 3 returns cached value or miss
Replication Manager

Syncs updated cache data

Replicates key123 update to replica nodes if needed

Protocol:Replication Sync
BeforeCache Node 3 has updated or new key123 data
AfterReplica nodes updated with key123 data
Monitoring and Metrics Service

Records cache hit/miss and replication stats

Logs metrics for key123 request and replication latency

Protocol:Metrics Collection
BeforeCache operation completed for key123
AfterMetrics updated for monitoring dashboards
🏗️
Architecture Diagram
Architecture Diagram
┌───────────────┐
│  Client SDK   │
└───────┬───────┘
        │ WebSocket
        ▼
┌───────────────┐
│Cache Coordinator│
└───────┬───────┘
        │ Consistent Hashing Lookup
        ▼
┌───────────────────────┐
│Cache Nodes (Redis)│
└───────┬───────────────┘
        │ Redis GET / SET
        ▼
┌───────────────────┐
│Replication Manager│
└───────┬───────────┘
        │ Replication Sync
        ▼
┌───────────────────────────────┐
│Monitoring and Metrics Service  │
└───────────────────────────────┘

This architecture uses a Client SDK to send cache requests via WebSocket to a Cache Coordinator. The coordinator applies consistent hashing to route the request to the correct Cache Node in a Redis cluster. The Cache Node performs Redis GET or SET operations, applying eviction policies as needed. The Replication Manager ensures data consistency by syncing updates to replica nodes asynchronously. Meanwhile, the Monitoring and Metrics Service collects operational data to track cache hits, misses, and replication health. This design balances scalability with data availability and fault tolerance by combining consistent hashing for load distribution, eviction for memory management, and replication for durability.

Primary Focus

✓

Practice

(1/5)
1. In a system designed for 99.999% availability, at step 4 the Delivery Worker checks the message queue for pending retries. What happens if the message is not acknowledged by the consumer within the visibility timeout?
easy
A. The message is archived permanently without retry
B. The message is deleted immediately to prevent duplicates
C. The message becomes visible again for redelivery to another consumer
D. The Delivery Worker blocks until the consumer acknowledges

Solution

  1. Step 1: Trace Delivery Worker decision at Step 4 -- message visibility timeout

    If the message is not acknowledged within the visibility timeout, it becomes visible again for redelivery to ensure no message loss.
  2. Step 2: Eliminate incorrect options

    Immediate deletion (A) risks message loss; archiving without retry (C) breaks availability; blocking (D) reduces throughput and availability.
  3. Final Answer:

    Option C -> Option C
  4. Quick Check:

    Visibility timeout expiry -> message re-queued for retry [check]
Hint: Visibility timeout expiry -> message re-queued for retry [check]
Common Mistakes:
2. A real-time leaderboard system must handle millions of score updates per second with low latency and provide instant rank queries. Which component is best suited to store and update scores efficiently while maintaining sorted order?
easy
A. A NoSQL document store like MongoDB for flexible schema
B. A relational database with indexed score columns
C. A distributed log like Kafka for storing score events
D. Redis Sorted Sets to maintain scores and ranks in-memory

Solution

  1. Step 1: Identify data structure for sorted order and fast updates

    Redis Sorted Sets provide O(log N) insertion and rank queries with in-memory speed, ideal for real-time leaderboards.
  2. Step 2: Eliminate other options based on latency and ordering

    Relational DBs have higher write latency and complex locking; Kafka stores events but doesn't maintain sorted order; MongoDB lacks efficient sorted queries at scale.
  3. Final Answer:

    Option D -> Option D
  4. Quick Check:

    Redis Sorted Sets maintain sorted scores with low latency [check]
Hint: Sorted Sets keep scores sorted with fast rank queries [check]
Common Mistakes:
3. For an API Gateway that must support both REST and gRPC clients, which architecture best balances protocol support and transformation complexity?
medium
A. Use gRPC-only API Gateway and require REST clients to upgrade to gRPC
B. Use separate API Gateways for REST and gRPC, each optimized for its protocol
C. Use REST-only API Gateway and require gRPC clients to use a proxy converting gRPC to REST
D. Use a single API Gateway that performs protocol translation and transformation internally

Solution

  1. Step 1: Analyze protocol support requirements

    Supporting both REST and gRPC clients in one gateway reduces operational complexity and centralizes transformation logic.
  2. Step 2: Evaluate alternatives

    Use separate API Gateways for REST and gRPC, each optimized for its protocol increases deployment and maintenance overhead. Use REST-only API Gateway and require gRPC clients to use a proxy converting gRPC to REST adds latency and complexity on client side. Use gRPC-only API Gateway and require REST clients to upgrade to gRPC is impractical as REST clients are widespread.
  3. Final Answer:

    Option D -> Option D
  4. Quick Check:

    Single gateway with protocol translation balances support and complexity [check]
Hint: One gateway handles both protocols with internal translation [check]
Common Mistakes:
4. For a high-traffic API, which approach better captures user experience degradation under load: reporting average latency or reporting P95 latency, and why?
medium
A. P95 latency, because it highlights the slowest 5% of requests affecting users
B. Average latency, because it is easier to compute in real-time
C. Average latency, because it summarizes all requests evenly
D. P95 latency, because it ignores outliers and focuses on median performance

Solution

  1. Step 1: Understand average latency limitations

    Average latency can be skewed by many fast requests and hide slow request impact, thus missing user experience degradation.
  2. Step 2: Why P95 latency is better

    P95 latency captures the slowest 5% of requests, which often cause user frustration and indicate performance issues under load.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    P95 latency reveals slow requests impacting users better than average [check]
Hint: P95 reveals slow requests; average hides tail latency [OK]
Common Mistakes:
5. For a system requiring cost optimization with infrequent data access but occasional retrieval within hours, which storage architecture is preferable?
medium
A. Implement warm storage with HDDs and cold storage with object storage, migrating data based on access patterns
B. Use only hot storage with SSDs for all data to ensure fastest access
C. Archive all data immediately to tape storage to minimize cost
D. Store all data in cold storage with no tiering to simplify design

Solution

  1. Step 1: Analyze access and cost requirements

    Data is infrequently accessed but needs retrieval within hours, so cold storage alone is too slow.
  2. Step 2: Evaluate tiering benefits

    Warm storage offers moderate cost and latency; cold storage reduces cost further. Tiering balances cost and retrieval time.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Warm + cold tiering balances cost and retrieval latency [check]
Hint: Warm + cold tiering balances cost and latency [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