SCRIPT LOAD and EVALSHA
Load scripts with SCRIPT LOAD and execute by SHA1 for efficiency.
SCRIPT LOAD and EVALSHA is a free Lua Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Lua Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
The Problem with EVAL
EVAL sends the full script text on every call. For frequently executed scripts this wastes bandwidth. SCRIPT LOAD + EVALSHA solve this by loading once and calling by SHA1 hash.
SCRIPT LOAD
SCRIPT LOAD script compiles the script, caches it in Redis, and returns its SHA1 hash. The script stays cached until the server restarts or SCRIPT FLUSH is called.
# Redis CLI:
SCRIPT LOAD "return redis.call('GET', KEYS[1])"
# Returns: "abc123...sha1hash..."EVALSHA
EVALSHA sha1 numkeys [keys] [args] executes the previously loaded script identified by its SHA1. Fails with NOSCRIPT if the script is not cached.
# Redis CLI:
EVALSHA abc123...sha1hash... 1 mykey
# Returns the same result as EVALApplication Pattern
On application startup: SCRIPT LOAD all scripts, store their SHA1s. Then use EVALSHA for all runtime calls.
local redis = require("redis")
local scriptText = [[
local v = redis.call("GET", KEYS[1])
return v
]]
local sha = redis:script_load(scriptText)
-- Later:
redis:evalsha(sha, 1, "mykey")Handling NOSCRIPT Error
If Redis is restarted, cached scripts are lost. Catch NOSCRIPT errors and fall back to EVAL to re-load.
local function safeEvalsha(r, sha, script, numkeys, ...)
local ok, result = pcall(r.evalsha, r, sha, numkeys, ...)
if not ok and result:find("NOSCRIPT") then
sha = r:script_load(script)
result = r:evalsha(sha, numkeys, ...)
end
return result
endSCRIPT EXISTS
SCRIPT EXISTS sha1 [sha1 ...] checks whether scripts are cached. Returns 1 for each cached script, 0 for missing ones. Use to pre-warm after restart.
SCRIPT FLUSH
SCRIPT FLUSH removes all cached scripts from Redis memory. Use during deployments when scripts change.
Script Versioning
When a script changes, its SHA1 changes. Store SHA1s with version numbers and update after deployment.
Bandwidth Savings
For a 500-byte script called 10,000 times per second, EVALSHA saves 5 MB/s of Redis traffic versus EVAL. Significant at scale.
Redis Cluster with EVALSHA
In cluster mode, EVALSHA works the same as EVAL regarding slot restrictions: all KEYS must be in the same slot.
Best Practice
Always use EVALSHA in production after loading scripts at startup. Implement the NOSCRIPT fallback for resilience after Redis restarts.
EVALSHA Question
What does EVALSHA use instead of the script text?
Recap: SCRIPT LOAD and EVALSHA
Load scripts once with SCRIPT LOAD to get a SHA1. Use EVALSHA for all subsequent calls to save bandwidth. Handle NOSCRIPT errors by re-loading. SCRIPT EXISTS checks cache status; SCRIPT FLUSH clears it.
Frequently asked questions
Is the “SCRIPT LOAD and EVALSHA” lesson free?
Yes — the full text of “SCRIPT LOAD and EVALSHA” is free to read here on the web, and the Lua Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Lua Academy course, upgrade to CoddyKit PRO.
What will I learn in “SCRIPT LOAD and EVALSHA”?
Load scripts with SCRIPT LOAD and execute by SHA1 for efficiency. You practise Lua Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Lua Academy?
No prior experience is required. Lua Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “SCRIPT LOAD and EVALSHA” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Lua Academy lesson?
Yes. Every Lua Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- EVAL and Redis Lua Environment
- Atomic Operations and Transactions
- Rate Limiting with Lua
- SCRIPT LOAD and EVALSHA