Scrivio Interview library
ScenarioIntermediate6 min read · updated 13 November 2026

A live-streaming service counts total views per stream with INCR on one key. A popular stream reaches 250,000 increments per second, and the Redis Cluster node that owns the key's slot pins at 100 percent CPU while other nodes are idle.

Redesign the counter so the load spreads, and explain what readers of the count now have to do.

One key lives in one slot on one primary, so 250,000 INCRs per second on it can never spread. I would split the counter into N sub-keys without a shared hash tag, such as views:s1:0 to views:s1:63, so they land on different nodes, and have each write pick one; readers then sum the sub-keys or read a total that a job refreshes every second. Better still, each pod counts locally and sends one INCRBY per second, cutting Redis writes to a few hundred per second, which is fine for view counts but not for billing.

Why this question is asked

Hot keys are the one problem that adding nodes cannot fix, and a viral counter is the classic example. Interviewers ask this because the redesign forces a real trade-off: we spread the writes, but the readers now have more work and a slightly older number. In this answer, we will see why one key cannot scale, split the counter across slots, look at what readers must do, then go further with aggregation in the application, and finish by deciding how accurate the count needs to be.

1Why One Key Cannot Spread

Redis Cluster divides the keyspace into 16,384 slots. Each key belongs to exactly one slot, computed from a hash of the key name, and each slot is owned by exactly one primary at a time. So every INCR on views:s1 goes to the same primary and is executed by its single main thread. At 250,000 increments per second, that thread is at its limit, whatever the other nodes are doing.

Replicas do not help. They copy the primary's writes, but they cannot accept writes themselves, and every INCR is a write. Adding nodes to the cluster does not help either, because the slot can only move as a whole. The only way to spread the load is to stop having one key.

2Splitting the Counter Into Sub-Keys

We replace one key with N keys: views:s1:0, views:s1:1, up to views:s1:63 for N = 64. Each write increments one of them, chosen at random or by the writing instance's id. Because the names differ and contain no shared hash tag (a part in curly braces, which would force them into one slot), they hash to different slots and therefore spread across the primaries.

Java
// Writer: one INCR on a random shard of the counter
int shard = ThreadLocalRandom.current().nextInt(64);
redis.opsForValue().increment("views:s1:" + shard); // INCR views:s1:37 -> 3912
// Do NOT do this: the hash tag puts all 64 keys in one slot
// "views:{s1}:" + shard

With 64 sub-keys on a cluster of, say, 8 primaries, each primary receives roughly 31,000 increments per second for this stream instead of one primary receiving 250,000. The increments are still atomic per sub-key, and the total is still exact, just spread out.

3What Readers Have to Do Now

The total is no longer in one key. A reader must fetch all 64 sub-keys and add them up. In a cluster, MGET only works when all keys are in the same slot, so a cluster-aware client either splits the read into one request per node or the reader sends 64 GETs in a pipeline. Lettuce's cluster API does the grouping by node for us. That is fine for an occasional reader, but if thousands of viewers poll the count every few seconds, the summing becomes its own hot workload.

So the usual pattern is a small job that sums the sub-keys once per second and writes the total to views:s1:total. Viewers read that one key, which is cheap and can be cached, and accept that the displayed number lags by up to a second, which nobody watching a stream will notice.

Step throughWriting and reading a split counter
  1. Viewer eventa pod receives a view
  2. INCR views:s1:<random 0-63>lands on one of many primaries
  3. Every second: aggregator jobGET all 64 sub-keys, grouped by node
  4. SET views:s1:total <sum>one small key for readers
  5. Viewers read views:s1:totalcheap, may lag by up to a second

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

4Better Still: Aggregate in the Application

Most of the 250,000 increments carry the same information: add one. If each application pod counts views in memory, with a LongAdder per stream for example, and flushes the accumulated number once per second with INCRBY, the load on Redis drops dramatically. With 200 pods, that is 200 INCRBY calls per second for this stream instead of 250,000 INCRs.

Java
@Component
class ViewCounter {
private final ConcurrentHashMap<String, LongAdder> pending = new ConcurrentHashMap<>();
private final StringRedisTemplate redis;
ViewCounter(StringRedisTemplate redis) { this.redis = redis; }
void record(String streamId) {
pending.computeIfAbsent(streamId, k -> new LongAdder()).increment(); // memory only
}
@Scheduled(fixedRate = 1000)
void flush() {
pending.forEach((id, adder) -> {
long n = adder.sumThenReset();
if (n > 0) redis.opsForValue().increment("views:" + id, n); // INCRBY views:s1 1250
});
}
}
// 250,000 INCR/s across 200 pods -> about 200 INCRBY/s

At a few hundred writes per second, even a single key is no longer hot, so for many systems this alone is enough and the sub-keys become unnecessary. For the most extreme streams, the two ideas combine: local aggregation plus a handful of sub-keys.

5Deciding How Accurate the Count Must Be

Both designs give up something. The split counter gives a total that lags slightly. Local aggregation can lose up to a second of counts if a pod crashes before flushing, and a flush that fails needs a retry policy that does not double count.

For a public view count, losing a few hundred views out of millions is invisible and acceptable. For counts that drive money, such as ad impressions billed to customers or creator payouts, it is not. Those should come from a durable event log: each view is an event in Kafka, and a stream processor computes exact counts with replayable state. Redis can still show the fast, approximate number on screen, while the billing number comes from the log.

Options and what they cost
DesignRedis writes per secondReader costAccuracy
Single key250,000 on one primaryone GETexact, but does not scale
64 sub-keys250,000 spread over nodessum 64 keys or read totalexact, total lags about 1 s
Local aggregationabout 200one GETmay lose about 1 s on a crash
Event log (Kafka)none for billingstream processorexact and replayable

6Follow-up questions

How would you choose N, and can it change later?

N should comfortably exceed the number of primaries so the sub-keys spread evenly, and give each sub-key a load well below one thread's capacity: 64 sub-keys over 8 primaries gives about 31,000 writes per second each at peak, with room to grow. N can grow later if readers sum a range that covers both old and new sub-keys, for example by storing N in a small key or by always reading up to the maximum N ever used; shrinking needs a migration that folds the extra sub-keys into the remaining ones.

Why not use a hash with 64 fields instead of 64 keys?

A hash is one key, so it lives in one slot on one primary, and HINCRBY on its fields would hit the same thread as before. Only separate keys in different slots spread the load.

Could the counter just live in the application database?

Not at 250,000 writes per second on one row, which would be an even worse hot spot. The same ideas, local aggregation and periodic flushes, are what make a database counter workable.

7Conclusion

In this answer, we saw that a single counter key is bound to one slot and one primary thread, so neither replicas nor more nodes help. Splitting it into sub-keys without a shared hash tag spreads the writes, at the cost of readers summing them or reading a total refreshed every second. Aggregating in each pod with one INCRBY per second removes most of the load, and the accuracy needed decides the design: approximate view counts are fine this way, while billing-grade counts belong in a durable event log.

What to say in the interviewAbout 80 seconds aloud

Open with

The problem is that one key maps to one slot, which lives on one primary with one main thread, so 250,000 INCRs per second on it can never spread, and replicas do not help because every INCR is a write that must go to the primary.

Then, if they want more

I would split the counter into N sub-keys, for example views:s1:0 to views:s1:63, deliberately without a shared hash tag so they land in different slots, and each write increments one picked at random or by instance id.

Readers then need to sum the 64 values, grouped by node, or more practically a job sums them every second and writes a total key that viewers read, accepting that the number lags slightly.

Even better is aggregating in the application: each pod counts locally, say with a LongAdder, and sends one INCRBY per second, which turns 250,000 writes per second into a few hundred.

Then I would agree the accuracy needed: losing a second of counts when a pod crashes is fine for view counts, but anything billing-grade should come from a durable event log such as Kafka, with Redis only showing the live number.

About this answer

Last updated
13 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.
Official documentation
Redis documentation
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 "Redesign the counter so the load spreads, and explain what readers of the count now have...": https://scrivio-interview-library.netlify.app/databases/redis-internals/8fedf2eb3e/