Bird
Raised Fist0
Redisquery~5 mins

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

Choose your learning style10 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?

Practice

(1/5)
1. What is the main benefit of sending multiple commands in a Redis pipeline?
easy
A. It compresses the data before sending.
B. It reduces network round trips and speeds up command execution.
C. It automatically backs up the data.
D. It encrypts the commands for security.

Solution

  1. Step 1: Understand what pipelining does

    Pipelining sends many commands at once without waiting for each reply, reducing network delays.
  2. Step 2: Identify the main benefit

    This reduces the number of network round trips, making command execution faster.
  3. Final Answer:

    It reduces network round trips and speeds up command execution. -> Option B
  4. Quick Check:

    Pipelining = faster execution [OK]
Hint: Pipelining batches commands to save network time [OK]
Common Mistakes:
  • Thinking pipelining encrypts commands
  • Assuming pipelining compresses data
  • Believing pipelining backs up data automatically
2. Which of the following is the correct way to start a Redis transaction pipeline?
easy
A. MULTI
B. PIPELINE
C. EXEC
D. BEGIN

Solution

  1. Step 1: Recall Redis transaction commands

    Redis transactions start with the command MULTI to begin queuing commands.
  2. Step 2: Confirm the correct command

    EXEC is used to execute queued commands, not to start the pipeline.
  3. Final Answer:

    MULTI -> Option A
  4. Quick Check:

    Start pipeline with MULTI [OK]
Hint: Start with MULTI, end with EXEC [OK]
Common Mistakes:
  • Using EXEC to start instead of end
  • Using PIPELINE which is not a Redis command
  • Using BEGIN which is not valid in Redis
3. Consider the following Redis pipeline commands:
MULTI
SET key1 value1
SET key2 value2
EXEC
What will be the output of the EXEC command?
medium
A. [1, 1]
B. ["value1", "value2"]
C. Error: EXEC without MULTI
D. ["OK", "OK"]

Solution

  1. Step 1: Understand MULTI/EXEC behavior

    Commands between MULTI and EXEC are queued and executed atomically when EXEC runs.
  2. Step 2: Analyze the commands SET key1 value1 and SET key2 value2

    Each SET command returns "OK" on success.
  3. Final Answer:

    ["OK", "OK"] -> Option D
  4. Quick Check:

    SET commands return OK in EXEC output [OK]
Hint: SET returns OK, EXEC returns array of results [OK]
Common Mistakes:
  • Expecting EXEC to return values set
  • Confusing EXEC output with command errors
  • Thinking EXEC returns numeric success codes
4. You wrote this Redis pipeline code:
MULTI
SET key1 value1
GET key1
EXEC
But the GET command returns nil instead of 'value1'. What is the likely cause?
medium
A. GET command is not supported inside MULTI/EXEC.
B. You forgot to call DISCARD after MULTI.
C. GET command runs before SET in the pipeline, so key1 is not set yet.
D. The key1 was deleted before EXEC.

Solution

  1. Step 1: Understand command queuing in MULTI/EXEC

    Commands are queued and executed atomically, but GET inside MULTI returns nil because it reads before SET is applied.
  2. Step 2: Explain why GET returns nil

    GET does not see the queued SET command's effect until after EXEC finishes.
  3. Final Answer:

    GET command runs before SET in the pipeline, so key1 is not set yet. -> Option C
  4. Quick Check:

    GET inside MULTI sees old data [OK]
Hint: GET inside MULTI reads old data, use EXEC results [OK]
Common Mistakes:
  • Assuming GET works immediately inside MULTI
  • Thinking DISCARD is needed here
  • Believing GET is unsupported in transactions
5. You want to increment two counters atomically using Redis pipeline. Which sequence correctly uses MULTI/EXEC to do this?
hard
A.
MULTI
INCR counter1
INCR counter2
EXEC
B.
INCR counter1
INCR counter2
C.
MULTI
INCR counter1
EXEC
INCR counter2
EXEC
D.
PIPELINE
INCR counter1
INCR counter2
EXEC

Solution

  1. Step 1: Identify atomic increment commands

    INCR commands increase counters by 1; to run atomically, they must be inside MULTI/EXEC.
  2. Step 2: Check each option for correct pipeline usage

    MULTI
    INCR counter1
    INCR counter2
    EXEC
    queues both INCR commands inside MULTI and executes them together with EXEC.
  3. Step 3: Explain why others are incorrect

    INCR counter1
    INCR counter2
    runs commands separately, not atomically.
    MULTI
    INCR counter1
    EXEC
    INCR counter2
    EXEC
    splits increments into two transactions.
    PIPELINE
    INCR counter1
    INCR counter2
    EXEC
    uses invalid PIPELINE command.
  4. Final Answer:

    MULTI INCR counter1 INCR counter2 EXEC -> Option A
  5. Quick Check:

    Atomic increments need MULTI/EXEC [OK]
Hint: Use MULTI before commands, EXEC after to run atomically [OK]
Common Mistakes:
  • Running commands outside MULTI/EXEC for atomicity
  • Using invalid PIPELINE command
  • Splitting increments into separate transactions
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard