What if you could run your Redis scripts instantly without resending them every time?
Why Script loading and caching in Redis? - Purpose & Use Cases
Imagine you have a complex script that you need to run many times on your Redis database. Each time, you copy and paste the entire script to execute it.
This feels like writing the same long letter over and over by hand.
Manually sending the full script every time wastes time and network resources.
It also increases the chance of mistakes, like typos or sending incomplete scripts.
Plus, it slows down your application because Redis has to parse the script every time.
Script loading and caching lets you send the script once to Redis, which stores it with a unique ID.
Later, you just call that ID to run the script instantly without resending the whole code.
This saves time, reduces errors, and speeds up your database operations.
EVAL "return redis.call('GET', KEYS[1])" 1 mykey
SCRIPT LOAD "return redis.call('GET', KEYS[1])" EVALSHA <script_sha1> 1 mykey
You can run complex scripts repeatedly with lightning speed and less network traffic.
A web app caches user session data in Redis and needs to update it often. Using script caching, it quickly runs update scripts without delay, improving user experience.
Manually sending scripts every time is slow and error-prone.
Script loading caches scripts in Redis for fast reuse.
This makes repeated script execution efficient and reliable.
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
