Consider the following Redis commands executed as a transaction:
MULTI SET key1 value1 INCR counter EXEC
Assuming counter was not set before, what will be the result of the EXEC command?
MULTI SET key1 value1 INCR counter EXEC
Remember that INCR initializes the key to 0 before incrementing if it does not exist.
The SET command returns OK. The INCR command increments counter from 0 to 1 and returns the new value as an integer 1. So the EXEC returns an array with ["OK", 1].
Which of the following statements about Redis transactions is true?
Think about what Redis transactions do and do not guarantee.
Redis transactions guarantee atomicity (all commands run as one unit) and isolation (no other commands run between them). However, Redis does not guarantee durability because data may be lost if the server crashes before persistence.
Which option contains a syntax error that will cause the Redis transaction to fail?
MULTI SET key1 value1 INCR EXEC
Check the command arguments carefully.
The INCR command requires a key argument. Option A misses the key argument, causing a syntax error.
Given the commands:
WATCH key1 MULTI SET key1 value1 EXEC
Assuming another client modifies key1 after WATCH but before EXEC, what will be the result of EXEC?
Think about what happens when a watched key is modified before EXEC.
If a watched key changes before EXEC, the transaction is aborted and EXEC returns null, meaning no commands run.
You want to increment a counter only if a key exists, using a Redis transaction. Which approach best avoids race conditions?
Think about atomic operations and avoiding race conditions.
Lua scripting runs atomically on Redis server, so checking and incrementing in one script avoids race conditions better than WATCH/MULTI.