Bird
Raised Fist0
Redisquery~10 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What does the SCRIPT LOAD command do in Redis?
easy
A. It executes a Lua script immediately without caching.
B. It lists all cached Lua scripts in Redis.
C. It deletes a cached Lua script by its SHA1 hash.
D. It loads a Lua script into Redis and returns its SHA1 hash.

Solution

  1. Step 1: Understand SCRIPT LOAD purpose

    The SCRIPT LOAD command stores a Lua script in Redis and returns a unique SHA1 hash for it.
  2. Step 2: Differentiate from other commands

    Unlike EVAL, SCRIPT LOAD does not execute the script but prepares it for later use.
  3. Final Answer:

    It loads a Lua script into Redis and returns its SHA1 hash. -> Option D
  4. Quick Check:

    SCRIPT LOAD returns SHA1 hash [OK]
Hint: SCRIPT LOAD stores script and returns SHA1 hash [OK]
Common Mistakes:
  • Confusing SCRIPT LOAD with EVAL which runs scripts immediately
  • Thinking SCRIPT LOAD deletes scripts
  • Assuming SCRIPT LOAD lists scripts
2. Which of the following is the correct syntax to load a Lua script into Redis and get its SHA1 hash?
easy
A. EVALSHA "return redis.call('PING')" 0
B. SCRIPT LOAD "return redis.call('PING')"
C. SCRIPT EXECUTE "return redis.call('PING')"
D. LOAD SCRIPT "return redis.call('PING')"

Solution

  1. Step 1: Identify correct command syntax

    The correct command to load a script is SCRIPT LOAD followed by the Lua script as a string.
  2. Step 2: Eliminate incorrect options

    EVALSHA runs a cached script by SHA, SCRIPT EXECUTE and LOAD SCRIPT are invalid Redis commands.
  3. Final Answer:

    SCRIPT LOAD "return redis.call('PING')" -> Option B
  4. Quick Check:

    SCRIPT LOAD syntax is SCRIPT LOAD "script" [OK]
Hint: SCRIPT LOAD "script" loads script and returns SHA1 [OK]
Common Mistakes:
  • Using EVALSHA instead of SCRIPT LOAD to load script
  • Using non-existent commands like SCRIPT EXECUTE
  • Incorrect command order or missing quotes
3. Given the following commands executed in Redis:
SCRIPT LOAD "return ARGV[1]"
EVALSHA <sha1> 0 Hello
What will be the output of the EVALSHA command?
medium
A. 0
B. An error because the script is not cached
C. "Hello"
D. The SHA1 hash string

Solution

  1. Step 1: Understand SCRIPT LOAD and EVALSHA interaction

    SCRIPT LOAD stores the script and returns its SHA1 hash. EVALSHA runs the cached script using that SHA1.
  2. Step 2: Analyze the script and arguments

    The script returns ARGV[1], and EVALSHA is called with argument "Hello" as ARGV[1]. So output is "Hello".
  3. Final Answer:

    "Hello" -> Option C
  4. Quick Check:

    EVALSHA runs cached script returning ARGV[1] = "Hello" [OK]
Hint: EVALSHA runs cached script with ARGV arguments [OK]
Common Mistakes:
  • Confusing SCRIPT LOAD output with EVALSHA output
  • Forgetting to pass correct number of keys (0 here)
  • Expecting SHA1 hash as output of EVALSHA
4. You run the following Redis commands:
SCRIPT LOAD "return redis.call('GET', KEYS[1])"
EVALSHA <sha1> 1 mykey
But you get an error: NOSCRIPT No matching script. Please use EVAL. What is the most likely cause?
medium
A. The SHA1 hash used in EVALSHA is incorrect or from a different script.
B. The KEYS argument count is wrong; it should be 0 instead of 1.
C. SCRIPT LOAD does not cache scripts, so EVALSHA fails.
D. Redis does not support EVALSHA for GET commands.

Solution

  1. Step 1: Understand NOSCRIPT error meaning

    This error means Redis does not have the script cached under the given SHA1 hash.
  2. Step 2: Identify common causes

    Most often, the SHA1 hash passed to EVALSHA is wrong or from a different script than loaded.
  3. Final Answer:

    The SHA1 hash used in EVALSHA is incorrect or from a different script. -> Option A
  4. Quick Check:

    NOSCRIPT error means SHA1 mismatch [OK]
Hint: NOSCRIPT means SHA1 hash mismatch or missing script [OK]
Common Mistakes:
  • Assuming KEYS count is always zero
  • Thinking SCRIPT LOAD does not cache scripts
  • Believing Redis disallows EVALSHA for certain commands
5. You want to optimize Redis script execution by caching a Lua script that increments a key's value by a given amount. Which sequence of commands correctly loads and runs this script multiple times efficiently?
hard
A. Use SCRIPT LOAD to get SHA1, then call EVALSHA with the SHA1 and arguments for each increment.
B. Use EVAL with the full script every time you want to increment the key.
C. Use SCRIPT FLUSH before every increment to clear cached scripts, then EVALSHA.
D. Use SCRIPT LOAD once, then call EVAL with the full script every time.

Solution

  1. Step 1: Understand caching for efficiency

    Loading the script once with SCRIPT LOAD caches it and returns SHA1 for reuse.
  2. Step 2: Use EVALSHA for repeated execution

    Calling EVALSHA with the SHA1 runs the cached script efficiently without sending full script each time.
  3. Step 3: Eliminate inefficient options

    Using EVAL every time sends full script, SCRIPT FLUSH clears cache unnecessarily.
  4. Final Answer:

    Use SCRIPT LOAD to get SHA1, then call EVALSHA with the SHA1 and arguments for each increment. -> Option A
  5. Quick Check:

    Cache script once, run with EVALSHA repeatedly [OK]
Hint: Load once with SCRIPT LOAD, run many times with EVALSHA [OK]
Common Mistakes:
  • Using EVAL repeatedly instead of EVALSHA
  • Flushing scripts unnecessarily with SCRIPT FLUSH
  • Reloading script before every execution
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard