Script loading and caching in Redis - Time & Space Complexity
When using Redis scripts, it is important to understand how loading and caching affect performance.
We want to know how the time to run scripts changes as we add more scripts or run them multiple times.
Analyze the time complexity of loading and running a Redis Lua script with caching.
-- Load script and get SHA1 hash
local sha = redis.sha1hex(script)
-- Check if script is cached
if redis.call('SCRIPT', 'EXISTS', sha)[1] == 0 then
redis.call('SCRIPT', 'LOAD', script)
end
-- Run the script by SHA
redis.call('EVALSHA', sha, keys_count, unpack(keys_and_args))
This code loads a Lua script into Redis if not already cached, then runs it by its hash.
Look for repeated checks or calls that affect performance.
- Primary operation: Checking if the script SHA exists and loading it if missing.
- How many times: Once per unique script; running the script by SHA can happen many times without reloading.
As the number of unique scripts grows, the initial load cost grows linearly.
| Number of Unique Scripts (n) | Approx. Load Checks and Loads |
|---|---|
| 10 | 10 checks, up to 10 loads |
| 100 | 100 checks, up to 100 loads |
| 1000 | 1000 checks, up to 1000 loads |
Once loaded, running scripts by SHA is fast and does not grow with n.
Time Complexity: O(n)
This means the time to load scripts grows linearly with the number of unique scripts, but running cached scripts is fast.
[X] Wrong: "Running a cached script takes as long as loading it every time."
[OK] Correct: Once a script is cached, running it by SHA is quick and does not repeat the loading cost.
Understanding how caching scripts affects performance shows you can reason about efficiency in real systems.
What if we skipped the existence check and always loaded the script before running? How would the time complexity change?
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
