Bird
Raised Fist0
Redisquery~20 mins

EVALSHA for cached scripts in Redis - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Redis EVALSHA Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate
2:00remaining
What is the output of this Redis EVALSHA command?
Given the Lua script SHA1 hash is abcd1234 and the script returns the sum of two keys' integer values, what will be the output of this command if key1=5 and key2=3?
Redis
EVALSHA abcd1234 2 key1 key2
ATypeError
B8
CError: NOSCRIPT No matching script. Please use EVAL.
D5 3
Attempts:
2 left
💡 Hint
EVALSHA runs a cached Lua script by its SHA1 hash and returns the script's result.
🧠 Conceptual
intermediate
2:00remaining
Why use EVALSHA instead of EVAL in Redis?
Choose the best reason why Redis clients prefer using EVALSHA over EVAL after the script is loaded.
AEVALSHA is faster because it uses the cached script by its SHA1 hash, avoiding re-sending the script code.
BEVALSHA can run scripts without any keys or arguments.
CEVALSHA automatically updates the script if it changes on the server.
DEVALSHA returns the script source code instead of executing it.
Attempts:
2 left
💡 Hint
Think about network efficiency and script caching.
📝 Syntax
advanced
2:00remaining
Which EVALSHA command syntax is correct to run a script with 3 keys and 2 arguments?
Select the correct Redis EVALSHA command syntax to run a cached script with SHA1 hash f1e2d3c4, passing 3 keys and 2 arguments.
AEVALSHA f1e2d3c4 5 key1 key2 key3 arg1 arg2
BEVALSHA f1e2d3c4 key1 key2 key3 3 arg1 arg2
CEVALSHA f1e2d3c4 3 key1 key2 key3 arg1 arg2
DEVALSHA f1e2d3c4 2 key1 key2 key3 arg1 arg2
Attempts:
2 left
💡 Hint
Remember the syntax: EVALSHA ... ...
🔧 Debug
advanced
2:00remaining
What error occurs if you run EVALSHA with an unknown SHA1 hash?
You run EVALSHA deadbeef 1 key1 arg1 but the script is not cached on the server. What error message will Redis return?
ASyntaxError: invalid command
BOK
CTypeError: missing arguments
DError: NOSCRIPT No matching script. Please use EVAL.
Attempts:
2 left
💡 Hint
Think about what happens when the script is not found in cache.
❓ optimization
expert
3:00remaining
How to optimize repeated Lua script execution in Redis using EVALSHA?
You have a Lua script that you run many times with different keys and arguments. Which approach best optimizes performance?
ALoad the script once with EVAL, then run it repeatedly with EVALSHA using the cached SHA1 hash.
BRun the script every time with EVAL to ensure the latest version is used.
CUse EVALSHA without loading the script first; Redis will auto-load it.
DStore the script in a Redis string and call it by GET command.
Attempts:
2 left
💡 Hint
Consider network traffic and script caching benefits.

Practice

(1/5)
1. What does the Redis command EVALSHA primarily do?
easy
A. Uploads a new Lua script to Redis cache
B. Deletes a script from the cache by its SHA1 hash
C. Lists all cached scripts by their SHA1 hashes
D. Runs a cached Lua script using its SHA1 hash

Solution

  1. Step 1: Understand the purpose of EVALSHA

    EVALSHA runs a Lua script already stored in Redis cache using its SHA1 hash instead of sending the full script text.
  2. Step 2: Compare with other script commands

    Other commands like SCRIPT LOAD upload scripts, SCRIPT FLUSH deletes scripts, but EVALSHA executes cached scripts.
  3. Final Answer:

    Runs a cached Lua script using its SHA1 hash -> Option D
  4. Quick Check:

    EVALSHA = run cached script [OK]
Hint: EVALSHA runs scripts by hash, not by full text [OK]
Common Mistakes:
  • Confusing EVALSHA with SCRIPT LOAD
  • Thinking EVALSHA deletes scripts
  • Assuming EVALSHA lists scripts
2. Which of the following is the correct syntax for using EVALSHA in Redis?
easy
A. EVALSHA <numkeys> <sha1> [key1 key2 ...] [arg1 arg2 ...]
B. EVALSHA <sha1> <numkeys> [key1 key2 ...] [arg1 arg2 ...]
C. EVALSHA <sha1> [key1 key2 ...] <numkeys> [arg1 arg2 ...]
D. EVALSHA <script> <numkeys> [key1 key2 ...] [arg1 arg2 ...]

Solution

  1. Step 1: Recall EVALSHA syntax

    The correct syntax is: EVALSHA followed by the SHA1 hash, then the number of keys, then the keys, then the arguments.
  2. Step 2: Check option formats

    EVALSHA <sha1> <numkeys> [key1 key2 ...] [arg1 arg2 ...] matches the correct order. Options A, B, and C have wrong order or use script text instead of SHA1.
  3. Final Answer:

    EVALSHA <sha1> <numkeys> [key1 key2 ...] [arg1 arg2 ...] -> Option B
  4. Quick Check:

    Syntax = EVALSHA sha1 numkeys keys args [OK]
Hint: SHA1 hash comes first, then number of keys [OK]
Common Mistakes:
  • Putting script text instead of SHA1 hash
  • Mixing order of numkeys and keys
  • Omitting number of keys
3. Given the following Redis commands executed in order:
127.0.0.1:6379> SCRIPT LOAD "return {KEYS[1],ARGV[1]}"
"a1b2c3d4e5f6g7h8i9j0"
127.0.0.1:6379> EVALSHA a1b2c3d4e5f6g7h8i9j0 1 mykey myarg

What will be the output of the EVALSHA command?
medium
A. ["mykey", "myarg"]
B. ["myarg", "mykey"]
C. Error: NOSCRIPT No matching script
D. ["a1b2c3d4e5f6g7h8i9j0", "myarg"]

Solution

  1. Step 1: Understand SCRIPT LOAD output

    SCRIPT LOAD returns the SHA1 hash of the script, here "a1b2c3d4e5f6g7h8i9j0".
  2. Step 2: Analyze EVALSHA call

    EVALSHA runs the cached script with 1 key: "mykey" and 1 argument: "myarg". The script returns {KEYS[1], ARGV[1]} so output is ["mykey", "myarg"].
  3. Final Answer:

    ["mykey", "myarg"] -> Option A
  4. Quick Check:

    Return keys[1], argv[1] = [mykey, myarg] [OK]
Hint: SCRIPT LOAD returns SHA1; EVALSHA uses keys then args [OK]
Common Mistakes:
  • Confusing keys and args order
  • Expecting error without loading script
  • Using script text instead of SHA1
4. You run the command EVALSHA deadbeef 2 key1 key2 arg1 but get the error NOSCRIPT No matching script. What is the most likely cause?
medium
A. The number of keys argument should be 1, not 2
B. You provided too many keys for the script
C. The SHA1 hash 'deadbeef' is not loaded in Redis script cache
D. The script syntax is invalid

Solution

  1. Step 1: Understand NOSCRIPT error meaning

    This error means Redis does not have a cached script matching the given SHA1 hash.
  2. Step 2: Check command usage

    Since the hash 'deadbeef' is not loaded, Redis cannot run the script. Number of keys or syntax errors cause different errors.
  3. Final Answer:

    The SHA1 hash 'deadbeef' is not loaded in Redis script cache -> Option C
  4. Quick Check:

    NOSCRIPT = missing cached script [OK]
Hint: NOSCRIPT means script hash not cached yet [OK]
Common Mistakes:
  • Assuming wrong number of keys causes NOSCRIPT
  • Thinking syntax error causes NOSCRIPT
  • Ignoring that script must be loaded first
5. You want to optimize a Redis Lua script execution by caching it. Which sequence correctly uses SCRIPT LOAD and EVALSHA to run the script multiple times with different keys and arguments?
hard
A. Load script once with SCRIPT LOAD, save SHA1, then call EVALSHA with SHA1, numkeys, keys, args each time
B. Call EVALSHA with script text each time, no need to load script
C. Use SCRIPT FLUSH before every EVALSHA to clear cache
D. Call EVALSHA with SHA1 but always set numkeys to 0

Solution

  1. Step 1: Understand caching workflow

    First, SCRIPT LOAD uploads the script and returns its SHA1 hash. This hash is saved for reuse.
  2. Step 2: Use EVALSHA for repeated execution

    Use EVALSHA with the saved SHA1, specifying number of keys, keys, and arguments to run the script efficiently multiple times.
  3. Final Answer:

    Load script once with SCRIPT LOAD, save SHA1, then call EVALSHA with SHA1, numkeys, keys, args each time -> Option A
  4. Quick Check:

    Load once, run many times with EVALSHA [OK]
Hint: Load script once, then run with EVALSHA and SHA1 [OK]
Common Mistakes:
  • Calling EVALSHA with script text instead of SHA1
  • Flushing scripts before every run
  • Setting numkeys incorrectly
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