Incident Response

Redis BUSY Redis is busy running a script: what to do

A Lua script or function has been running longer than busy-reply-threshold, and Redis now answers almost everything with BUSY. Here is when SCRIPT KILL works, when only SHUTDOWN NOSAVE is left, how Sentinel reacts, and how to stop it from happening again.

· 11 min read · AG Data Team

BUSY Redis is busy running a script. You can only call SCRIPT KILL or SHUTDOWN NOSAVE. This error means a Lua script (EVAL/EVALSHA) or a Redis Function (FCALL) has been running longer than busy-reply-threshold, 5 seconds by default. The script is still running. Redis has simply started rejecting other clients' commands. What you can do next depends on one question: has the script already written anything?

127.0.0.1:6379> GET session:42
(error) BUSY Redis is busy running a script. You can only call SCRIPT KILL or SHUTDOWN NOSAVE.

-- with Redis Functions (FCALL) the hint names FUNCTION KILL instead
(error) BUSY Redis is busy running a script. You can only call FUNCTION KILL or SHUTDOWN NOSAVE.

Why Redis stops answering

Redis runs a script atomically. The scripting documentation says that while a script executes, all server activities are blocked for its entire runtime. A script that runs for a millisecond is invisible. A script that loops over a million keys freezes the whole instance for every client.

The threshold does not stop the script. According to the comments in redis.conf, once the maximum execution time is reached Redis starts to reply to most commands with a BUSY error and allows only a handful of commands, such as SCRIPT KILL, FUNCTION KILL and SHUTDOWN NOSAVE. The setting used to be called lua-time-limit; since Redis 7.0 it is busy-reply-threshold, and the old name still works as an alias. Setting it to 0 or a negative value disables the mechanism, so clients simply wait with no way to interrupt the script. Do not do that in production.

127.0.0.1:6379> CONFIG GET busy-reply-threshold
1) "busy-reply-threshold"
2) "5000"

What to do right now

  1. Find out what is running. For functions, FUNCTION STATS is allowed during BUSY and the documentation says it returns the function name, the command it was called with and duration_ms.
    127.0.0.1:6379> FUNCTION STATS
    -- shortened; the running_script part contains:
    --   name         the function name, e.g. cleanup_expired
    --   command      e.g. FCALL cleanup_expired 0
    --   duration_ms  how long it has been running
    For EVAL scripts there is no such command, so check the application side: what was deployed, which job started, what calls EVAL.
  2. Try to kill it. Use SCRIPT KILL for EVAL scripts and FUNCTION KILL for functions. Each refuses the other kind. The SCRIPT KILL documentation explains that it only works if the script has not yet performed a write. The client that ran the script gets an error, and the rest of Redis returns to normal.
    127.0.0.1:6379> SCRIPT KILL
    OK
    
    -- the script already wrote something:
    127.0.0.1:6379> SCRIPT KILL
    (error) UNKILLABLE Sorry the script already executed write commands against the dataset. You can either wait the script termination or kill the server in a hard way using the SHUTDOWN NOSAVE command.
  3. If the answer is UNKILLABLE, choose between waiting and a hard stop. A script that already wrote cannot be stopped halfway without breaking atomicity. Your options are to wait until it finishes, or to run SHUTDOWN NOSAVE, which kills the process without saving so that half-written data is never persisted. After a restart Redis loads whatever RDB snapshot or AOF it has; with RDB only, everything written since the last snapshot is lost. On a primary with replicas, a failover to a replica is usually the better "hard stop".

Replicas are not stuck. Since Redis 7.0 scripts are replicated only by their effects: the documentation says replicas run the write commands without executing the script, and the commands are sent as one MULTI/EXEC block once the script finishes. A long script on the primary does not freeze replicas, which is why failing over to one is a real option.

What Sentinel does

If you run Sentinel, the Sentinel documentation describes the behaviour: when the master returns BUSY, Sentinel tries SCRIPT KILL before triggering a failover. That only succeeds if the script was read-only. If the instance is still in an error state after this try, it will eventually be failed over. So a write-heavy runaway script can cause a failover once it has been BUSY for longer than down-after-milliseconds. That is usually what you want, but check your Sentinel setup so the timeout and quorum match your expectations.

Valkey

Valkey has the same mechanism, the same busy-reply-threshold setting with the lua-time-limit alias and the same 5000 ms default. The message names the server it runs on: Valkey 9.0 replies BUSY Valkey is busy running a script unless extended-redis-compatibility is enabled, which valkey.conf describes as a temporary option added in 8.0 that makes Valkey pretend to be Redis. If your alerting greps for the exact text "Redis is busy", update it after a migration.

On large production instances

On a test instance with a few thousand keys, every script finishes in milliseconds. The same script on tens of millions of keys is what produces BUSY, so plan for scale:

Key takeaways

Redis Valkey Lua Errors
Work with us

Redis stuck in BUSY right now?

We get Redis and Valkey back online, decide safely between waiting, killing and failing over, and rewrite the scripts that caused it.

Get in touch More from the blog