📖 Scenario: You are managing a Redis database for a simple online store. You want to update the stock quantity and sales count together without any other operations interrupting. This ensures your data stays accurate and consistent.
🎯 Goal: Build a Lua script in Redis that updates stock and sales atomically, so both changes happen together or not at all.
📋 What You'll Learn
Create a Lua script that decreases the stock count by 1
Increase the sales count by 1 in the same script
Ensure the script runs atomically in Redis
Use Redis commands inside the Lua script to update keys
💡 Why This Matters
🌍 Real World
Online stores and inventory systems need to update stock and sales counts together to avoid errors from simultaneous updates.
💼 Career
Understanding Lua scripting in Redis is valuable for backend developers and database administrators to ensure data consistency and atomic operations.
Progress0 / 4 steps
1
Setup Redis keys for stock and sales
Create two Redis keys called stock and sales with initial values 100 and 0 respectively using Redis commands.
Redis
Hint
Use the SET command to create keys with initial values.
2
Write a Lua script to update stock and sales
Write a Lua script that decreases the stock key by 1 and increases the sales key by 1 using Redis commands redis.call('DECR', 'stock') and redis.call('INCR', 'sales').
Redis
Hint
Use redis.call inside the Lua script to run Redis commands atomically.
3
Run the Lua script atomically in Redis
Use the Redis EVAL command to run the Lua script atomically. The script should be passed as a string to EVAL with 0 keys argument count.
Redis
Hint
Use EVAL to execute the Lua script atomically in Redis.
4
Complete the Lua script with return values
Modify the Lua script to return the updated stock and sales values as a table at the end using return {stock, sales}.
Redis
Hint
Return the updated values as a table to see the result of the atomic operation.
Practice
(1/5)
1. Why do Lua scripts in Redis ensure atomicity?
easy
A. Because they run as a single, uninterruptible operation
B. Because they use multiple threads simultaneously
C. Because they store data in separate databases
D. Because they execute commands randomly
Solution
Step 1: Understand Lua script execution in Redis
Lua scripts run all commands inside them as one single operation without interruption.
Step 2: Connect atomicity to uninterrupted execution
This uninterrupted execution means no other commands can interfere, ensuring atomicity.
Final Answer:
Because they run as a single, uninterruptible operation -> Option A
Quick Check:
Atomicity = single uninterruptible operation [OK]
Hint: Atomicity means no interruptions during script run [OK]
Common Mistakes:
Thinking Lua scripts run in multiple threads
Believing Lua scripts split commands across databases
Assuming commands execute randomly
2. Which of the following is the correct way to start a Lua script in Redis?
easy
A. EVAL 'return redis.call("GET", "key")' 1 key
B. RUN 'return redis.call("GET", "key")' 1 key
C. EXECUTE 'return redis.call("GET", "key")' 1 key
D. SCRIPT 'return redis.call("GET", "key")' 1 key
Solution
Step 1: Identify Redis command to run Lua scripts
Redis uses the EVAL command to run Lua scripts.
Step 2: Check syntax correctness
EVAL 'script' number_of_keys keys... is the correct syntax.
Final Answer:
EVAL 'return redis.call("GET", "key")' 1 key -> Option A
Quick Check:
Use EVAL to run Lua scripts [OK]
Hint: Lua scripts start with EVAL command in Redis [OK]
Common Mistakes:
Using RUN instead of EVAL
Using EXECUTE or SCRIPT commands incorrectly
Missing the number of keys argument
3. What will be the output of this Lua script run in Redis?