Bird
Raised Fist0
Redisquery~10 mins

Script loading and caching in Redis - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to load a Lua script into Redis and get its SHA1 hash.

Redis
sha1 = redis.call('SCRIPT', '[1]', script)
Drag options to blanks, or click blank then click option'
ALOAD
BEXECUTE
CRUN
DEVAL
Attempts:
3 left
💡 Hint
Common Mistakes
Using EVAL instead of LOAD to get the SHA1 hash.
Using EXECUTE which is not a Redis command.
Confusing RUN with the correct command.
2fill in blank
medium

Complete the code to execute a cached Lua script by its SHA1 hash.

Redis
result = redis.call('[1]', sha1, 0)
Drag options to blanks, or click blank then click option'
AEVALSHA
BLOAD
CEXECUTE
DRUN
Attempts:
3 left
💡 Hint
Common Mistakes
Using LOAD instead of EVALSHA to execute the script.
Using EXECUTE which is not a Redis command.
Using RUN which is not a Redis command.
3fill in blank
hard

Fix the error in the code to check if a script is cached before running it.

Redis
if redis.call('SCRIPT', '[1]', sha1) == 1 then
    redis.call('EVALSHA', sha1, 0)
else
    sha1 = redis.call('SCRIPT', 'LOAD', script)
    redis.call('EVALSHA', sha1, 0)
end
Drag options to blanks, or click blank then click option'
AEXECUTE
BLOAD
CRUN
DEXISTS
Attempts:
3 left
💡 Hint
Common Mistakes
Using LOAD to check existence instead of EXISTS.
Using RUN or EXECUTE which are not Redis commands.
Confusing SCRIPT LOAD with SCRIPT EXISTS.
4fill in blank
hard

Fill both blanks to load a script only if it is not cached, then execute it.

Redis
if redis.call('SCRIPT', '[1]', sha1) == 0 then
    sha1 = redis.call('SCRIPT', '[2]', script)
end
redis.call('EVALSHA', sha1, 0)
Drag options to blanks, or click blank then click option'
AEXISTS
BLOAD
CRUN
DEXECUTE
Attempts:
3 left
💡 Hint
Common Mistakes
Using LOAD to check existence.
Using RUN or EXECUTE commands which do not exist.
Not updating the sha1 variable after loading.
5fill in blank
hard

Fill all three blanks to cache a script, check its existence, and execute it safely.

Redis
sha1 = redis.call('SCRIPT', '[1]', script)
if redis.call('SCRIPT', '[2]', sha1) == 1 then
    redis.call('[3]', sha1, 0)
else
    redis.call('EVAL', script, 0)
end
Drag options to blanks, or click blank then click option'
ALOAD
BEXISTS
CEVALSHA
DRUN
Attempts:
3 left
💡 Hint
Common Mistakes
Using RUN which is not a Redis command.
Using EVAL instead of EVALSHA to run cached script.
Not checking if the script exists before running.

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

  1. Step 1: Understand SCRIPT LOAD purpose

    The SCRIPT LOAD command stores a Lua script in Redis and returns a unique SHA1 hash for it.
  2. Step 2: Differentiate from other commands

    Unlike EVAL, SCRIPT LOAD does not execute the script but prepares it for later use.
  3. Final Answer:

    It loads a Lua script into Redis and returns its SHA1 hash. -> Option D
  4. 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

  1. Step 1: Identify correct command syntax

    The correct command to load a script is SCRIPT LOAD followed by the Lua script as a string.
  2. Step 2: Eliminate incorrect options

    EVALSHA runs a cached script by SHA, SCRIPT EXECUTE and LOAD SCRIPT are invalid Redis commands.
  3. Final Answer:

    SCRIPT LOAD "return redis.call('PING')" -> Option B
  4. 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:
SCRIPT LOAD "return ARGV[1]"
EVALSHA <sha1> 0 Hello
What will be the output of the EVALSHA command?
medium
A. 0
B. An error because the script is not cached
C. "Hello"
D. The SHA1 hash string

Solution

  1. 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.
  2. 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".
  3. Final Answer:

    "Hello" -> Option C
  4. Quick Check:

    EVALSHA runs cached script returning ARGV[1] = "Hello" [OK]
Hint: EVALSHA runs cached script with ARGV arguments [OK]
Common Mistakes:
  • Confusing SCRIPT LOAD output with EVALSHA output
  • Forgetting to pass correct number of keys (0 here)
  • Expecting SHA1 hash as output of EVALSHA
4. You run the following Redis commands:
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?
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

  1. Step 1: Understand NOSCRIPT error meaning

    This error means Redis does not have the script cached under the given SHA1 hash.
  2. Step 2: Identify common causes

    Most often, the SHA1 hash passed to EVALSHA is wrong or from a different script than loaded.
  3. Final Answer:

    The SHA1 hash used in EVALSHA is incorrect or from a different script. -> Option A
  4. 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

  1. Step 1: Understand caching for efficiency

    Loading the script once with SCRIPT LOAD caches it and returns SHA1 for reuse.
  2. Step 2: Use EVALSHA for repeated execution

    Calling EVALSHA with the SHA1 runs the cached script efficiently without sending full script each time.
  3. Step 3: Eliminate inefficient options

    Using EVAL every time sends full script, SCRIPT FLUSH clears cache unnecessarily.
  4. Final Answer:

    Use SCRIPT LOAD to get SHA1, then call EVALSHA with the SHA1 and arguments for each increment. -> Option A
  5. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard