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