Scrivio Interview library
DebuggingIntermediate6 min read · updated 12 November 2026

Redis latency spikes to 200 ms every few minutes. redis-cli --bigkeys shows a hash with 4 million fields, and the slow log shows HGETALL and DEL on it. Explain why this hurts and how you would fix it without downtime.

Redis runs commands on a single main thread, so HGETALL on a 4-million-field hash and a synchronous DEL that frees it both block every other client for the whole time they take, which shows up as 200 ms spikes. We stop the stalls by deleting with UNLINK or lazyfree settings so memory is freed in a background thread, and by reading with HSCAN in pages. Then we redesign the data into many small hashes spread across buckets and cluster slots, and monitor with SLOWLOG, LATENCY DOCTOR and MEMORY USAGE with alerts on big keys.

Why this question is asked

Big keys are one of the most common causes of mysterious Redis latency. Everything is fast on average, yet every few minutes every client sees a pause, timeouts fire, and circuit breakers open in services that have nothing to do with the big key. Interviewers ask this debugging question to see whether we understand Redis's execution model well enough to connect a slow command on one key to latency for everyone, and whether we can fix it without taking the service down. In this answer, we will recap the single-thread model, see why these two commands hurt, stop the immediate pain, redesign the data, and set up monitoring so it does not happen again.

1A Quick Recap of Redis's Single Main Thread

Redis handles network I/O with helper threads in recent versions, but it still executes commands one at a time on one main thread. That is what makes every command atomic and most commands take microseconds. The other side of the same design is that while one command runs, every other command waits in line, whatever key it is for.

So the latency a client sees is not only the cost of its own command but also the cost of whatever ran just before it. A 200 ms command on one key is a 200 ms pause for the entire instance.

2Why HGETALL and DEL Hurt on This Key

HGETALL is O(n): with 4 million fields, Redis walks the whole hash and builds a reply of 8 million strings, the field names and values, in one go. Building and sending that reply can take hundreds of milliseconds and briefly uses a lot of memory for the output buffer. The slow log, which records commands that run longer than a threshold, shows it clearly.

YAML
SLOWLOG GET 3
# 1) id 4211, duration 212000 us, command: HGETALL user:prefs
# 2) id 4210, duration 187000 us, command: DEL user:prefs:old
# 3) ...
MEMORY USAGE user:prefs SAMPLES 0
# (integer) 412000000 about 400 MB in one key
Step throughOne big command blocks everyone
Report jobRedis main threadCheckout service
HGETALL user:prefs (4M fields)
GET cart:991
walk 4M fields, build reply: about 200 ms
8M strings
now runs GET cart:991
reply after about 200 ms instead of 0.2 ms

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

The checkout request had nothing to do with the big key, but it waited for it.

DEL is less obvious. Deleting a key with millions of elements means freeing millions of small memory allocations, and plain DEL does that synchronously on the main thread. So a cleanup job that deletes an old copy of the hash causes a stall of its own. If the key had an expiry, the same stall would happen whenever it expired, unless lazy freeing is configured for expiry too.

3Stopping the Pain Without Downtime

The first fixes change how we use the key, not the key itself, so they can ship immediately. For deletes, we replace DEL with UNLINK, available since Redis 4.0. UNLINK removes the key from the keyspace at once and hands the memory to a background thread to free, so the main thread is blocked only for a moment. We can also make plain DEL behave the same way with lazyfree-lazy-user-del, and protect expiry and eviction with lazyfree-lazy-expire and lazyfree-lazy-eviction.

Text
UNLINK user:prefs:old # (integer) 1, returns immediately
CONFIG SET lazyfree-lazy-user-del yes # DEL now frees in the background (Redis 6.0+)
CONFIG SET lazyfree-lazy-expire yes
# Read in pages instead of HGETALL
HSCAN user:prefs 0 COUNT 1000 # 1) "2048" 2) about 1000 field-value pairs
HSCAN user:prefs 2048 COUNT 1000 # continue until the cursor is "0"

For reads, the report job switches from HGETALL to HSCAN. Each call returns a cursor and a batch of roughly COUNT entries, so the job takes many short turns and other clients run in between. In Spring Data Redis, opsForHash().scan(key, ScanOptions.scanOptions().count(1000).build()) returns a cursor we iterate and must close. The job takes a little longer in total, but nobody else notices it.

Remember to persist configuration changes made with CONFIG SET in redis.conf or our managed configuration, or they disappear at the next restart.

4Redesigning the Data Into Small Keys

The lasting fix is not to have a 4-million-field key at all. We split it into many smaller hashes by hashing the field name into a fixed number of buckets. With 1,024 buckets, each hash holds about 4,000 fields, a size where every command is fast, and in a Redis Cluster the buckets spread across slots and therefore across nodes.

Java
static String bucketKey(String userId) {
int bucket = (userId.hashCode() & 0x7fffffff) % 1024;
return "user:prefs:" + bucket; // user:prefs:0 .. user:prefs:1023
}
redis.opsForHash().put(bucketKey("u-981"), "u-981", prefsJson); // HSET user:prefs:377 u-981 ...
redis.opsForHash().get(bucketKey("u-981"), "u-981"); // HGET, still O(1)

Notice that single-field reads stay O(1) and need no change in callers beyond computing the key. Small hashes also benefit from Redis's compact encoding for small collections, which can reduce memory noticeably. To migrate without downtime, we write to the new bucketed keys and read from the new key first, falling back to the old hash on a miss, while a background job copies the old hash with HSCAN in batches. When the copy is complete, we UNLINK the old key.

For the report that needed everything, we ask whether it should read Redis at all. Often the full data also lives in a database, and the report belongs there.

5Monitoring So It Does Not Come Back

We make big keys visible before they hurt. SLOWLOG GET shows slow commands; LATENCY DOCTOR, after enabling latency monitoring with a threshold, describes latency events and their likely causes; MEMORY USAGE gives a key's size. redis-cli --bigkeys and --memkeys scan the keyspace with SCAN, so they can run against production with little impact, ideally on a replica. We alert when any key passes a size threshold, for example 10 MB or 100,000 elements, and when slow log entries appear.

6Follow-up questions

How do you find big keys without blocking the server?

Use tools built on SCAN, which walks the keyspace in small batches: redis-cli --bigkeys or --memkeys, ideally against a replica. For a precise picture, a script can SCAN keys and call MEMORY USAGE on each, or we can analyse an RDB snapshot offline with a parser, which puts no load on the live server at all.

Does UNLINK always free memory in the background?

Redis frees small values synchronously anyway because it is cheaper than handing them off; only keys above a small threshold of elements are passed to the background thread. So UNLINK is safe to use everywhere: it costs nothing extra on small keys and prevents stalls on large ones.

Would moving to Redis Cluster alone fix it?

No. A cluster spreads keys across nodes, but one key always lives on one node, so a 4-million-field hash is just as big there and blocks that node. Splitting the data into many keys is what lets the cluster help.

7Conclusion

In this answer, we saw that Redis executes commands on one main thread, so HGETALL on a 4-million-field hash and a synchronous DEL of it each block all clients for their full duration. We removed the immediate stalls with UNLINK and lazyfree settings and by reading with HSCAN, then redesigned the hash into 1,024 small bucketed keys migrated in the background, and set up SLOWLOG, LATENCY DOCTOR, MEMORY USAGE and big-key alerts.

What to say in the interviewAbout 90 seconds aloud

Open with

The cause is the single main thread. Redis executes commands one at a time, so an O(n) command on a huge key blocks every other client for as long as it runs.

Then, if they want more

HGETALL on 4 million fields has to walk the whole hash and build a reply of 8 million strings, which can take a couple of hundred milliseconds, and plain DEL of a big key frees millions of allocations synchronously, which stalls the server again. That matches the spikes every few minutes when the job runs.

Without downtime, I would first change usage: replace DEL with UNLINK, which frees memory in a background thread, or enable lazyfree-lazy-user-del, plus lazyfree for expiry and eviction, and replace HGETALL with HSCAN in pages of about a thousand, so other clients run between calls.

Then I would fix the data model: shard the hash into, say, 1,024 smaller hashes by hashing the field into a bucket, so each key stays small, single-field reads stay O(1), and in a cluster the buckets spread across slots.

I migrate by writing new keys, reading new with fallback to old, and copying in the background with HSCAN, then UNLINK the old key. Finally I monitor with SLOWLOG GET, LATENCY DOCTOR and MEMORY USAGE, run --bigkeys on a replica, and alert on keys above a size threshold.

About this answer

Last updated
12 November 2026
Versions it names
Redis 4, 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
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 "Redis latency spikes to 200 ms every few minutes. redis-cli --bigkeys shows a hash with...": https://scrivio-interview-library.netlify.app/databases/redis/5a29c56741/