Scrivio Interview library
DesignAdvanced7 min read · updated 12 November 2026

Your distributed cache layer uses consistent hashing across 40 nodes. When a node fails, its keys move to neighbours, and the database is flooded by cache misses. How would you improve resilience?

The miss flood has two causes: the failed node's keys land on too few neighbours, and every miss goes straight to the database at once. I would spread a failed node's keys over many nodes with more virtual nodes, replicate hot keys to two or more nodes so their reads survive a failure, and protect the database with request coalescing (one load per key), rate limits on cache-fill queries and stale-while-revalidate. Health checks with hysteresis stop flapping nodes from churning the ring, and a load test that kills a node proves the improvement by measuring database load during the event.

Why this question is asked

A cache in front of a database works as a shield, and losing one cache node should be a small event. In many clusters it is not: the keys of the failed node all miss at the same moment, the database receives a burst of queries it was never sized for, and the outage spreads from the cache to everything that uses the database. Interviewers ask this design question to see whether we can reason about both the distribution of keys and the dynamics of a miss storm. In this answer, we will look at what happens when a node fails, then improve the key spread, protect hot keys, protect the database, stabilise membership, and finally test it.

1What Happens When a Node Fails

With 40 nodes, each holds about 2.5 percent of the keys. Suppose the cache hit rate is 98 percent and the cluster serves 200,000 reads per second, so the database normally sees 4,000 queries per second. When a node fails, its 2.5 percent of keys now miss, which is 5,000 extra reads per second, more than doubling database load to 9,000. If the node held some hot keys, the share of traffic is much larger than its share of keys. And all of these misses start in the same second, so many requests for the same popular key all miss and all query the database in parallel.

If the ring has few virtual points per node, the failed node's whole range passes to one or two neighbours, which now also take a wave of new keys and may run out of memory or CPU, evicting their own keys and causing more misses. That is how one failure becomes several.

2Spread a Failed Node's Keys With More Virtual Nodes

With virtual nodes, each physical node owns many small arcs of the ring, for example 160 points per node. When it fails, each arc passes to the next point clockwise, which belongs to a random other node, so its keys are spread over almost all 39 survivors, each taking about 1/39 of the extra load. No single neighbour is overwhelmed. This does not reduce the number of misses, but it keeps the remaining cache nodes healthy, so the event stays a single node failure.

3Replicate Hot Keys

Misses on cold keys are cheap; misses on hot keys are what flood the database. So we replicate hot keys to two or more nodes, typically the owner and its successor on the ring (successor replication). Writes or fills go to both, reads go to either, preferably the primary. When the owner fails, reads for that key go to the successor, which already has it, so they do not miss at all.

Replicating every key doubles memory, so we usually replicate only keys known to be hot, detected by sampling read counts in the client, or we use a small local in-process cache, such as Caffeine with a few seconds' expiry, in each service for the hottest keys. The local cache alone often absorbs most of the hot traffic during a node failure.

4Protect the Database From the Miss Storm

Whatever we do, some misses will reach the database, so the database path needs its own protection. The first and most effective is request coalescing, also called single flight: when many requests miss on the same key at once, only one of them loads from the database, and the others wait for its result. A popular product page requested 2,000 times in the second after a failure then costs one database query instead of 2,000.

Java
private final ConcurrentHashMap<String, CompletableFuture<Product>> inFlight = new ConcurrentHashMap<>();
CompletableFuture<Product> load(String key) {
CompletableFuture<Product> mine = new CompletableFuture<>();
CompletableFuture<Product> running = inFlight.putIfAbsent(key, mine);
if (running != null) return running; // someone is already loading it
CompletableFuture.supplyAsync(() -> fillLimiter.call(() -> db.loadProduct(key)), dbPool) // rate-limited fill
.whenComplete((v, e) -> {
inFlight.remove(key, mine); // the next miss loads again
if (e != null) mine.completeExceptionally(e); else mine.complete(v);
});
return mine;
}
// 2,000 concurrent misses for "product-7": 1 database query, 1,999 callers share the future
Step throughRead path with miss protection
  1. Local cache has the key?
    YesReturn itNoAsk the cache node
  2. Cache node (or replica) has it?
    YesReturn it; refresh in background if staleNoTreat as a miss
  3. Load for this key already in flight?
    YesWait for that resultNoStart one load
  4. Fill rate limit allows it?
    YesQuery the database, fill the cacheNoServe stale or a fallback

4 steps in order. Play them, or step through them one at a time.

Each step removes duplicate or excess load before it reaches the database.

Within one service instance this collapses duplicates completely; with 50 instances, the database sees at most 50 loads per key, which is still a huge reduction. Caffeine's AsyncLoadingCache gives the same behaviour per instance without writing the map ourselves. The second protection is a rate limit on cache-fill queries, the fillLimiter above, sized to what the database can absorb above its normal load; requests beyond it get a fallback or a fast error instead of joining a queue that would overwhelm the database. The third is stale-while-revalidate: entries carry a soft expiry and a hard expiry, and after the soft expiry we keep serving the old value while one request refreshes it in the background, so popular keys rarely miss at all.

5Stabilise Membership With Hysteresis

A node that flaps, failing health checks for a few seconds and then passing again, is worse than one that fails cleanly. Each removal moves its keys to other nodes, and each re-addition moves them back, cold, so every flap causes two waves of misses. Hysteresis means using different thresholds for removing and re-adding: remove a node after, say, 3 consecutive failed checks, but add it back only after 30 seconds of continuous success. Some clients also keep a node's position reserved for a short grace period, so a brief network blip does not reshuffle keys at all. The aim is that membership changes are rare and deliberate.

6Load-Test the Failure

Finally, we prove it. In a staging environment with production-like traffic, or carefully in production during a quiet hour, we kill a cache node and measure: database queries per second, database CPU and latency, cache hit rate, and application error rate, from a minute before to several minutes after. The goal is a written number, such as 'database load rises by at most 40 percent when one cache node fails, and returns to normal within 2 minutes'. We repeat the test after changes to the cache client, the database or traffic patterns, because the result depends on all of them.

Measured effect of each improvement (illustrative)
ConfigurationDatabase QPS after one node failsNeighbour node load
Few virtual nodes, no protection4,000 -> 20,000+ for minutesOne neighbour doubled
160 virtual nodes4,000 -> 9,000Spread over 39 nodes
+ hot-key replication and local cache4,000 -> 6,000Spread
+ single flight and fill rate limit4,000 -> 5,000, cappedSpread
The numbers are an example of what to measure, not a promise.

7Follow-up questions

Why not just make the database big enough for a full cache failure?

Because a database sized for 100 percent misses would cost many times more and sit idle almost always. It is cheaper to make cache failures small and to cap how much load a failure can push onto the database.

Does replication cause consistency problems?

It can: two copies of a key may briefly differ after an update. For a cache with short expiries and invalidation on writes, that is usually acceptable; for data that must never be stale, the cache is the wrong place to rely on.

What about cold start after the node comes back?

It returns empty, so its keys miss again. With hysteresis, a short warm-up period where the node receives only a share of traffic, and the same single-flight protection, the return is gentle.

8Conclusion

In this answer, we saw why one failed node in a 40-node consistent-hashing cache floods the database: its keys miss at once, hot keys miss many times in parallel, and with few virtual points a single neighbour takes the whole range. More virtual nodes spread the keys over all survivors, replicating hot keys avoids their misses, request coalescing, fill rate limits and stale-while-revalidate cap what reaches the database, hysteresis stops flapping nodes from churning the ring, and a load test that kills a node turns all of this into a measured guarantee.

What to say in the interviewAbout 100 seconds aloud

Open with

When a node fails, all of its keys miss in the same moment, and the hot ones miss thousands of times in parallel, so the database gets a burst it was never sized for.

Then, if they want more

If there are few virtual nodes, one neighbour also inherits the whole range and can fall over too. I would improve it in layers. First, more virtual nodes per server, say 150 or so, so a failed node's keys spread across all 39 survivors instead of one neighbour.

Second, replicate hot keys to the successor node, so reads for them don't miss at all when the owner fails, and keep a small local Caffeine cache in each service for the very hottest keys.

Third, protect the database directly: request coalescing so only one load per key is in flight and the other requests wait for it, a rate limit on cache-fill queries sized to what the database can absorb, with stale data or a fallback beyond that, and stale-while-revalidate so popular keys are refreshed in the background instead of expiring.

Fourth, health checks with hysteresis: remove a node after a few failed checks but re-add it only after a sustained healthy period, so a flapping node doesn't reshuffle keys back and forth.

And finally, I would load-test it by killing a cache node under production-like traffic and measuring database QPS, latency and errors during the event, and set a target such as database load rising no more than 40 percent.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Your distributed cache layer uses consistent hashing across 40 nodes. When a node fails...": https://scrivio-interview-library.netlify.app/databases/partitioning-and-sharding/125015381e/