Lua Academy · Lesson

SCRIPT LOAD and EVALSHA

Load scripts with SCRIPT LOAD and execute by SHA1 for efficiency.

Lesson 4 of 413 steps

SCRIPT LOAD and EVALSHA is a free Lua Academy lesson on CoddyKit. This is lesson 4 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Lua Academy learning path, and your progress syncs across the web and the CoddyKit app. The Lua Academy course includes 4 lessons in total.

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 EVAL

Application 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
end

SCRIPT 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.

Free to start

Learn Lua with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
40
Lessons
159

Frequently Asked Questions

Is the “SCRIPT LOAD and EVALSHA” lesson free?

Yes — any 3 lessons of the Lua Academy learning path, including “SCRIPT LOAD and EVALSHA”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Lua Academy course includes 4 lessons in total.

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, so you can start here or from the beginning and move at your own pace. This is lesson 4 of 4.

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

  1. EVAL and Redis Lua Environment
  2. Atomic Operations and Transactions
  3. Rate Limiting with Lua
  4. SCRIPT LOAD and EVALSHA
← Back to Lua Academy