Skip to content

Debugging the BUSY error from a long-running Redis Lua script

Diagnose and recover from BUSY Redis is busy running a script: SCRIPT KILL, SHUTDOWN NOSAVE, busy-script-timeout, and the patterns that prevent recurrence.

The error BUSY Redis is busy running a script is what every client sees when a Lua script has been running longer than lua-time-limit (default 5 seconds). The server refuses every other command — except a tiny allowlist — until the script either finishes or is killed. This is a self-defense mechanism: the script holds the main thread, and serving other clients while a runaway script reads or writes data would be unsafe. This article is the runbook for diagnosing what is stuck, killing it safely (or not), and preventing it from happening again.

How to verify

# What is running?
redis-cli INFO scripting
# look for: lua_script_busy and the running script SHA

redis-cli CLIENT LIST TYPE normal
# look for a client with cmd=eval or cmd=evalsha

# How long has it been busy?
redis-cli --no-auth-warning INFO server | grep uptime

# Configured timeout
redis-cli CONFIG GET busy-reply-threshold
redis-cli CONFIG GET lua-time-limit

What’s happening

When EVAL or EVALSHA runs, Redis hands the script the main thread and waits for it to finish. The script can call redis.call(...) arbitrarily; each call runs inside the same thread context, so nothing else can interleave. If the script runs longer than lua-time-limit (default 5000 ms), Redis sets a flag: every subsequent client request returns BUSY Redis is busy running a script. The only commands that work in this state are:

  • SCRIPT KILL — terminate the running script if it has not yet written any data.
  • SHUTDOWN NOSAVE — kill the daemon entirely. Loses unpersisted data and disrupts any in-flight reads, but is the only way out if the script has already written.

The decision tree: if the script is read-only, SCRIPT KILL is safe and recovery is immediate. If the script has already issued a write, SCRIPT KILL will not work — the kernel refuses to half-execute a write-touching script because it could leave the database in an inconsistent state. SHUTDOWN NOSAVE is the escape hatch.

After SHUTDOWN, a restart kicks in normal recovery: the AOF (if enabled) replays writes that were fsync’d before the kill. Anything between the last fsync and the SHUTDOWN is lost.

The remediation is structural: scripts should never run for seconds. The pattern that caused this is almost always a loop over a large data structure (KEYS * inside a script, or SCAN with an inadequate exit condition) — fix the script, not the timeout.

The procedure

  1. Confirm the BUSY state. From a separate client (the original redis-cli is unhelpful):

    redis-cli -h <host> -p <port> -a "$PASS" PING
    # (error) BUSY Redis is busy running a script. You can only call SCRIPT KILL or SHUTDOWN NOSAVE
  2. Identify the script. The SHA is in INFO scripting:

    redis-cli -h <host> -p <port> -a "$PASS" INFO scripting
    # used_memory_scripts:1024
    # number_of_cached_scripts:42
    # lua_script_busy:1
    # busy_script_sha:abc123def456...
  3. Decide kill or shutdown. Look at the application code that fired the script. If you cannot determine read-only-ness in 60 seconds:

    • If reads-only is plausible, try SCRIPT KILL first.
    • If writes are involved, prepare for SHUTDOWN NOSAVE.
  4. Try SCRIPT KILL:

    redis-cli -h <host> -p <port> -a "$PASS" SCRIPT KILL
    # OK            -> done, server back to normal
    # NOTBUSY       -> race condition, script already finished
    # UNKILLABLE    -> script has written, can't kill
  5. If UNKILLABLE, escalate to SHUTDOWN NOSAVE:

    redis-cli -h <host> -p <port> -a "$PASS" SHUTDOWN NOSAVE
    # Connection lost — the daemon has stopped

    Restart the daemon:

    sudo systemctl restart redis-server
  6. For Sentinel-managed Redis, the SHUTDOWN triggers a failover. Sentinels detect the dead master, elect a replica as new master, and the application sees a +switch-master notification. Recovery time is the same as a planned failover.

  7. Verify recovery:

    redis-cli PING
    redis-cli INFO replication
    redis-cli INFO scripting | grep lua_script_busy
    # Expected: lua_script_busy:0
  8. Post-mortem: find the script. From the SHA, search application code:

    grep -r "$BUSY_SCRIPT_SHA" /path/to/app/
    # or, if the SHA is unknown:
    grep -rn "redis.call\|EVAL\|EVALSHA\|fcall" /path/to/app/

    Inspect the script and find the loop, the KEYS call, or the unbounded recursion that caused the runaway. Refactor it to bound the work (e.g., process N keys per invocation, use SCAN cursor instead of KEYS).

  9. Tighten the timeout if scripts in this codebase should never approach 5 seconds:

    lua-time-limit 1000   # 1 second

    This makes BUSY happen sooner, surfacing bad scripts in dev faster.

Common pitfalls

  • SCRIPT KILL is silently ignored if the running script has already written. The error code distinguishes — read the response carefully.
  • SHUTDOWN NOSAVE discards any AOF buffer not yet fsync’d. With appendfsync everysec you lose up to 1 second of writes; with appendfsync always you lose nothing committed.
  • A loop using redis.call('SCAN', ...) inside a Lua script does not yield — Redis is single-threaded and the whole loop blocks. SCAN is meant to be called by the client repeatedly, not from inside a script.
  • A misconfigured client library that retries on BUSY with the same script SHA piles requests until the underlying script finishes — and when it does, the queued requests run all at once. Disable retries on BUSY in client code; let the script finish or kill it.
  • BUSY on a replica is unusual but possible during a long script replicated from the master. The replica’s busy state propagates: applications using REPLICAOF topologies need to handle BUSY on both endpoints.

In the engagements we run, every Lua script in version control has a CI check that times its execution against a fixed dataset and fails if it exceeds lua-time-limit / 2. We operate that under Managed Operations — and once the immediate BUSY is resolved, the deeper analysis flows into the script-cache hygiene that Lua scripting and Functions both cover.