Scrivio Interview library
ScenarioIntermediate7 min read · updated 12 November 2026

A Redis instance used as a cache for a Java service shows a single key read 200,000 times per second during a sale, one shard at 100 percent CPU and others idle. What is the problem and how do you fix it?

This is a hot key: every read of that one key goes to the single shard that owns its hash slot, so that shard's CPU saturates while the others sit idle, and adding shards does not help. We fix it by absorbing most reads in a small in-process cache such as Caffeine with a TTL of a second or so, by spreading the key over several copies (key#1 to key#N) read at random or by reading from replicas when slight staleness is fine, by preventing stampedes when the value expires, and by monitoring key access so hot keys are found before the next sale.

Why this question is asked

Sharding is supposed to make a cache scale: more shards, more capacity. A sale exposes the case where that promise fails, because traffic is not spread evenly across keys. One product, one banner or one configuration value can attract a large share of all requests. Interviewers ask this scenario because it tests whether we know how keys map to shards and can choose fixes that work at the application level. In this answer, we will recap how Redis Cluster places keys, see why one shard burns while the others idle, and then go through the fixes in the order we would apply them, finishing with stampedes and monitoring.

1A Quick Recap of How Redis Cluster Places Keys

Redis Cluster divides the key space into 16,384 hash slots. Each key is assigned to a slot by computing CRC16 of the key modulo 16,384, and each shard, a primary with its replicas, owns a range of slots. A client library such as Lettuce learns this map and sends each command straight to the shard that owns the slot. Within a shard, Redis executes commands on one main thread, so a single shard can serve on the order of 100,000 simple commands per second, depending on hardware and value size.

This design spreads load well when requests are spread over many keys. But one key always maps to one slot, and one slot always lives on one shard. However many shards we add, the reads of product:123 all land on the same one.

Step throughWhy all reads of one key hit one shard
Service instancesShard AShard BShard C
GET product:123 (slot 5,798) x 200,000/s
single thread at 100 percent CPU
slow replies, timeouts
other keys, light load
other keys, light load

5 messages in order. Play them, or step through them one at a time.

Adding shard D would change nothing for product:123.

2Why Adding Shards Does Not Help

At 200,000 reads per second for one key, the owning shard spends all its time parsing commands and writing replies. Latency rises for every key on that shard, not only the hot one, and clients start to time out, which often triggers retries that make the load even worse. Resharding moves slots between shards, but the hot slot just moves the problem to another machine.

So the fix has to reduce or spread the requests for that one key. We have three levers: serve the key without asking Redis, store the key in more than one slot, or read it from more than one machine.

3Fix 1: A Local Cache in Front of Redis

The most effective fix is an in-process cache inside each service instance. If 50 instances each keep product:123 in memory for one second, Redis sees at most about 50 reads per second for that key instead of 200,000. Caffeine is the standard choice in Java and is what Spring's cache abstraction uses for local caching.

Java
LoadingCache<String, Product> hot = Caffeine.newBuilder()
.maximumSize(10_000)
.expireAfterWrite(Duration.ofSeconds(1)) // short TTL keeps staleness small
.recordStats()
.build(key -> redisProducts.get(key)); // load from Redis on a miss
Product p = hot.get("product:123"); // first call per instance per second -> Redis
Product q = hot.get("product:123"); // served from memory, Redis not called
hot.stats().hitRate(); // 0.9997 during the sale

Notice the trade-off: a price change can take up to one second to appear on every instance. For a product page that is almost always acceptable; for stock counts at checkout we would still read the source of truth. Caffeine's get with a loader also loads each key only once per instance at a time, so concurrent requests on the same instance wait for one load instead of all calling Redis.

4Fix 2: Copies of the Key or Reads From Replicas

If local caching is not enough, or not allowed for a value, we can store the hot value under several keys, such as product:123#1 to product:123#8. These names hash to different slots and usually to different shards. Each reader picks one at random, so the load divides by eight. The cost is on writes: an update must write all eight copies, and for a moment some copies may be newer than others.

Java
int copies = 8;
String readKey = "product:123#" + (1 + ThreadLocalRandom.current().nextInt(copies));
String json = redis.opsForValue().get(readKey); // load spread over ~8 shards
// on update, write every copy
for (int i = 1; i <= copies; i++) {
redis.opsForValue().set("product:123#" + i, newJson, Duration.ofMinutes(10));
}

Do not use a hash tag here. Braces in a key, such as {product:123}#1, tell Redis Cluster to hash only the part in braces, which would put all copies in the same slot and defeat the purpose. The other option is reading from replicas: Lettuce can be configured with ReadFrom.REPLICA_PREFERRED so reads go to the shard's replicas, which multiplies read capacity by the number of replicas. Replication is asynchronous, so replica reads can be slightly stale.

5Preventing Stampedes on Expiry

A hot key creates a second danger when it expires. At that moment, thousands of requests miss together and all try to rebuild the value from the database, which can knock the database over. This is a cache stampede. We prevent it in three ways. Request coalescing makes concurrent misses share one load, which Caffeine already does inside one instance. Early refresh reloads the value in the background before it expires, using Caffeine's refreshAfterWrite, so readers keep getting the old value while the new one loads. And a short distributed lock, such as SET lock:product:123 NX EX 5 in Redis, lets only one instance rebuild the Redis value while others keep serving the previous one.

Adding a little random jitter to TTLs also helps, so that many hot keys created at the same moment do not all expire in the same second.

6Finding Hot Keys Before the Next Sale

Hot keys are predictable once we look for them. Redis can report frequently used keys with redis-cli --hotkeys, which needs an LFU eviction policy to be configured; check your version for the details. Per-shard metrics that show one shard's CPU or operations far above the others are the earliest signal, and client-side metrics that count reads per key prefix show which business object is responsible. MONITOR shows every command but is expensive, so it belongs in short sessions, not production dashboards.

In design reviews we ask which keys could get sale traffic, such as featured products, global configuration and counters, and plan local caching or copies for them in advance. Keys that aggregate everything, such as a single global counter incremented on every order, should be split by design, for example into 16 counters summed on read.

Step throughChoosing a fix for a hot key
  1. Can readers accept about a second of staleness?
    YesLocal Caffeine cacheNoNext question
  2. Can writes update several copies?
    YesCopies key#1..N, random readNoRead from replicas or redesign
  3. Protect expirycoalesce, refresh early, jitter TTL

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

7Follow-up questions

Why not just give that shard a bigger machine?

Redis executes commands on one main thread per instance, so more cores help little; newer versions can use I/O threads for network work, which helps somewhat. Even a much faster machine buys maybe a factor of two, while a local cache reduces the load by a factor of thousands.

Does Redis 6 client-side caching help here?

Yes. Redis 6 added server-assisted client-side caching, where the server tracks keys a client has read and sends invalidation messages when they change. That gives a local cache with faster invalidation than a fixed TTL. Client support varies, so check your client library and version.

What if the hot key is written, not read, such as a counter?

Copies of a written key do not work the same way. We split the counter into several keys, increment a random one, and sum them on read, or count locally in each instance and flush the totals to Redis every second.

8Conclusion

In this answer, we saw that a hot key overloads one shard because Redis Cluster maps every key to one of 16,384 slots on a single shard, so adding shards cannot help. A short-lived local Caffeine cache removes almost all of the load, copies of the key in different slots or replica reads spread what remains, coalescing and early refresh stop stampedes when the value expires, and per-shard and per-key monitoring finds hot keys before they hurt.

What to say in the interviewAbout 80 seconds aloud

Open with

This is a hot key. In Redis Cluster each key hashes to one of 16,384 slots, and each slot belongs to one shard, so all 200,000 reads per second for that key land on one shard, whose single command thread saturates while the others are idle.

Then, if they want more

Adding shards does not help because the key cannot be split. My first fix is a local in-process cache with Caffeine in front of Redis, with a very short TTL like one second.

With 50 instances, Redis then sees around 50 reads per second for that key, and the cost is up to a second of staleness, which is fine for a product page.

If that is not enough, I would store several copies of the value under keys like product:123#1 to #8, without hash tags so they land in different slots, read a random one, and write all copies on update, or read from replicas with Lettuce if slight staleness is acceptable.

I would also protect against a stampede when the key expires by coalescing loads, refreshing early with refreshAfterWrite and adding jitter to TTLs. Finally, I would monitor per-shard load and key access, using hot key detection, and plan for hot keys before the next sale.

About this answer

Last updated
12 November 2026
Versions it names
Redis 6
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.
Official documentation
Redis documentation, MongoDB manual
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 "A Redis instance used as a cache for a Java service shows a single key read 200,000...": https://scrivio-interview-library.netlify.app/databases/nosql-fundamentals/613aded214/