Script loading and caching helps Redis run Lua scripts faster by saving them once and reusing them without sending the full script every time.
Script loading and caching in Redis
SCRIPT LOAD <script> EVALSHA <sha1> <numkeys> [key ...] [arg ...] SCRIPT EXISTS <sha1> SCRIPT FLUSH
SCRIPT LOAD loads a Lua script and returns its SHA1 hash.
EVALSHA runs a cached script by its SHA1 hash, faster than EVAL.
SCRIPT LOAD "return redis.call('set', KEYS[1], ARGV[1])"<sha1> with one key and one argument.EVALSHA <sha1> 1 mykey myvalueSCRIPT EXISTS <sha1>
SCRIPT FLUSH
This example loads a Lua script that sets a key to a value, runs it using its SHA1 hash, and then retrieves the value to confirm it was set.
127.0.0.1:6379> SCRIPT LOAD "return redis.call('set', KEYS[1], ARGV[1])" "e0e1f3c3a7f9b8d9c6a1b2c3d4e5f67890123456" 127.0.0.1:6379> EVALSHA e0e1f3c3a7f9b8d9c6a1b2c3d4e5f67890123456 1 mykey myvalue OK 127.0.0.1:6379> GET mykey "myvalue"
Always use EVALSHA after loading a script to save bandwidth and improve speed.
If you try to run EVALSHA with a SHA1 that Redis does not know, it will return an error.
You can use SCRIPT EXISTS to check if a script is cached before running it.
Script loading and caching lets Redis store Lua scripts by their SHA1 hash.
Use SCRIPT LOAD to load scripts and get their SHA1 hash.
Use EVALSHA to run cached scripts quickly without sending the full script again.
Practice
SCRIPT LOAD command do in Redis?Solution
Step 1: Understand SCRIPT LOAD purpose
The SCRIPT LOAD command stores a Lua script in Redis and returns a unique SHA1 hash for it.Step 2: Differentiate from other commands
Unlike EVAL, SCRIPT LOAD does not execute the script but prepares it for later use.Final Answer:
It loads a Lua script into Redis and returns its SHA1 hash. -> Option DQuick Check:
SCRIPT LOAD returns SHA1 hash [OK]
- Confusing SCRIPT LOAD with EVAL which runs scripts immediately
- Thinking SCRIPT LOAD deletes scripts
- Assuming SCRIPT LOAD lists scripts
Solution
Step 1: Identify correct command syntax
The correct command to load a script is SCRIPT LOAD followed by the Lua script as a string.Step 2: Eliminate incorrect options
EVALSHA runs a cached script by SHA, SCRIPT EXECUTE and LOAD SCRIPT are invalid Redis commands.Final Answer:
SCRIPT LOAD "return redis.call('PING')" -> Option BQuick Check:
SCRIPT LOAD syntax is SCRIPT LOAD "script" [OK]
- Using EVALSHA instead of SCRIPT LOAD to load script
- Using non-existent commands like SCRIPT EXECUTE
- Incorrect command order or missing quotes
SCRIPT LOAD "return ARGV[1]" EVALSHA <sha1> 0 HelloWhat will be the output of the
EVALSHA command?Solution
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.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".Final Answer:
"Hello" -> Option CQuick Check:
EVALSHA runs cached script returning ARGV[1] = "Hello" [OK]
- Confusing SCRIPT LOAD output with EVALSHA output
- Forgetting to pass correct number of keys (0 here)
- Expecting SHA1 hash as output of EVALSHA
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?Solution
Step 1: Understand NOSCRIPT error meaning
This error means Redis does not have the script cached under the given SHA1 hash.Step 2: Identify common causes
Most often, the SHA1 hash passed to EVALSHA is wrong or from a different script than loaded.Final Answer:
The SHA1 hash used in EVALSHA is incorrect or from a different script. -> Option AQuick Check:
NOSCRIPT error means SHA1 mismatch [OK]
- Assuming KEYS count is always zero
- Thinking SCRIPT LOAD does not cache scripts
- Believing Redis disallows EVALSHA for certain commands
Solution
Step 1: Understand caching for efficiency
Loading the script once with SCRIPT LOAD caches it and returns SHA1 for reuse.Step 2: Use EVALSHA for repeated execution
Calling EVALSHA with the SHA1 runs the cached script efficiently without sending full script each time.Step 3: Eliminate inefficient options
Using EVAL every time sends full script, SCRIPT FLUSH clears cache unnecessarily.Final Answer:
Use SCRIPT LOAD to get SHA1, then call EVALSHA with the SHA1 and arguments for each increment. -> Option AQuick Check:
Cache script once, run with EVALSHA repeatedly [OK]
- Using EVAL repeatedly instead of EVALSHA
- Flushing scripts unnecessarily with SCRIPT FLUSH
- Reloading script before every execution
Ready to prove it?
Pass the test and this pattern moves unknown
