Quick revision
The short answer
Redis runs a Lua script atomically on its single main thread, so while the script loops over thousands of keys, no other client's command can run. After busy-reply-threshold (lua-time-limit in older versions, 5 seconds by default) Redis starts answering everyone else with BUSY; SCRIPT KILL can stop the script only if it has not written anything, otherwise SHUTDOWN NOSAVE is the only way out. The fix is to keep scripts short and bounded, do the SCAN iteration in the client in small batches, and pass every key in KEYS so the script also works in Redis Cluster.
What to remember
In this answer, we saw that a script is one command on Redis's single main thread, so a script that loops over the keyspace with SCAN freezes every client. After busy-reply-threshold Redis replies BUSY, SCRIPT KILL works only before the first write, and otherwise only SHUTDOWN NOSAVE remains. The fix is to iterate in the client with SCAN in small batches, keep scripts bounded, pass all keys in KEYS within one slot for Cluster, and remember that Redis 7 functions follow the same rules.
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.
Redis runs a Lua script atomically on its single main thread, so while the script loops over thousands of keys, no other client's command can run. After busy-reply-threshold (lua-time-limit in older versions, 5 seconds by default) Redis starts answering everyone else with BUSY; SCRIPT KILL can stop the script only if it has not written anything, otherwise SHUTDOWN NOSAVE is the only way out. The fix is to keep scripts short and bounded, do the SCAN iteration in the client in small batches, and pass every key in KEYS so the script also works in Redis Cluster.
Why this question is asked
An eight second freeze of Redis is a production incident for everything that depends on it: sessions time out, rate limiters fail, caches miss and fall through to the database. Interviewers ask this because the cause is not a bug in Redis but a misunderstanding of what atomic means, and the same misunderstanding breaks scripts in Redis Cluster. In this answer, we will recap how Redis executes commands, see why a long script stops everyone, look at what the BUSY state allows and forbids, rewrite the job correctly, and then cover the Cluster rules and Redis 7 functions.
1A Quick Recap: One Thread Runs All Commands
Redis executes commands on one main thread, one after another. That is why each command is atomic without locks, and why Redis is fast: most commands take microseconds. Newer versions use extra threads for network I/O, but the commands themselves still run one at a time.
A Lua script, sent with EVAL, or a function called with FCALL, is treated as one command. Redis guarantees that nothing else runs while it executes, which is the property we use for things like rate limiters. The flip side is direct: a script that runs for 8 seconds is an 8 second command, and every other client waits.
2Reproducing the Problem
The script in the incident looks roughly like this. It was meant to expire old session keys, and it uses SCAN inside the script to find them.
- Script startsMain thread busy
- Other commands queueClients wait on their sockets
- busy-reply-threshold passes5 s by default
- Redis replies BUSY to othersStill not running their commands
- Has the script written?YesOnly SHUTDOWN NOSAVE helpsNoSCRIPT KILL stops it
- Script ends or is killedQueued commands run
6 steps in order. Play them, or step through them one at a time.
Atomicity means everyone else waits.
Notice the repeat loop: it does not stop until SCAN has visited the whole keyspace. SCAN is cheap per call when a client calls it, because the client's calls are spread out and other commands run between them. Inside a script, all those calls happen inside one command, so the whole walk becomes one long blocking operation.
3What BUSY Allows and Forbids
Redis does not interrupt a script on its own, because stopping it halfway would break the all-or-nothing promise. Instead, once the script has run longer than busy-reply-threshold, Redis starts processing incoming connections just enough to reply BUSY Redis is busy running a script, and it accepts a few special commands.
SCRIPT KILL stops the script, but only if it has not performed any write yet. Our script deletes keys as it goes, so it has written. Killing it would leave the dataset with some deletes applied and others not, which would violate atomicity, so Redis refuses. The only remaining option is SHUTDOWN NOSAVE, which stops the server without saving, losing everything since the last persistence point, and relying on a replica or a restart. That is why a write-heavy long script is so dangerous: there is no clean way out.
Clients in the meantime see BUSY errors or their own command timeouts. Lettuce reports the BUSY reply as an error (RedisBusyException in the versions we have used; check yours), while requests that simply waited may time out with RedisCommandTimeoutException, so both appear in the logs.
4Fixing It: Iterate in the Client, Keep Scripts Small
The cleanup does not need to be atomic across the whole keyspace. Each key's check and delete can stand alone. So we move the loop into the client and let Redis interleave other commands between small batches.
Each call now blocks Redis for a millisecond or two, and other clients' commands run between them. UNLINK instead of DEL frees large values in a background thread. Better still, sessions should have a TTL when they are written, so Redis expires them itself and no cleanup job is needed at all.
The general rules are that a script does a bounded amount of work, ideally a handful of keys known in advance, and that any iteration over keys found at runtime happens in the client.
5Scripts in Redis Cluster
The incident script would also fail in Redis Cluster. A script runs on one node, and Cluster requires that every key it touches is declared in KEYS and that they all hash to the same slot. Our script declared no keys and discovered them with SCAN, so the client cannot route it, and SCAN on one node only sees that node's keys. Redis may reject access to undeclared keys, or the script silently processes only part of the data.
So in Cluster, a script receives its keys from the caller, the keys share a hash tag, and anything that spans many keys is done by the client node by node.
6Redis 7 Functions
Redis 7 added functions: libraries of server-side code loaded once with FUNCTION LOAD and called by name with FCALL. Unlike EVAL scripts, they are persisted and replicated with the dataset, have names and versions, and can be flagged as read-only with no-writes so they may run on replicas. They are the managed way to ship server-side logic. But they run exactly like scripts, atomically on the main thread, so the same limits apply: a long function blocks everyone, and busy-reply-threshold, FUNCTION KILL and the no-writes rule behave the same way.
7Follow-up questions
When is MULTI/EXEC enough instead of a script?
MULTI/EXEC queues several commands and runs them together without interleaving, so it is enough when the commands do not depend on each other's results, such as setting a value and its expiry or updating two counters. It is not enough when a later step depends on an earlier result, such as expire only if the count is 1. For that we either use WATCH with optimistic retries or a script. A transaction has the same blocking concern if it queues a huge number of commands.
How would you detect long scripts before they cause an outage?
SLOWLOG records commands, including EVAL and FCALL, that exceed slowlog-log-slower-than, so a script that takes 200 ms shows up long before one takes 8 seconds. LATENCY monitoring and client-side p99 latency alerts complement it, and code review should reject scripts that loop over SCAN or KEYS.
Is raising busy-reply-threshold a fix?
No. It only changes when other clients start receiving BUSY; they are blocked for the whole duration either way. The threshold is a safety valve for operators, not a performance setting.
8Conclusion
In this answer, we saw that a script is one command on Redis's single main thread, so a script that loops over the keyspace with SCAN freezes every client. After busy-reply-threshold Redis replies BUSY, SCRIPT KILL works only before the first write, and otherwise only SHUTDOWN NOSAVE remains. The fix is to iterate in the client with SCAN in small batches, keep scripts bounded, pass all keys in KEYS within one slot for Cluster, and remember that Redis 7 functions follow the same rules.
Open with
Redis executes commands on a single main thread, and a Lua script counts as one command, so while it runs nothing else does. This script loops SCAN over the whole keyspace and deletes keys inside the script, which turns a normally cheap, interleaved scan into one 8 second blocking operation.
Then, if they want more
After busy-reply-threshold, called lua-time-limit in older versions and 5 seconds by default, Redis replies BUSY to other clients but still does not run their commands. SCRIPT KILL only works if the script has not written; this one deletes keys, so the only option would be SHUTDOWN NOSAVE, losing unsaved data.
To fix it, I would move the iteration to the client: use SCAN with a match pattern and a count of a few hundred, and process each batch with a pipeline of UNLINKs, so each round trip blocks Redis for a millisecond or two. Even better, give sessions a TTL so no cleanup job is needed.
Scripts should be short and touch a bounded set of keys passed in KEYS. That also matters for Redis Cluster: a script runs on one node, so all its keys must be declared and in one slot, and a script that discovers keys at runtime breaks routing.
Redis 7 functions with FUNCTION LOAD and FCALL are a nicer way to manage server-side code, but they have the same atomicity limits.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Redis 7
- 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 stops responding to all clients for 8 seconds and clients log 'BUSY Redis is busy...": https://scrivio-interview-library.netlify.app/databases/redis/3d4e02cf3c/