0
0
Redisquery~10 mins

Script loading and caching in Redis - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Script loading and caching
Write Lua script
Load script with SCRIPT LOAD
Receive SHA1 hash
Run script with EVALSHA using SHA1
If script missing, run EVAL to load and execute
Cache script in Redis for reuse
This flow shows how a Lua script is loaded into Redis, cached by its SHA1 hash, and executed efficiently using EVALSHA.
Execution Sample
Redis
SCRIPT LOAD "return redis.call('set', KEYS[1], ARGV[1])"
EVALSHA <sha1> 1 mykey myvalue
EVALSHA <sha1> 1 mykey newvalue
Load a Lua script that sets a key, then run it twice using its SHA1 hash to reuse the cached script.
Execution Table
StepCommandActionResultNotes
1SCRIPT LOAD "return redis.call('set', KEYS[1], ARGV[1])"Load script into RedisReturns SHA1 hash: abc123...Script stored and cached by SHA1
2EVALSHA abc123... 1 mykey myvalueRun cached script by SHA1OKKey 'mykey' set to 'myvalue'
3EVALSHA abc123... 1 mykey newvalueRun cached script againOKKey 'mykey' updated to 'newvalue'
4EVALSHA missing_sha 1 mykey valScript missing errorNOSCRIPT No matching script. Please use EVAL.Script not cached, must load first
5EVAL "return redis.call('set', KEYS[1], ARGV[1])" 1 mykey valRun script directlyOKScript runs and caches automatically
💡 Execution stops when script runs successfully or NOSCRIPT error occurs requiring reload.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5
Script SHA1noneabc123...abc123...abc123...abc123...abc123...
Key 'mykey' valuenonenonemyvaluenewvaluenewvalueval
Key Moments - 3 Insights
Why do we use SCRIPT LOAD before EVALSHA?
SCRIPT LOAD stores the script in Redis and returns its SHA1 hash. EVALSHA uses this hash to run the cached script without sending the full script text again, improving performance. See execution_table rows 1 and 2.
What happens if we run EVALSHA with a SHA1 that Redis does not know?
Redis returns a NOSCRIPT error because it does not have the script cached. We must then run EVAL with the full script to load it. See execution_table row 4.
Does running EVAL cache the script automatically?
Yes, running EVAL with the full script loads and caches it in Redis, so subsequent EVALSHA calls with the returned SHA1 will work. See execution_table row 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the SHA1 hash after SCRIPT LOAD?
Aabc123...
Bmissing_sha
Cnone
DNOSCRIPT
💡 Hint
Check Step 1 in execution_table for the SHA1 returned by SCRIPT LOAD.
At which step does Redis return a NOSCRIPT error?
AStep 2
BStep 5
CStep 4
DStep 1
💡 Hint
Look for the step where the command EVALSHA uses a missing SHA1.
If we skip SCRIPT LOAD and run EVAL directly, what happens?
AScript is not cached
BScript runs and caches automatically
CNOSCRIPT error
DRedis crashes
💡 Hint
See execution_table row 5 for what happens when EVAL runs the script.
Concept Snapshot
SCRIPT LOAD stores a Lua script in Redis and returns its SHA1 hash.
EVALSHA runs the cached script by SHA1 without sending full script text.
If script is missing, EVALSHA returns NOSCRIPT error.
EVAL runs the script directly and caches it automatically.
Use SCRIPT LOAD + EVALSHA for efficient repeated script execution.
Full Transcript
This visual execution shows how Redis handles Lua script loading and caching. First, SCRIPT LOAD stores the script and returns a SHA1 hash. Then, EVALSHA runs the cached script using this hash, avoiding sending the full script text again. If the script is missing, EVALSHA returns a NOSCRIPT error, requiring the script to be loaded again. Running EVAL with the full script runs and caches it automatically. Variables tracked include the script SHA1 and the key's value changes. Key moments clarify why SCRIPT LOAD is needed, what NOSCRIPT means, and how EVAL caches scripts. The quiz tests understanding of SHA1 values, error steps, and caching behavior. This method improves performance by reusing cached scripts.