0
0
Redisquery~20 mins

Atomic operations with Lua in Redis - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Lua Atomic Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
What is the output of this Lua script in Redis?
Consider the following Lua script executed in Redis:
redis.call('SET', 'counter', 10)
local val = redis.call('INCRBY', 'counter', 5)
return val

What will be the output after running this script?
Redis
redis.call('SET', 'counter', 10)
local val = redis.call('INCRBY', 'counter', 5)
return val
Anil
B10
C5
D15
Attempts:
2 left
💡 Hint
Remember that INCRBY increases the value stored at the key by the given amount and returns the new value.
📝 Syntax
intermediate
2:00remaining
Which Lua script will cause a syntax error in Redis?
Identify the Lua script that will cause a syntax error when executed in Redis.
Alocal val = redis.call('INCR', 'counter') return val
Blocal val = redis.call('GET', 'key' return val
Clocal val = redis.call('SET', 'key', 'value') return val
Dreturn redis.call('GET', 'key')
Attempts:
2 left
💡 Hint
Look for missing or misplaced parentheses.
optimization
advanced
2:00remaining
Which Lua script is the most efficient for incrementing a counter and returning its new value?
You want to increment a Redis key 'visits' atomically and get the updated value. Which script is the most efficient?
Areturn redis.call('INCR', 'visits')
B
local current = redis.call('GET', 'visits')
local new = tonumber(current) + 1
redis.call('SET', 'visits', new)
return new
C
redis.call('SET', 'visits', redis.call('GET', 'visits') + 1)
return redis.call('GET', 'visits')
D
local new = redis.call('INCRBY', 'visits', 1)
return new
Attempts:
2 left
💡 Hint
Consider atomicity and number of Redis calls.
🧠 Conceptual
advanced
2:00remaining
Why are Lua scripts in Redis considered atomic?
Choose the best explanation for why Lua scripts executed in Redis are atomic.
ABecause Redis queues Lua scripts and executes them one at a time without interruption.
BBecause Redis locks the entire database during script execution, preventing other commands.
CBecause Lua scripts run in a separate thread from Redis commands.
DBecause Lua scripts automatically rollback if an error occurs.
Attempts:
2 left
💡 Hint
Think about how Redis handles commands and scripts internally.
🔧 Debug
expert
2:00remaining
What error does this Lua script raise in Redis?
Given the Lua script:
local val = redis.call('INCRBY', 'counter', 'five')
return val

What error will Redis return when executing this script?
Redis
local val = redis.call('INCRBY', 'counter', 'five')
return val
Anil
BSyntaxError: unexpected string
CERR value is not an integer or out of range
DTypeError: argument must be a number
Attempts:
2 left
💡 Hint
INCRBY expects a numeric increment value.