Quick revision
The short answer
We keep a Top-K per minute (TOPK.RESERVE login:topk:<minute> 100 2000 7 0.925, fed by TOPK.INCRBY from the gateways) to hold the 100 heaviest IPs, and a Count-Min Sketch per minute so any IP's approximate count can be looked up; it can overestimate but never underestimates, and its error is relative to all traffic in the window. Gateways aggregate locally for a second and send batched increments, so Redis sees thousands of commands instead of millions. Keys rotate per minute with TTLs, and the results feed blocking only after an exact counter check and allowlists, because a false positive locks out real users.
What to remember
In this answer, we designed heavy-hitter detection with a per-minute Top-K that keeps the 100 heaviest IPs and a per-minute Count-Min Sketch that estimates any IP's count, never too low and at most 0.1 percent of the window's total too high. Gateways aggregate for a second and send batched increments, keys rotate per minute with TTLs and spread across shards, and blocking happens only after exact counters for candidates, allowlists for shared addresses and failure ratios, with graduated, expiring actions.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
We keep a Top-K per minute (TOPK.RESERVE login:topk:<minute> 100 2000 7 0.925, fed by TOPK.INCRBY from the gateways) to hold the 100 heaviest IPs, and a Count-Min Sketch per minute so any IP's approximate count can be looked up; it can overestimate but never underestimates, and its error is relative to all traffic in the window. Gateways aggregate locally for a second and send batched increments, so Redis sees thousands of commands instead of millions. Keys rotate per minute with TTLs, and the results feed blocking only after an exact counter check and allowlists, because a false positive locks out real users.
Why this question is asked
During a credential-stuffing attack, attackers try stolen username and password pairs at a huge rate, and every minute of delay means more accounts taken over. Counting exactly per IP at 2 million requests a second across hundreds of thousands of sources is expensive and slow to query; probabilistic structures give the answer within seconds for a few megabytes. Interviewers ask this to see whether we can choose the right structures, understand their errors in concrete numbers and turn approximate signals into safe actions. In this answer, we will set out the numbers, design the Top-K and Count-Min Sketch keys, reduce the write load at the gateways, rotate windows, and feed the results into blocking safely.
1The Numbers We Are Designing For
Two million login requests a second is 120 million a minute. Hundreds of thousands of source IPs means an exact counter per IP per minute would be hundreds of thousands of keys created and expired every minute, and finding the top 100 would mean sorting them. We want the heaviest sources within a few seconds, and we can accept approximate counts as long as we never block on them alone.
Two Redis structures fit. Top-K maintains the k most frequent items in a stream using a small, fixed amount of memory. A Count-Min Sketch estimates the count of any item, also in fixed memory. Together they answer 'who is loudest' and 'how loud is this one'.
2Top-K for the Heaviest Sources
TOPK.RESERVE takes k, then width, depth and decay, which size the internal sketch it uses to estimate counts: here 100 items to keep, a width of 2,000 counters in each of 7 rows, and a decay of 0.925 that lets new heavy items push out old ones. Larger width and depth mean more accurate counts and more memory, still only kilobytes to a few hundred kilobytes.
The counts from TOPK.LIST are estimates; for heavy items they are close, which is all we need to rank them.
3Count-Min Sketch for Any IP's Count
Top-K only remembers the top 100. When a support engineer or a rule asks 'how many attempts did this IP make in the last minute?', we use a Count-Min Sketch. CMS.INITBYPROB key error probability sizes it so that, with the given probability, the overestimate is at most error times the total count in the sketch.
| Structure | Answers | Error | Memory |
|---|---|---|---|
| Top-K | the 100 heaviest IPs this minute | rank close for heavy items | fixed, small |
| Count-Min Sketch | approximate count for any IP | never low; up to 0.1% of total high | fixed, small |
| Exact counter | true count for one IP | none | one key per IP |
Notice the second answer. The error bound is relative to everything in the window: 0.001 times 120 million requests is 120,000. With 99 percent probability, any estimate is at most 120,000 too high, and it is never too low. So for heavy IPs with millions of attempts the estimate is useful, and for quiet ones it is close to meaningless. That is exactly why we never block on a sketch estimate alone.
4Aggregating at the Gateways
Sending one command per login request would mean 2 million Redis commands a second, which is a cluster in itself. Instead, each gateway counts locally in memory for one second, a map from IP to count, and then sends one TOPK.INCRBY and one CMS.INCRBY with all its IPs and their counts, split into chunks of a few thousand items. With 50 gateways, Redis sees a few hundred commands a second, each carrying many increments.
The local map is bounded: if a gateway sees too many distinct IPs in one second, it flushes early. We lose up to a second of freshness, which is well within 'within seconds'.
7 messages in order. Play them, or step through them one at a time.
5Rotating Windows and Spreading Load
Keys are per minute, such as login:topk:202611121405, created by the first writer of the minute and given a TTL of a few minutes. Old traffic drops out as windows rotate, so an IP that stopped attacking leaves the top list within a minute, and memory stays flat. For a smoother view, the detector reads the current and previous minute together.
Different minutes, and, if we run several regions, different regions, are separate keys, so they hash to different slots and spread across shards. One minute's Top-K receives only a few hundred commands a second, which a single shard handles easily.
6Feeding Results Into Blocking Safely
A false positive here locks real users out, so candidates go through checks before any action. For each of the 100 candidates, the gateways keep an exact counter (INCR rate:<ip>:<minute> with a TTL) only for those IPs, which is cheap because there are only 100. We check allowlists for large shared addresses: mobile carriers and corporate proxies put thousands of real users behind one IP, and a university's NAT can look like an attacker. We also look at the failure ratio: an attacker's logins mostly fail, a busy legitimate proxy's mostly succeed.
The action is graduated: a challenge or a strict rate limit first, then a short block (SET block:203.0.113.7 1 EX 600) that expires on its own, with every block logged and easy to lift. Blocks feed back into a dashboard so the security team can see what the system did.
7Follow-up questions
How would you handle an attack spread evenly over many addresses?
Per-IP heavy hitters will not find it, because no single IP is loud. We aggregate on other dimensions instead: the /24 network or ASN of the source, the targeted account (many IPs against one account), device or client fingerprints, and the global failure rate. We also make each attempt less valuable to the attacker: checking passwords against known breached lists, step-up verification for logins from new devices, and multi-factor authentication. Top-K on subnets or fingerprints works the same way as on IPs.
Why not use a plain sorted set with ZINCRBY per IP?
It would be exact, but with hundreds of thousands of IPs per minute it grows large, and the write load is per IP rather than per batch item in fixed memory. A sorted set is fine for the 100 candidates; for the whole stream, Top-K and a sketch keep memory fixed regardless of how many sources the attacker uses.
What does the decay parameter in Top-K do?
When a new item collides with a counter held by another item, the existing counter is decremented with a probability based on the decay, so items that stop appearing gradually lose their place to new heavy ones. A value close to 1 changes the top list more slowly.
8Conclusion
In this answer, we designed heavy-hitter detection with a per-minute Top-K that keeps the 100 heaviest IPs and a per-minute Count-Min Sketch that estimates any IP's count, never too low and at most 0.1 percent of the window's total too high. Gateways aggregate for a second and send batched increments, keys rotate per minute with TTLs and spread across shards, and blocking happens only after exact counters for candidates, allowlists for shared addresses and failure ratios, with graduated, expiring actions.
Open with
I'd use a Top-K per one-minute window, created with TOPK.RESERVE login:topk and the minute, 100, 2000, 7, 0.925, so it keeps the 100 heaviest IPs, and TOPK.LIST WITHCOUNT gives them with estimated counts.
Then, if they want more
Next to it, a Count-Min Sketch per minute, CMS.INITBYPROB with error 0.001 and probability 0.01, so I can query any IP's approximate count.
It can overestimate but never underestimates, and the error is relative to the total in the window, so at 120 million requests a minute it can be off by up to 120,000, which is fine for heavy IPs and meaningless for quiet ones.
I wouldn't send 2 million commands a second: each gateway counts locally for a second and sends one batched TOPK.INCRBY and CMS.INCRBY, so Redis sees a few hundred commands a second. Keys rotate per minute with TTLs, so old traffic drops out, and windows or regions spread across shards. For blocking, Top-K only nominates candidates.
For those hundred I'd keep exact counters, check allowlists for big shared NAT and carrier addresses, and look at the failure ratio, because a false positive locks out real users. Then the action is graduated, a challenge or rate limit first, then a short, expiring block, all logged.
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 "Design heavy-hitter detection with Redis probabilistic structures, explain the error...": https://scrivio-interview-library.netlify.app/databases/redis-patterns/c87add193f/