0
0
Redisquery~5 mins

Sending multiple commands in pipeline in Redis - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: Sending multiple commands in pipeline
O(n)
Understanding Time 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?

Scenario Under Consideration

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.

Identify Repeating Operations

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.
How Execution Grows With Input

As you add more commands, the total work grows roughly by the number of commands.

Input Size (n)Approx. Operations
10About 10 commands sent
100About 100 commands sent
1000About 1000 commands sent

Pattern observation: The work grows directly with the number of commands you send.

Final Time Complexity

Time Complexity: O(n)

This means if you double the number of commands, the total time roughly doubles too.

Common Mistake

[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.

Interview Connect

Understanding how pipelining affects time helps you explain how to handle many commands efficiently in real projects.

Self-Check

What if we sent commands one by one without pipelining? How would the time complexity change?