Difficulty: Intermediate
What is Redis pipelining, why does it improve performance, and how is it different from a transaction?
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.
GET executes in microseconds, but a round trip between an app in one availability zone and Redis in another can be around a millisecond.GETs at 1 ms RTT take about 10 seconds; pipelined in batches they take a fraction of a second.read() and writes many replies with one write(), which raises throughput even on localhost.| Technique | Fewer round trips | Atomic / isolated | Can branch on results |
|---|---|---|---|
| Pipeline | Yes | No | No |
MULTI/EXEC | Yes (when pipelined) | Yes | No |
| Lua script / Function | Yes | Yes | Yes |
MGET / MSET | Yes | Yes | No |
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.
MULTI.enableAutoPipelining option, so Promise.all over many commands already benefits.redis-cli --pipe streams raw protocol for maximum speed.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 | Pipelined | |
|---|---|---|
| Round trips for N commands | N | About 1 per batch |
| Client waits between commands | Yes | No |
| Atomic | No | No |
| Server memory for replies | One reply at a time | Buffered until read |
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.
// 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.
Pipelining, Round-Trip Time, Batching, Auto-pipelining, MULTI vs Pipeline