Buildfest Learn System DesignRead and experiment freely · Chapter activities stay in this tab
Practice librarySystem DesignWorked interview distributed cache

LESSON 11 • SYSTEM DESIGN

Worked interview distributed cacheShare the cached work.
Protect the source of truth.

Worked interview distributed cache. Learn the concepts, explore a model and defend your design.

DEFINITION

A distributed cache spreads reusable key-value entries across a group of machines. The service must decide which node owns a key, how entries expire, what happens during node loss and how the authoritative source survives a surge of misses. This worked case uses a publicly reported Google interview topic with original teaching constraints and an original solution.

A cache key must identify the representation
  1. CONCEPTKey inputsObject + version + relevant scope
  2. CONCEPTOwnershipRoute key to cache node
  3. CONCEPTFallbackAuthoritative database

A shared cache must not mix personalized or permission-sensitive representations.

CONCEPT 1

Google reported topic · A distributed cache

1 Clarify the teaching problem

Design a shared cache for public catalogue descriptions. Support get, put and delete. Assume entries may be briefly stale and all authoritative data remains in a database. Ask whether the cache must survive loss without misses; here it does not. Exclude financial balances and authorization decisions. These constraints are chosen for this example, not attributed to Google.

2 Estimate the useful size

Assume 10 million cached entries, each with a 1 KB value and a 100-byte key. Raw key-plus-value size is about 11 GB in decimal units. With one additional copy, raw stored bytes become about 22 GB before metadata, allocator overhead and safety margin. Assume a desired 90 percent hit rate at 100,000 reads per second; source reads would be around 10,000 per second before correlated misses and refresh work.

3 Choose an API and ownership map

Use get(key), put(key,value,ttl) and delete(key). The client must use the same routing version as the cache cluster. A simple hash modulo node count is easy initially, but adding a node can move many keys. Consistent hashing places keys and nodes on a logical ring so a key can belong to the next node. A stable partition-to-node map is another option. Both reduce avoidable remapping but still need a membership and migration policy. Authentication, key-space isolation and maximum entry size are part of the service contract.

CONCEPT 2

Distributed cache · Complete the reasoning

4 Explain one read and one write

On a hit, return a valid entry. On a miss, let one request fetch from the source, fill the cache with a TTL and share the result with waiting requests for that key. On a source update, invalidate the appropriate keys or use a version-aware policy. Deleting after a write still permits an old in-flight read to refill, so explicitly accept a bounded stale window or add stronger coordination where required.

5 Handle the failures

A failed cache node creates misses or requires reading a replica. Limit concurrent source fetches so that the database does not collapse during recovery. Warm important entries gradually. A replica improves availability of cached values but can itself lag; it does not remove the freshness policy. For a single extremely popular key, replicate or locally cache suitable values rather than expecting more ordinary shards to divide that one key automatically.

6 Answer the likely deep dives

How do you evict? Choose an LRU-like or frequency-aware policy that matches measured use and bound memory. How do you prevent a stampede? Combine per-key request coalescing with suitable expiry variation and admission limits. What if a cache write fails? The database remains authoritative; treat the fill as best effort under this example's read policy. What would you measure? Hits, source load, tail latency, memory, evictions, stale responses and behavior during node replacement.

A concise model answer

I would distribute cache ownership with an explicit routing map, bound entry lifetimes and memory, and keep the source database authoritative. My main failure risk is a surge of misses after node loss, so I would coalesce fetches and cap source traffic. If the product required strongly current reads, I would revisit the contract rather than claiming this cache provides them.

A hot key needs more than uniform hashing
  1. STEP 1Popular objectConcentrated read demand
  2. STEP 2MitigationCoalescing / eligible replicas
  3. STEP 3ProtectionBound fallback database work

The constraints and solution are an original teaching adaptation of a reported interview topic.

Key learning points

  • Define whether stale data and misses are acceptable before choosing a protocol.
  • Estimate raw bytes separately from replication and implementation overhead.
  • Make ownership/routing changes explicit.
  • Protect the source during cache loss and address hot keys.

Compare the important ideas

Worked interview distributed cache: concepts and trade-offs
DecisionChosen teaching approachReason
AuthorityCatalogue database remains authoritativeCache loss can become misses, not lost business truth
OwnershipVersioned partition map or consistent hashingBound remapping and coordinate migration
FreshnessDeclared short TTL plus invalidation/version policyDescriptions may tolerate a bounded stale window
RecoveryCoalescing and source admission ceilingNode loss must not collapse the database

WORKED EXAMPLE

One miss with many waiting readers

If 100 readers concurrently miss the same key, one in-flight fetch can obtain the source value and share it with the waiting readers. It is not 100 new authoritative writes. Apply a timeout and admission ceiling, and decide how errors are propagated. Fetch coalescing improves this case but does not collapse misses for 100 different keys into one operation.

Further reading

Original teaching explanations by Buildfest Learn. Check these primary references for the underlying concepts and implementation details.

Interview cases adapt publicly reported topics with original teaching constraints and solutions. They are not official company questions or answer keys.