0
0
Redisquery~10 mins

Atomic operations with Lua in Redis - Interactive Code Practice

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

Complete the Lua script to increment a Redis key atomically.

Redis
redis.call('[1]', KEYS[1])
Drag options to blanks, or click blank then click option'
Adel
Bget
Cincr
Dset
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'get' instead of 'incr' returns the value but does not increment.
Using 'set' overwrites the value instead of incrementing.
2fill in blank
medium

Complete the Lua script to set a key only if it does not exist.

Redis
redis.call('[1]', KEYS[1], ARGV[1])
Drag options to blanks, or click blank then click option'
Asetnx
Bgetset
Cset
Ddel
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'set' overwrites the key regardless of existence.
Using 'getset' replaces the value and returns the old one.
3fill in blank
hard

Fix the error in the Lua script to decrement a key's value atomically.

Redis
redis.call('[1]', KEYS[1])
Drag options to blanks, or click blank then click option'
Aincr
Bdecr
Cdel
Dexpire
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'incr' increases the value instead of decreasing.
Using 'del' deletes the key instead of decrementing.
4fill in blank
hard

Fill both blanks to atomically add a member to a set and set an expiration time.

Redis
redis.call('[1]', KEYS[1], ARGV[1])
redis.call('[2]', KEYS[1], tonumber(ARGV[2]))
Drag options to blanks, or click blank then click option'
Asadd
Bexpire
Cset
Ddel
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'set' instead of 'sadd' overwrites the key.
Using 'del' removes the key instead of setting expiration.
5fill in blank
hard

Fill all three blanks to atomically increment a hash field, check its value, and delete the key if zero.

Redis
local newVal = redis.call('[1]', KEYS[1], ARGV[1], 1)
if newVal == tonumber('[2]') then
  redis.call('[3]', KEYS[1])
end
Drag options to blanks, or click blank then click option'
Ahincrby
B0
Cdel
Dhset
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'hset' instead of 'hincrby' does not increment.
Comparing to '1' instead of '0' causes wrong deletion logic.