Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to start a Lua script in Redis that runs atomically.
Redis
[1] "return redis.call('SET', 'key', 'value')" 0
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using MULTI or EXEC instead of EVAL for Lua scripts.
✗ Incorrect
The EVAL command runs Lua scripts atomically in Redis.
2fill in blank
mediumComplete the code to return a value from a Lua script in Redis.
Redis
return redis.call([1], 'GET', 'key')
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using SET instead of GET to retrieve a value.
✗ Incorrect
The GET command retrieves the value of a key in Redis.
3fill in blank
hardFix the error in the Lua script to ensure atomic execution.
Redis
redis.call('SET', 'counter', redis.call([1], 'INCR', 'counter'))
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using GET or SET instead of INCR for incrementing.
✗ Incorrect
The INCR command increments a counter atomically in Redis.
4fill in blank
hardFill both blanks to create a Lua script that atomically checks and sets a key.
Redis
if redis.call([1], 'EXISTS', 'lock') == 0 then redis.call([2], 'SET', 'lock', '1') end
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using GET instead of EXISTS to check key presence.
✗ Incorrect
The script checks if 'lock' exists and sets it if not, all atomically.
5fill in blank
hardFill all three blanks to write a Lua script that atomically increments a counter only if it exists.
Redis
if redis.call([1], 'EXISTS', 'counter') == 1 then return redis.call([2], 'INCR', 'counter') else return redis.call([3], 'SET', 'counter', '1') end
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using DEL instead of SET to initialize the counter.
✗ Incorrect
This script checks if 'counter' exists, increments it if yes, or sets it to 1 if no, all atomically.