KatInterview

Designing a Distributed In-Memory Caching Tier

System DesignModel answer
HardModel answerSystem Designsystem-designdistributed-systemscaching

Last updated

Design a scalable, resilient distributed in-memory cache shared by backend services, covering memory management, partitioning, failover, and database protection.

Architect a shared, multi-tenant distributed in-memory cache used by backend microservices to offload read traffic from underlying datastores.

Functional Requirements

  • Core Operations: Provide key-value primitives: GET(key), SET(key, value, ttl=None), DELETE(key), and atomic operations such as COMPARE_AND_SWAP(key, old_val, new_val).
  • Opaque Data: Keys and values are uninterpreted byte sequences.
  • Dynamic Topology: Support expanding or shrinking the cluster capacity with minimal remapping of existing entries.
  • Fault Tolerance: Tolerate single-node failures without losing overall service availability or exposing backend databases to excessive read surges.

System Assumptions & Constraints

  • Data Volatility: The cache is a volatile layer. Data loss is acceptable, but return of stale data must be strictly bounded and predictable.
  • Scope: Primary design targets a single-region deployment; multi-region extension can be discussed as a follow-up.

Key Discussion Areas

Part 1: API Semantics & Single-Node Architecture

  • Define key operations, including batch reads, optional time-to-live (TTL), and atomic writes.
  • Detail the layout of data in memory on a single node, managing lock contention, memory overhead, and allocation fragmentation.
  • Explain eviction algorithms (e.g., LRU derivatives) and expiry mechanisms under high concurrency.
  • Provide back-of-the-envelope scale calculations (RAM per node, cluster footprint, bandwidth).

Part 2: Partitioning, Topology, & Fault Tolerance

  • Compare client-side routing against proxy-based routing topologies.
  • Describe the key distribution scheme (e.g., Consistent Hashing with virtual nodes) and explain how topology updates occur without causing widespread cache misses.
  • Detail the consensus/membership management approach and failure detection.
  • Detail replication mechanisms (e.g., primary-backup) and explain potential data loss scenarios during node failover.

Part 3: Data Freshness & Datastore Protection

  • Define the consistency model and trace the lifetime of an update/invalidation path versus read-fills.
  • Address race conditions where out-of-order asynchronous fills overwrite fresh invalidation events.
  • Architect mechanisms to mitigate system-wide failure modes: Hot Keys, Thundering Herds (Cache Stampedes), and Cold Starts.

Hints

Hint 1

Divide the design into node-level memory mechanics, distributed partitioning/replication, and backend safeguard patterns.

Hint 2

Address the read-fill race condition using lease tokens (generation identifiers) issued during a cache miss.

Hint 3

Protect the database during key expiry spikes by combining single-flight request coalescing with probabilistic early expiration.