0
0
Redisquery~10 mins

Why Lua scripts enable atomicity in Redis - Test Your Understanding

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
AEVAL
BEXEC
CCALL
DMULTI
Attempts:
3 left
💡 Hint
Common Mistakes
Using MULTI or EXEC instead of EVAL for Lua scripts.
2fill in blank
medium

Complete 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'
AEXISTS
BSET
CDEL
DGET
Attempts:
3 left
💡 Hint
Common Mistakes
Using SET instead of GET to retrieve a value.
3fill in blank
hard

Fix 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'
AGET
BINCR
CDEL
DSET
Attempts:
3 left
💡 Hint
Common Mistakes
Using GET or SET instead of INCR for incrementing.
4fill in blank
hard

Fill 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'
AEXISTS
BSET
CGET
DDEL
Attempts:
3 left
💡 Hint
Common Mistakes
Using GET instead of EXISTS to check key presence.
5fill in blank
hard

Fill 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'
AEXISTS
BINCR
CSET
DDEL
Attempts:
3 left
💡 Hint
Common Mistakes
Using DEL instead of SET to initialize the counter.