← All problems
Dashboard
Courses
Problems
Badges
Theme
Sign inStart free
Problem 030 · Caching

Design a Distributed Key-Value Cache

Medium 55 min

Your company's services share one in-memory cache to keep load off their databases. A single cache server is now full and saturated, so you are asked to design a cache cluster. It holds 2 TB of data across many nodes, serves 120,000 operations per second at peak with sub-millisecond server time, and lets the team add or remove nodes without moving most keys. Some keys (a celebrity profile, a home-page config) are read thousands of times more often than the rest.

Functional requirements

  • Get, set and delete keys with an optional time-to-live
  • Spread keys across nodes and route each request to the right node
  • Add or remove nodes while the cluster keeps serving traffic
  • Evict the least recently used keys when a node runs out of memory

Non-functional requirements

  • Under 5 ms p95 for a get as seen by the calling service, including any proxy hop
  • Losing one node does not take the cluster down or flood the databases
  • Adding a node moves only about 1/N of the keys

Constraints and scale

  • 2 TB of cached data; nodes have 64 GB of memory each
  • Peak of 120,000 operations per second, 90% reads
  • Individual hot keys reach 20,000 reads per second

You will practise

  • Partition keys with consistent hashing and virtual nodes
  • Choose between client-side routing and a proxy tier
  • Handle hot keys and node failures without overloading the backing databases
Show 2 hints
  1. With hash(key) mod N, what fraction of keys move when N goes from 40 to 41?
  2. One key at 20,000 reads per second lands on one node. How could you spread that load?

Ready to build?

Open the canvas, place your components and explain your decisions. Your draft saves automatically.

Sign in to start

Failure scenarios

  • A cache node dies at peakOne of 40 cache nodes loses power at peak traffic. Its 50 GB of keys disappear, and every service whose keys lived there falls through to its database at once.

Run them from the workspace once your design is in place.