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?"