Transaction execution model in Redis - Time & Space Complexity
When using Redis transactions, it is important to understand how the time to execute commands grows as more commands are added.
We want to know how the total work changes when the number of commands in a transaction increases.
Analyze the time complexity of the following Redis transaction commands.
MULTI
SET key1 value1
INCR counter
GET key1
EXEC
This code starts a transaction, queues three commands, and then executes them all at once.
Look for repeated actions inside the transaction.
- Primary operation: Executing each queued command in the transaction.
- How many times: Once for each command queued before EXEC.
As you add more commands to the transaction, the total work grows with the number of commands.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 commands executed |
| 100 | 100 commands executed |
| 1000 | 1000 commands executed |
Pattern observation: The total execution time grows directly with the number of commands queued.
Time Complexity: O(n)
This means the time to run the transaction grows linearly with the number of commands inside it.
[X] Wrong: "All commands inside a transaction run instantly together, so time does not grow with more commands."
[OK] Correct: Each command still needs to be executed one by one during EXEC, so more commands mean more work and more time.
Understanding how Redis transactions scale helps you design efficient data operations and shows you can reason about performance in real systems.
"What if the transaction included commands that themselves take longer to run, like scanning many keys? How would that affect the time complexity?"
Practice
MULTI command in Redis transactions?Solution
Step 1: Understand the role of MULTI
TheMULTIcommand starts a transaction and queues subsequent commands instead of executing them immediately.Step 2: Differentiate MULTI from other commands
EXECruns the queued commands,DISCARDcancels them, and no command checks status directly.Final Answer:
To start a transaction and queue commands for atomic execution -> Option CQuick Check:
MULTI = start transaction [OK]
- Confusing MULTI with EXEC
- Thinking MULTI executes commands immediately
- Assuming MULTI discards commands
Solution
Step 1: Recall the transaction command order
First,MULTIstarts the transaction, then commands are queued, and finallyEXECruns them.Step 2: Verify the sequence
Only MULTI, commands, EXEC follows the correct order:MULTI, then commands, thenEXEC.Final Answer:
MULTI, commands, EXEC -> Option DQuick Check:
Order = MULTI -> commands -> EXEC [OK]
- Starting with EXEC instead of MULTI
- Running commands before MULTI
- Mixing order of commands and EXEC
MULTI SET key1 value1 INCR counter EXECWhat will be the result of the
EXEC command if counter does not exist before the transaction?Solution
Step 1: Analyze commands in the transaction
SET key1 value1sets a string key, andINCR counterincrements the counter key. Ifcounterdoes not exist, Redis treats it as 0 before incrementing.Step 2: Understand Redis transaction behavior
Redis queues commands and executes them atomically. Sincecounteris missing,INCRwill create it with value 1. Both commands succeed.Final Answer:
["OK", 1] -> Option BQuick Check:
INCR missing key = 1 [OK]
- Expecting error if key is missing
- Thinking transaction rolls back on error
- Confusing syntax error with runtime error
MULTI SET key1 value1 INCR key1 EXECWhat is the expected outcome and why?
Solution
Step 1: Examine commands and data types
SET key1 value1sets key1 to a string.INCR key1expects an integer but key1 is a string.Step 2: Understand Redis transaction error handling
Redis queues commands and executes all onEXEC. It does not rollback on errors.INCRfails with a type error, butSETsucceeds.Final Answer:
EXEC returns an error for INCR because key1 is not an integer -> Option AQuick Check:
Type error in transaction = error on EXEC [OK]
- Assuming transaction rolls back on error
- Expecting INCR to convert string automatically
- Thinking only first command runs
lock does not exist, using Redis transactions. Which sequence correctly ensures atomicity?Solution
Step 1: Understand the need for atomic check and increment
You must check iflockexists and incrementcounteronly if it does not, atomically.Step 2: Use WATCH to monitor
lockWATCH lockwatches the key for changes. Iflockchanges beforeEXEC, the transaction aborts, ensuring atomicity.Step 3: MULTI queues commands and EXEC runs them if
This sequence ensures the increment happens only iflockunchangedlockwas not modified.Final Answer:
WATCH lock MULTI INCR counter EXEC -> Option AQuick Check:
WATCH + MULTI = atomic check and update [OK]
- Not using WATCH for conditional logic
- Checking key inside MULTI without WATCH
- Assuming MULTI alone ensures atomic condition
Ready to prove it?
Pass the test and this pattern moves unknown
