EXEC to execute in Redis - Time & Space Complexity
We want to understand how the time to run a Redis transaction changes as we add more commands inside it.
How does the number of commands inside EXEC affect the total time to complete?
Analyze the time complexity of the following Redis transaction using EXEC.
MULTI
SET key1 value1
SET key2 value2
... (more SET commands) ...
EXEC
This code starts a transaction, queues several SET commands, then runs them all at once with EXEC.
Look for repeated actions inside the transaction.
- Primary operation: Each SET command queued inside MULTI.
- How many times: Number of SET commands added before EXEC.
As you add more commands, the total work grows.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 SET commands executed |
| 100 | 100 SET commands executed |
| 1000 | 1000 SET commands executed |
Pattern observation: The total execution time grows roughly in direct proportion to the number of commands.
Time Complexity: O(n)
This means the time to run EXEC grows linearly with the number of commands inside the transaction.
[X] Wrong: "EXEC runs all commands instantly no matter how many there are."
[OK] Correct: Each command still needs to be processed, so more commands mean more work and more time.
Knowing how Redis transactions scale helps you design efficient data operations and shows you understand how commands affect performance.
"What if we replaced many SET commands with a single MSET command? How would the time complexity change?"
Practice
EXEC command do in Redis transactions?Solution
Step 1: Understand Redis transaction commands
TheMULTIcommand starts queuing commands, andEXECruns them all together.Step 2: Identify the role of
EXECEXECexecutes all commands queued afterMULTIin one atomic operation.Final Answer:
It runs all queued commands in a transaction at once. -> Option AQuick Check:
EXEC runs queued commands = B [OK]
- Confusing EXEC with MULTI
- Thinking EXEC cancels commands
- Assuming EXEC checks server status
Solution
Step 1: Recall the transaction command order
First,MULTIstarts the transaction, then commands likeSETqueue, and finallyEXECruns them.Step 2: Match the correct order
MULTI, SET key value, EXEC follows this order:MULTI, thenSET, thenEXEC.Final Answer:
MULTI, SET key value, EXEC -> Option CQuick Check:
Start with MULTI, end with EXEC = A [OK]
- Putting EXEC before MULTI
- Running SET before MULTI
- Mixing command order
MULTI SET user:1:name "Alice" INCR user:1:visits EXECWhat will be the result of the
EXEC command?Solution
Step 1: Analyze queued commands
SET user:1:name "Alice"sets a string key, returns "OK";INCR user:1:visitsincrements a numeric key, returns the new value.Step 2: Understand EXEC output
EXECreturns an array with results of each command: ["OK", 1].Final Answer:
["OK", 1] -> Option BQuick Check:
EXEC returns results array = C [OK]
- Expecting QUEUED responses from EXEC
- Thinking INCR causes error on string key
- Assuming EXEC returns empty array
MULTI SET key1 "value1" GET key1 EXECWhat will be the result of the
EXEC command?Solution
Step 1: Understand command behavior in transactions
Some commands likeGETare allowed but returnQUEUEDduring MULTI, and EXEC returns their results after execution.Step 2: Identify that no error occurs
GETis fully supported inside MULTI/EXEC blocks, so EXEC succeeds and returns ["OK", "value1"].Final Answer:
No error; EXEC always succeeds if MULTI was called -> Option DQuick Check:
GET inside MULTI causes no error = D [OK]
- Assuming all commands work inside MULTI
- Forgetting to call MULTI before EXEC
- Expecting EXEC to never fail
WATCH, MULTI, and EXEC to do this safely?Solution
Step 1: Understand WATCH usage
WATCHmonitors keys for changes before starting a transaction withMULTI.Step 2: Correct command order for safe increment
FirstWATCH counter, thenMULTI, queueINCR counter, and finallyEXECto run if no changes occurred.Final Answer:
WATCH counter MULTI INCR counter EXEC -> Option AQuick Check:
WATCH before MULTI for safe transactions = D [OK]
- Calling WATCH after MULTI
- Running INCR before WATCH
- Not using WATCH for conditional updates
Ready to prove it?
Pass the test and this pattern moves unknown
