Which statement best describes the atomicity guarantee when using Lua scripts compared to Redis transactions?
Think about how Redis handles command execution inside Lua scripts versus MULTI/EXEC blocks.
Lua scripts in Redis run atomically, meaning all commands inside the script execute as a single, uninterrupted operation. Transactions using MULTI/EXEC queue commands and execute them atomically, but other commands may interleave between MULTI and EXEC.
Given two clients concurrently incrementing the same Redis key, which approach prevents race conditions?
Client 1 uses a Lua script to increment the key. Client 2 uses MULTI/EXEC transaction commands to increment the key.
What is the expected behavior?
Consider how Redis executes Lua scripts and transactions in the context of concurrent clients.
Lua scripts execute atomically, so concurrent increments via scripts do not cause race conditions. Transactions queue commands and execute them atomically, but overlapping transactions can cause race conditions if commands are not properly isolated.
Which Lua script correctly increments a Redis key atomically?
local current = redis.call('GET', KEYS[1]) local new = tonumber(current) + 1 redis.call('SET', KEYS[1], new) return new
Remember Redis stores strings, so the value set must be a string.
Redis expects string values for SET commands. Option A converts the incremented number back to a string before setting it, ensuring correct data type and no errors.
You want to execute multiple Redis commands atomically and efficiently. Which approach is generally faster and why?
Think about network round-trips and server-side execution.
Lua scripts execute all commands inside Redis in one call, reducing network round-trips and ensuring atomic execution. Transactions still require multiple commands sent from client to server.
A Redis transaction using MULTI/EXEC fails silently and does not apply any commands. Which of the following is the most likely cause?
Consider how Redis handles errors inside transactions.
If any command queued in a MULTI block is invalid, EXEC will abort and not apply any commands, causing silent failure of the transaction.