📖 Scenario: You are managing a Redis database for a web application. To improve performance, you want to load a Lua script into Redis and cache it for repeated use without sending the full script every time.
🎯 Goal: Build a Redis setup where you load a Lua script, cache its SHA1 hash, and then execute the script using the cached hash.
📋 What You'll Learn
Create a Lua script that increments a counter key by 1
Load the Lua script into Redis and store its SHA1 hash in a variable
Use the cached SHA1 hash to execute the script
Handle the case where the script is not cached and needs to be loaded again
💡 Why This Matters
🌍 Real World
Caching Lua scripts in Redis improves performance by avoiding sending full scripts repeatedly. This is useful in web apps, caching layers, and real-time systems.
💼 Career
Understanding script loading and caching in Redis is important for backend developers, DevOps engineers, and database administrators working with Redis to optimize performance.
Progress0 / 4 steps
1
Create the Lua script to increment a counter
Create a variable called lua_script that contains the Lua code to increment a Redis key by 1. The script should use redis.call('INCR', KEYS[1]).
Redis
Hint
Use a string variable to hold the Lua script exactly as: return redis.call('INCR', KEYS[1])
2
Load the Lua script into Redis and cache its SHA1 hash
Use the Redis command SCRIPT LOAD to load the lua_script into Redis. Store the returned SHA1 hash in a variable called script_sha.
Redis
Hint
Use redis.call('SCRIPT', 'LOAD', lua_script) to load the script and assign it to script_sha.
3
Execute the cached Lua script using its SHA1 hash
Use redis.call('EVALSHA', script_sha, 1, 'counter') to execute the cached Lua script on the key 'counter'.
Redis
Hint
Use redis.call('EVALSHA', script_sha, 1, 'counter') to run the script on the key named 'counter'.
4
Handle missing cached script by loading and executing
Write code that tries to execute the script using EVALSHA. If it fails because the script is missing, load the script again with SCRIPT LOAD and then execute it with EVALSHA. Use variables lua_script and script_sha.
Redis
Hint
Use pcall to catch errors when running EVALSHA. If it fails, reload the script and run EVALSHA again.
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
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 D
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
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 B
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:
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
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 A
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
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 A
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 unknownstrongwhile earning +1 Grip toward your certificate.