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.
11SYSTEM 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
CONCEPTKey inputsObject + version + relevant scope
CONCEPTOwnershipRoute key to cache node
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
STEP 1Popular objectConcentrated read demand
STEP 2MitigationCoalescing / eligible replicas
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
Decision
Chosen teaching approach
Reason
Authority
Catalogue database remains authoritative
Cache loss can become misses, not lost business truth
Ownership
Versioned partition map or consistent hashing
Bound remapping and coordinate migration
Freshness
Declared short TTL plus invalidation/version policy
Descriptions may tolerate a bounded stale window
Recovery
Coalescing and source admission ceiling
Node 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.
CHANGE AN ASSUMPTION. EXPLAIN THE RESULT.
Size the distributed cache
Estimate raw value bytes, then add replica copies. Explain why this still understates memory allocation.
120 million entries
210 GB raw values
320 GB with 2 copies
Value-byte estimate: 20 decimal GB
Entries x value bytes gives the raw value size. Multiply by total copies, then separately budget keys, metadata, overhead and recovery margin.
Chapter review: two questions
Attempt both questions before opening the model answers.
Question 1
Twenty million entries each use a 500-byte value and a 100-byte key. Estimate raw bytes with one extra full copy; name two costs excluded from that estimate.
Reveal model answer
Raw key-plus-value data is 20,000,000 x 600 bytes = 12 GB. One additional full copy gives 24 GB using decimal units. Allocator/entry metadata, routing structures, safety margin and network traffic are excluded. Exact provisioning needs implementation measurements.
Question 2
A cache node fails and source reads jump toward the full request rate. Explain three controls and the freshness/availability choice you would expose to the product.
Reveal model answer
Use per-key fetch coalescing, bounded source concurrency/admission and gradual warm-up; suitable replica/local copies may also help. Define whether stale descriptions can be served or reads should fail under pressure. Never silently apply that stale policy to authorization or balances.
Answers stay in this tab. Download before leaving. This practice is not graded or saved to your account.