Bird
Raised Fist0
Redisquery~10 mins

EVAL command for Lua execution in Redis - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - EVAL command for Lua execution
Start EVAL command
↓
Parse Lua script
↓
Receive KEYS and ARGV
↓
Execute Lua script with KEYS and ARGV
↓
Return result to client
↓
End
The EVAL command runs a Lua script on Redis server using provided keys and arguments, then returns the script's result.
Execution Sample
Redis
EVAL "return {KEYS[1], ARGV[1]}" 1 mykey myarg
Runs a Lua script that returns the first key and first argument passed to it.
Execution Table
StepActionInputLua VariablesOutput
1Receive EVAL commandScript: return {KEYS[1], ARGV[1]}, NumKeys: 1, Keys: [mykey], Args: [myarg]KEYS = ["mykey"], ARGV = ["myarg"]None yet
2Parse Lua scriptreturn {KEYS[1], ARGV[1]}KEYS = ["mykey"], ARGV = ["myarg"]None yet
3Execute Lua scriptreturn {KEYS[1], ARGV[1]}KEYS = ["mykey"], ARGV = ["myarg"]["mykey", "myarg"]
4Return resultResult from scriptNone["mykey", "myarg"]
💡 Script execution completes and returns the array with first key and argument.
Variable Tracker
VariableStartAfter Step 1After Step 3Final
KEYSempty["mykey"]["mykey"]["mykey"]
ARGVempty["myarg"]["myarg"]["myarg"]
Outputnonenone["mykey", "myarg"]["mykey", "myarg"]
Key Moments - 2 Insights
Why do we specify the number of keys after the Lua script in EVAL?
Because Redis needs to know how many arguments are keys (in KEYS array) and how many are regular arguments (in ARGV). This is shown in execution_table row 1 where NumKeys is 1.
What happens if the Lua script tries to access KEYS or ARGV indices that don't exist?
The Lua script will get nil for those indices, which can cause errors or unexpected results. This is implied in the variable_tracker where KEYS and ARGV only have one element each.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of ARGV after Step 1?
Aempty
B["mykey"]
C["myarg"]
Dnil
💡 Hint
Check the 'Lua Variables' column in row 1 of execution_table.
At which step does the Lua script actually run and produce output?
AStep 3
BStep 2
CStep 1
DStep 4
💡 Hint
Look for the step where 'Execute Lua script' action happens in execution_table.
If the number of keys specified was 0, where would 'mykey' appear?
AIn KEYS array
BIn ARGV array
CNowhere, error occurs
DIn both KEYS and ARGV
💡 Hint
Refer to key_moments explanation about how Redis separates keys and arguments.
Concept Snapshot
EVAL command runs Lua scripts on Redis server.
Syntax: EVAL <script> <numkeys> <key1> ... <arg1> ...
KEYS array holds keys, ARGV holds other args.
Script runs atomically and returns result.
Use to extend Redis with custom logic.
Full Transcript
The Redis EVAL command executes a Lua script on the server. You provide the script, the number of keys, then the keys and arguments. Redis separates keys into the KEYS array and other arguments into ARGV. The Lua script runs with these inputs and returns a result. For example, the script 'return {KEYS[1], ARGV[1]}' returns the first key and first argument. The execution steps include receiving the command, parsing the script, running it with KEYS and ARGV, and returning the output. It's important to specify the number of keys correctly so Redis knows how to split inputs. If the script accesses keys or args that don't exist, it gets nil. This command allows custom logic inside Redis with atomic execution.

Practice

(1/5)
1. What is the main purpose of the EVAL command in Redis?
easy
A. To run Lua scripts inside Redis for atomic operations
B. To execute SQL queries on Redis data
C. To backup Redis data to a file
D. To create new Redis databases

Solution

  1. Step 1: Understand Redis scripting

    Redis uses Lua scripts to perform complex or multiple operations atomically.
  2. Step 2: Identify EVAL command role

    The EVAL command runs Lua scripts inside Redis, ensuring atomicity and reducing network calls.
  3. Final Answer:

    To run Lua scripts inside Redis for atomic operations -> Option A
  4. Quick Check:

    EVAL runs Lua scripts = D [OK]
Hint: EVAL runs Lua scripts atomically inside Redis [OK]
Common Mistakes:
  • Confusing EVAL with SQL execution
  • Thinking EVAL backs up data
  • Assuming EVAL creates databases
2. Which of the following is the correct syntax to run a Lua script with EVAL in Redis?
easy
A. EVAL script numkeys key [key ...] arg [arg ...]
B. EVAL numkeys script key [key ...] arg [arg ...]
C. EVAL script key numkeys arg [arg ...]
D. EVAL script arg numkeys key [key ...]

Solution

  1. Step 1: Recall EVAL syntax

    The correct syntax is EVAL script numkeys key [key ...] arg [arg ...], where numkeys tells how many keys follow.
  2. Step 2: Match options

    Only EVAL script numkeys key [key ...] arg [arg ...] matches this exact order and structure.
  3. Final Answer:

    EVAL script numkeys key [key ...] arg [arg ...] -> Option A
  4. Quick Check:

    Script, numkeys, keys, args order = A [OK]
Hint: Remember: script, numkeys, keys, then args in EVAL [OK]
Common Mistakes:
  • Placing numkeys after keys or args
  • Mixing order of keys and args
  • Omitting numkeys parameter
3. Given the Lua script return redis.call('GET', KEYS[1]) and the command EVAL script 1 mykey, what will Redis return if mykey holds the value 'hello'?
medium
A. An error because arguments are missing
B. nil
C. 'hello'
D. The string 'mykey'

Solution

  1. Step 1: Understand the Lua script

    The script calls redis.call('GET', KEYS[1]), which fetches the value of the first key passed.
  2. Step 2: Analyze the command

    The command passes 1 key: mykey. If mykey holds 'hello', the script returns 'hello'.
  3. Final Answer:

    'hello' -> Option C
  4. Quick Check:

    GET mykey = 'hello' [OK]
Hint: Lua script returns value of first key passed [OK]
Common Mistakes:
  • Confusing keys and args
  • Expecting key name instead of value
  • Assuming nil if key exists
4. What is wrong with this Redis command? EVAL 'return redis.call('GET', KEYS[1])' 1 mykey
medium
A. Missing number of keys argument
B. The Lua script string uses conflicting single quotes causing syntax error
C. Keys and args are swapped
D. The command is correct and will run successfully

Solution

  1. Step 1: Inspect Lua script quoting

    The Lua script is enclosed in single quotes, but inside it also uses single quotes for 'GET', causing quote conflict.
  2. Step 2: Identify syntax error

    This causes a syntax error because the shell or Redis cannot parse the string properly.
  3. Final Answer:

    The Lua script string uses conflicting single quotes causing syntax error -> Option B
  4. Quick Check:

    Quote conflict in script string = A [OK]
Hint: Use different quotes or escape inner quotes in Lua script [OK]
Common Mistakes:
  • Ignoring quote conflicts
  • Forgetting to escape inner quotes
  • Miscounting number of keys
5. You want to atomically increment a Redis key counter by a value passed as an argument using EVAL. Which Lua script and command correctly achieve this?
hard
A. Script: return redis.call('INCRBY', ARGV[1], KEYS[1])
Command: EVAL script 1 counter 5
B. Script: return redis.call('INCRBY', KEYS[1], ARGV[1])
Command: EVAL script 2 counter 5
C. Script: return redis.call('INCRBY', KEYS[1], ARGV[1])
Command: EVAL script 0 counter 5
D. Script: return redis.call('INCRBY', KEYS[1], ARGV[1])
Command: EVAL script 1 counter 5

Solution

  1. Step 1: Understand INCRBY usage in Lua script

    The command redis.call('INCRBY', KEYS[1], ARGV[1]) increments the key by the argument value.
  2. Step 2: Check command arguments

    Passing 1 as number of keys, then counter as key, and 5 as argument matches the script.
  3. Final Answer:

    Script: return redis.call('INCRBY', KEYS[1], ARGV[1]) and Command: EVAL script 1 counter 5 -> Option D
  4. Quick Check:

    INCRBY key arg with correct keys count = B [OK]
Hint: Keys count must match keys passed; args follow keys [OK]
Common Mistakes:
  • Swapping keys and args order
  • Incorrect number of keys argument
  • Using zero keys when keys are needed
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