Sending multiple commands in pipeline in Redis - Time & Space Complexity
When sending many commands to Redis using a pipeline, we want to know how the time to process them grows as we add more commands.
We ask: How does the total work increase when we send more commands together?
Analyze the time complexity of the following code snippet.
MULTI
SET key1 value1
SET key2 value2
SET key3 value3
EXEC
This code sends multiple SET commands inside a transaction pipeline to Redis, executing them all at once.
Look for repeated actions in the code.
- Primary operation: Sending each SET command to Redis.
- How many times: Once for each key-value pair inside the transaction.
As you add more commands, the total work grows roughly by the number of commands.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 commands sent |
| 100 | About 100 commands sent |
| 1000 | About 1000 commands sent |
Pattern observation: The work grows directly with the number of commands you send.
Time Complexity: O(n)
This means if you double the number of commands, the total time roughly doubles too.
[X] Wrong: "Sending commands in a pipeline makes the time stay the same no matter how many commands I send."
[OK] Correct: Even though pipelining reduces network delays, Redis still processes each command one by one, so total time grows with the number of commands.
Understanding how pipelining affects time helps you explain how to handle many commands efficiently in real projects.
What if we sent commands one by one without pipelining? How would the time complexity change?