0
0
Redisquery~10 mins

EVALSHA for cached scripts in Redis - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - EVALSHA for cached scripts
Write Lua script
Load script with SCRIPT LOAD
Get SHA1 hash of script
Run script with EVALSHA using SHA1
If SHA1 not found -> Error
Optionally reload script and retry
You write a Lua script, load it into Redis to get a SHA1 hash, then run it using EVALSHA with that hash. If Redis doesn't have the script cached, you get an error and can reload it.
Execution Sample
Redis
SCRIPT LOAD "return redis.call('GET', KEYS[1])"
EVALSHA <sha1> 1 mykey
Load a Lua script that gets a key's value, then run it using its SHA1 hash.
Execution Table
StepCommandInputActionOutput/Result
1SCRIPT LOAD"return redis.call('GET', KEYS[1])"Load script into Redis cacheReturns SHA1 hash: abc123...
2EVALSHAabc123... 1 mykeyRun cached script with SHA1Returns value of 'mykey' if exists
3EVALSHAwrongsha 1 mykeyTry running with unknown SHA1Error: NOSCRIPT No matching script. Please use EVAL.
4SCRIPT LOAD"return redis.call('GET', KEYS[1])"Reload script after errorReturns SHA1 hash: abc123...
5EVALSHAabc123... 1 mykeyRun script again after reloadReturns value of 'mykey'
💡 Execution stops when script runs successfully or error occurs due to missing SHA1.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5
SHA1nullabc123...abc123...wrongshaabc123...abc123...
Script Cached?falsetruetruefalsetruetrue
Script Resultnullnullvalue of mykey or nilError NOSCRIPTnullvalue of mykey or nil
Key Moments - 2 Insights
Why do we get an error when using EVALSHA with a SHA1 that Redis doesn't know?
Because Redis only runs scripts it has cached. If the SHA1 is unknown, Redis returns a NOSCRIPT error as shown in step 3 of the execution_table.
What does SCRIPT LOAD do and why is it important before EVALSHA?
SCRIPT LOAD stores the Lua script in Redis and returns its SHA1 hash. This hash is needed to run the script with EVALSHA, as shown in step 1 and 2.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the output of SCRIPT LOAD in step 1?
AReturns the SHA1 hash of the script
BReturns the value of 'mykey'
CReturns an error NOSCRIPT
DReturns null
💡 Hint
Check the 'Output/Result' column in step 1 of the execution_table
At which step does Redis return an error because the SHA1 is unknown?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Look for the 'Error: NOSCRIPT' message in the execution_table
If you skip SCRIPT LOAD and run EVALSHA with a random SHA1, what happens?
AScript runs successfully
BRedis returns NOSCRIPT error
CRedis loads the script automatically
DRedis returns null
💡 Hint
Refer to step 3 in the execution_table where unknown SHA1 causes error
Concept Snapshot
EVALSHA runs a cached Lua script by its SHA1 hash.
Use SCRIPT LOAD to cache script and get SHA1.
If SHA1 unknown, Redis returns NOSCRIPT error.
Reload script with SCRIPT LOAD before retrying EVALSHA.
This improves performance by avoiding sending full script each time.
Full Transcript
This visual execution shows how Redis runs Lua scripts using EVALSHA. First, you write a Lua script and load it into Redis with SCRIPT LOAD. This returns a SHA1 hash representing the script. You then run the script by calling EVALSHA with that SHA1 and the keys/arguments. If Redis does not have the script cached (unknown SHA1), it returns a NOSCRIPT error. You must reload the script with SCRIPT LOAD to cache it again before retrying EVALSHA. The execution table traces these steps, showing the SHA1 variable, caching state, and results. Key moments clarify why the NOSCRIPT error happens and the role of SCRIPT LOAD. The quiz tests understanding of these steps and error conditions. This method improves efficiency by sending only the SHA1 hash after caching the script once.