Bird
Raised Fist0
Redisquery~20 mins

Lua script syntax in Redis - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Redis Lua Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate
2:00remaining
What does this Redis Lua script return?
Consider this Lua script run inside Redis:
return redis.call('SET', KEYS[1], ARGV[1])

What is the output if the key does not exist before?
Redis
return redis.call('SET', KEYS[1], ARGV[1])
AError: wrong number of arguments
Bnil
C0
D"OK"
Attempts:
2 left
💡 Hint
The SET command returns a simple string reply when successful.
📝 Syntax
intermediate
2:00remaining
Which Lua script syntax is correct for incrementing a key's value?
You want to increment the integer value stored at a key using Redis Lua script. Which option is syntactically correct?
Aredis.call('INCR', KEYS[1])
Bredis.call('INCR', ARGV[1])
Credis.call('INCR')
Dredis.call('INCR', KEYS)
Attempts:
2 left
💡 Hint
The INCR command requires exactly one key argument.
🔧 Debug
advanced
2:00remaining
Why does this Lua script cause an error in Redis?
Given this script:
local val = redis.call('GET', KEYS[1])
return val + 1

What error will it cause and why?
Redis
local val = redis.call('GET', KEYS[1])
return val + 1
AAttempt to perform arithmetic on a nil value error
BSyntax error: unexpected symbol near '+'
CAttempt to perform arithmetic on a string value error
DNo error, returns incremented value
Attempts:
2 left
💡 Hint
redis.call('GET', key) returns a string or nil, not a number.
🧠 Conceptual
advanced
2:00remaining
What is the purpose of KEYS and ARGV in Redis Lua scripts?
In Redis Lua scripting, what do KEYS and ARGV represent and why are they separated?
AKEYS holds keys to access; ARGV holds additional arguments; separation helps Redis optimize command execution.
BKEYS holds values; ARGV holds keys.
CKEYS and ARGV both hold keys but in different formats.
DKEYS holds all arguments; ARGV is unused in scripts.
Attempts:
2 left
💡 Hint
Think about how Redis needs to know which keys a script will access.
❓ optimization
expert
2:00remaining
How to optimize a Redis Lua script that increments multiple keys atomically?
You want to increment 100 keys atomically in Redis using Lua. Which approach is best for performance?
ACall redis.call('INCR', key) 100 times in separate scripts.
BUse a single Lua script with a loop calling redis.call('INCR', key) for each key.
CUse redis.pcall inside the script to increment keys concurrently.
DUse a Lua script that builds a multi-command string and calls redis.call once.
Attempts:
2 left
💡 Hint
Lua scripts run atomically and reduce network round-trips.

Practice

(1/5)
1. What is the correct way to start a Lua script in Redis using the EVAL command?
easy
A. EVAL redis.call('PING') 0
B. EVAL 'return redis.call("PING")' 0
C. EVAL 'PING' 0
D. EVAL return redis.call('PING') 0

Solution

  1. Step 1: Understand EVAL syntax

    The EVAL command requires a Lua script as a string, followed by the number of keys and then the keys and arguments.
  2. Step 2: Check correct Lua script format

    The Lua script must be a string and use redis.call to execute Redis commands, e.g., return redis.call("PING").
  3. Final Answer:

    EVAL 'return redis.call("PING")' 0 -> Option B
  4. Quick Check:

    EVAL with Lua string and 0 keys = A [OK]
Hint: EVAL needs Lua code as string, then number of keys [OK]
Common Mistakes:
  • Not passing Lua code as a string
  • Missing return statement in Lua script
  • Incorrect number of keys argument
  • Using Redis commands directly without redis.call
2. Which of the following is the correct syntax to call the Redis command SET inside a Lua script?
easy
A. redis.call('SET', 'key', 'value')
B. redis.SET('key', 'value')
C. call.redis('SET', 'key', 'value')
D. redis.call.SET('key', 'value')

Solution

  1. Step 1: Recall Redis Lua API

    In Lua scripts for Redis, commands are called using redis.call with the command name as first argument.
  2. Step 2: Check syntax correctness

    The correct syntax is redis.call('SET', 'key', 'value'). Other options misuse the API or method calls.
  3. Final Answer:

    redis.call('SET', 'key', 'value') -> Option A
  4. Quick Check:

    Use redis.call('COMMAND', args...) = B [OK]
Hint: Use redis.call('COMMAND', args) to run Redis commands in Lua [OK]
Common Mistakes:
  • Using dot notation like redis.SET
  • Swapping order of arguments
  • Using call.redis instead of redis.call
  • Missing quotes around command name
3. What will the following Lua script return when run with EVAL in Redis?
return redis.call('INCR', KEYS[1])

Assuming the key counter does not exist before and the script is called with KEYS[1] = 'counter'.
medium
A. 0
B. Error
C. nil
D. 1

Solution

  1. Step 1: Understand INCR behavior on non-existing key

    INCR command creates the key with value 0 then increments it by 1, so the new value is 1.
  2. Step 2: Return value of redis.call('INCR', key)

    The call returns the new value after increment, which is 1.
  3. Final Answer:

    1 -> Option D
  4. Quick Check:

    INCR on new key returns 1 [OK]
Hint: INCR on missing key returns 1 after creating it [OK]
Common Mistakes:
  • Assuming INCR returns 0 on new key
  • Expecting nil for missing key
  • Confusing return value with command success
  • Not passing KEYS argument correctly
4. Identify the error in this Lua script for Redis:
local val = redis.call('GET', KEYS[1])
if val == nil then
  redis.call('SET', KEYS[1], '1')
else
  redis.call('INCR', KEYS[1])
end
return redis.call('GET', KEYS[1])
medium
A. Using local variable val is not allowed
B. Missing return statement in Lua script
C. Using redis.call('INCR') on a string value causes error
D. Incorrect use of KEYS array index

Solution

  1. Step 1: Review the script logic

    The script fetches the value with GET. If nil, sets to '1'; else calls INCR, then returns the final GET.
  2. Step 2: Spot the problem with INCR

    redis.call('INCR', KEYS[1]) will raise an error if the key's existing value is a non-integer string (e.g., 'abc'). The script does not validate the value type before incrementing.
  3. Step 3: Rule out other options

    There is a return statement (not B), local variables are allowed in Redis Lua (not C), and KEYS[1] uses correct 1-based indexing (not D).
  4. Final Answer:

    Using redis.call('INCR') on a string value causes error -> Option C
  5. Quick Check:

    INCR errors on non-integer strings [OK]
Hint: INCR works on strings representing integers only [OK]
Common Mistakes:
  • Thinking INCR fails on string '1'
  • Confusing local variable usage
  • Assuming missing return statement
  • Misusing KEYS index
5. You want to atomically increment a counter only if it exists in Redis using a Lua script. Which script correctly implements this?
-- Option A
if redis.call('EXISTS', KEYS[1]) == 1 then
  return redis.call('INCR', KEYS[1])
else
  return nil
end

-- Option B
return redis.call('INCR', KEYS[1])

-- Option C
if redis.call('GET', KEYS[1]) then
  redis.call('INCR', KEYS[1])
end
return redis.call('GET', KEYS[1])

-- Option D
if redis.call('EXISTS', KEYS[1]) == 0 then
  return redis.call('INCR', KEYS[1])
else
  return nil
end
hard
A. Option A
B. Option B
C. Option C
D. Option D

Solution

  1. Step 1: Understand requirement

    Increment counter only if it exists. If not, return nil without creating it.
  2. Step 2: Analyze the scripts

    The script checking redis.call('EXISTS', KEYS[1]) == 1 then returning redis.call('INCR', KEYS[1]) else nil matches exactly.
    One script calls INCR unconditionally, creating if missing.
    Another checks if redis.call('GET', KEYS[1]) (truthy for any non-nil), calls INCR but returns subsequent GET (may error on non-integers or not return increment directly).
    The last increments if EXISTS == 0, opposite of needed.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Check EXISTS before INCR to avoid creating key [OK]
Hint: Check EXISTS == 1 before INCR to avoid creating new key [OK]
Common Mistakes:
  • Incrementing without checking key existence
  • Using EXISTS == 0 to increment
  • Not returning correct value after increment
  • Using GET without handling nil properly
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