📖 Scenario: You are managing a Redis database for a small online store. You want to use Lua scripting to perform atomic operations that combine multiple Redis commands into one.
🎯 Goal: Learn how to write and execute a simple Lua script using the Redis EVAL command to increment a product's stock count safely.
📋 What You'll Learn
Create a Redis key for a product stock count with an initial value
Write a Lua script that increments the stock count by a given amount
Use the EVAL command to run the Lua script with keys and arguments
Verify the stock count is updated correctly after script execution
💡 Why This Matters
🌍 Real World
Using Lua scripts with Redis EVAL allows atomic operations that combine multiple commands, useful in inventory management, counters, and session handling.
💼 Career
Many backend developers and database administrators use Redis scripting to ensure data consistency and improve performance in real-time applications.
Progress0 / 4 steps
1
DATA SETUP: Create a Redis key for product stock
Use the Redis command SET to create a key called product_stock with the initial value 10.
Redis
Hint
Use the SET command to assign the value 10 to the key product_stock.
2
CONFIGURATION: Write a Lua script to increment stock
Create a Lua script string called increment_script that increments the key's value by the first argument ARGV[1]. Use redis.call('INCRBY', KEYS[1], ARGV[1]) inside the script.
Redis
Hint
Assign the Lua script as a string to increment_script. It should call INCRBY on KEYS[1] with ARGV[1] converted to a number.
3
CORE LOGIC: Use EVAL to run the Lua script
Use the Redis EVAL command to run the increment_script with 1 key, the key name product_stock, and the increment value 5 as argument. The command format is EVAL increment_script 1 product_stock 5.
Redis
Hint
Use the EVAL command with the script variable, number of keys 1, the key product_stock, and the increment 5.
4
COMPLETION: Verify the updated stock count
Use the Redis GET command to check the value of product_stock after running the script. The command is GET product_stock.
Redis
Hint
Use GET product_stock to see the new stock value after increment.
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
Step 1: Understand Redis scripting
Redis uses Lua scripts to perform complex or multiple operations atomically.
Step 2: Identify EVAL command role
The EVAL command runs Lua scripts inside Redis, ensuring atomicity and reducing network calls.
Final Answer:
To run Lua scripts inside Redis for atomic operations -> Option A
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
Step 1: Understand the Lua script
The script calls redis.call('GET', KEYS[1]), which fetches the value of the first key passed.
Step 2: Analyze the command
The command passes 1 key: mykey. If mykey holds 'hello', the script returns 'hello'.
Final Answer:
'hello' -> Option C
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
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.
Step 2: Identify syntax error
This causes a syntax error because the shell or Redis cannot parse the string properly.
Final Answer:
The Lua script string uses conflicting single quotes causing syntax error -> Option B
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?