Pipelining

Difficulty: Intermediate

Question

What is Redis pipelining, why does it improve performance, and how is it different from a transaction?

Answer

Pipelining means sending many commands to Redis without waiting for each reply, then reading all the replies together. Redis is usually so fast that the network round-trip time (RTT) dominates, so sending 1,000 commands in one pipeline costs roughly one RTT instead of 1,000. A pipeline is purely a network optimisation: it gives no atomicity, and other clients' commands can run between yours.

Why round trips dominate

  • A GET executes in microseconds, but a round trip between an app in one availability zone and Redis in another can be around a millisecond.
  • 10,000 sequential GETs at 1 ms RTT take about 10 seconds; pipelined in batches they take a fraction of a second.
  • Pipelining also reduces system calls: Redis reads many commands with one read() and writes many replies with one write(), which raises throughput even on localhost.

Pipeline vs MULTI/EXEC vs Lua vs multi-key commands

TechniqueFewer round tripsAtomic / isolatedCan branch on results
PipelineYesNoNo
MULTI/EXECYes (when pipelined)YesNo
Lua script / FunctionYesYesYes
MGET / MSETYesYesNo

Use MGET/MSET when one command does the job; use a pipeline when you need many different commands; use MULTI or Lua when they must not interleave with other clients.

Practical guidance

  • Batch size: Redis buffers all replies in memory until the client reads them. Send in chunks (for example 1,000 to 10,000 commands) rather than one million at once.
  • Cluster: commands in a pipeline may target different slots. Cluster-aware clients like ioredis and redis-py split the pipeline per node, so cross-slot pipelines work, unlike cross-slot MULTI.
  • Auto-pipelining: node-redis (v4+) automatically pipelines commands issued in the same event-loop tick, and ioredis has an enableAutoPipelining option, so Promise.all over many commands already benefits.
  • Errors: one failing command does not stop the rest; check every reply.
  • Bulk loading: for very large imports, redis-cli --pipe streams raw protocol for maximum speed.

Real example

Loading a user's dashboard might need their profile hash, cart count, 10 recent notifications and wishlist size. Pipelining those four commands turns four RTTs into one, which matters when a page makes dozens of Redis calls per request.

Sequential calls vs a pipeline. The commands are identical; only the waiting pattern changes.
SequentialPipelined
Round trips for N commandsNAbout 1 per batch
Client waits between commandsYesNo
AtomicNoNo
Server memory for repliesOne reply at a timeBuffered until read

Code examples

Pipelining in Python (redis-py)

import redis
r = redis.Redis()

user_id = 42
# transaction=False: plain pipeline, no MULTI/EXEC wrapper
pipe = r.pipeline(transaction=False)
pipe.hgetall(f"user:{user_id}")
pipe.hget(f"cart:{user_id}", "items")
pipe.lrange(f"notifications:{user_id}", 0, 9)
pipe.scard(f"wishlist:{user_id}")
profile, cart_items, notifs, wishlist_size = pipe.execute()  # one round trip

# Bulk write in chunks to keep server buffers small
with r.pipeline(transaction=False) as p:
    for i in range(100_000):
        p.set(f"warmup:{i}", i, ex=3600)
        if i % 5_000 == 0:
            p.execute()
    p.execute()

redis-py pipelines wrap commands in MULTI/EXEC by default; transaction=False sends a plain pipeline. Chunking large batches avoids huge reply buffers.

Auto-pipelining in Node.js

// node-redis v4+ pipelines commands issued in the same tick
const ids = [101, 102, 103, 104];
const products = await Promise.all(
  ids.map((id) => redis.get(`product:${id}`))
);

// Explicit batch without MULTI
const [views, likes] = await redis
  .multi()
  .incr('post:7:views')
  .incr('post:7:likes')
  .execAsPipeline();

Promise.all over independent commands already shares one round trip, and execAsPipeline sends a batch without wrapping it in a transaction.

Key points

  • Pipelining saves round trips, not server CPU per command.
  • A pipeline is not atomic; use MULTI/EXEC or Lua for isolation.
  • Send large batches in chunks so Redis does not buffer huge replies.
  • Cluster clients split pipelines by node, so cross-slot pipelines work.
  • node-redis and ioredis can auto-pipeline concurrent commands.

Common follow-up questions

  • How is pipelining different from MULTI/EXEC?
  • What happens if one command in a pipeline fails?
  • How would you bulk load 50 million keys into Redis?
  • Does pipelining help when Redis runs on the same machine as the app?

Interview tips

  • Quantify with RTT: N commands at 1 ms each vs one round trip.
  • Explicitly say pipelining is not atomic; mixing it up with MULTI is a common red flag.
  • Mention chunking and auto-pipelining to show practical experience.

Concepts covered

Pipelining, Round-Trip Time, Batching, Auto-pipelining, MULTI vs Pipeline

More Redis & Caching questions