Difficulty: Advanced
What are SCRIPT LOAD and EVALSHA for, how do clients handle a missing script, and how do Redis 7 Functions change the picture?
SCRIPT LOAD sends a Lua script to Redis, stores it in the server's script cache and returns its SHA1 digest. EVALSHA <sha1> numkeys key... arg... then runs the cached script by that digest, so the client sends 40 hex characters instead of the full script on every call. If the script is not in the cache, Redis replies with a NOSCRIPT error and the client must fall back to EVAL or load it again.
A Lua script runs atomically: while it executes, no other command runs. That lets you do read-modify-write logic (check a value, then update several keys) without WATCH/MULTI retries and in a single network round trip. Rate limiters, compare-and-delete lock release and inventory decrements are common examples.
EVAL also caches the script it runs, under the SHA1 of its body. SCRIPT LOAD just caches without running.SCRIPT FLUSH, and a newly promoted replica or a new cluster node may not have your script.SCRIPT EXISTS sha1 [sha1 ...] checks presence.NOSCRIPT. Libraries do this automatically: ioredis defineCommand, redis-py register_script and Jedis try EVALSHA first and fall back to EVAL on NOSCRIPT.KEYS, not built from strings inside the script. Redis Cluster uses KEYS to route the call, and all keys must hash to the same slot.busy-reply-threshold (formerly lua-time-limit, default 5 seconds) Redis starts replying BUSY to others, and you can stop a read-only script with SCRIPT KILL.EVAL_RO and EVALSHA_RO for read-only scripts that can run on replicas.| Aspect | EVAL / EVALSHA | Functions (7.0+) |
|---|---|---|
| Loaded with | SCRIPT LOAD or EVAL | FUNCTION LOAD |
| Called with | EVALSHA sha1 ... | FCALL name numkeys ... |
| Survives restart | No | Yes, saved in RDB and AOF |
| Replicated to replicas | No (cache is local) | Yes |
| Identified by | SHA1 of the body | Library and function name |
Functions treat server-side logic like stored procedures that the server owns, which removes the NOSCRIPT dance. Ephemeral scripts with EVALSHA are still common and fully supported, especially when the logic ships with the application code.
# Atomic stock decrement: fail if not enough stock
SCRIPT LOAD "local s = tonumber(redis.call('GET', KEYS[1]) or '0') if s < tonumber(ARGV[1]) then return -1 end return redis.call('DECRBY', KEYS[1], ARGV[1])"
# -> "b6c1..." (40 hex chars)
SET stock:sku123 10
EVALSHA b6c1... 1 stock:sku123 3
SCRIPT EXISTS b6c1...
Replace b6c1... with the full digest returned by SCRIPT LOAD. The check and the decrement cannot be interleaved with other clients.
import Redis from 'ioredis';
const redis = new Redis();
redis.defineCommand('takeStock', {
numberOfKeys: 1,
lua: `
local s = tonumber(redis.call('GET', KEYS[1]) or '0')
if s < tonumber(ARGV[1]) then return -1 end
return redis.call('DECRBY', KEYS[1], ARGV[1])`,
});
// ioredis sends EVALSHA and falls back to EVAL on NOSCRIPT
const left = await redis.takeStock('stock:sku123', 3);
console.log(left === -1 ? 'Out of stock' : `Left: ${left}`);
Defining the command once lets the library handle hashing, EVALSHA and the NOSCRIPT retry for you.
Lua scripting, SCRIPT LOAD, EVALSHA, NOSCRIPT, Redis Functions