Pipelining
Fewer round trips, and why it's the cheapest big speedup
Redis executes a simple command in about a microsecond. The network round trip to send it and get the reply takes 50-100 µs on localhost and 0.5-1 ms across a data centre. So for most workloads, latency is round trips, and the fastest way to speed up Redis code is to make fewer of them.
What pipelining does
Without pipelining, the client sends a command, waits for the reply, then sends the next:
client: SET a 1 -> (wait 0.5ms) <- OK
client: SET b 2 -> (wait 0.5ms) <- OK
client: SET c 3 -> (wait 0.5ms) <- OK
total: ~1.5msWith pipelining, the client sends all of them, then reads all the replies:
client: SET a 1 / SET b 2 / SET c 3 -> (wait 0.5ms) <- OK OK OK
total: ~0.5msThe server still runs the commands one by one, in order. Nothing changes about their semantics. They just share a round trip.
docker exec redis redis-benchmark --user default -a redis123 -t set,get -n 100000 -q
GET: 326797.41 requests per second
docker exec redis redis-benchmark --user default -a redis123 -t set,get -n 100000 -q -P 16
GET: 3448276.00 requests per second # 16 commands per round trip: ~10xIn code
pipe = r.pipeline(transaction=False) # plain pipeline, no MULTI/EXEC
for p in players:
for _ in range(random.randint(1, 5)):
pipe.zincrby(KEY, random.randint(0, 1000), p)
pipe.execute() # one batch, ~30,000 commandsIn redis-py, pipeline() defaults to transaction=True, which wraps the
batch in MULTI/EXEC. Pass transaction=False when you only want
fewer round trips and don't need the batch applied together.
Multi-key commands are the other way to save round trips: MGET/MSET,
HMGET, SADD key m1 m2 m3, variadic DEL. Prefer them over
pipelining single-key commands when they fit.
Pipelining is not atomic
Other clients' commands can run between your pipelined commands. A pipeline guarantees order and saves round trips; it doesn't give you isolation. For that, see Transactions and Lua Scripting.
Batch size
Bigger isn't always better:
- Redis buffers all the replies for a pipeline in that client's output
buffer. A pipeline of a million
GETs on 1 KB values builds a 1 GB reply in Redis's memory. - The client also has to hold all the replies.
- While Redis is executing your huge batch, it isn't fair to other clients. Each command is fast, but many thousands in a row add up.
Batches of 100-1,000 commands get nearly all of the benefit. Split anything larger.
The commonest real-world Redis performance bug is an N+1 pattern:
looping over 200 IDs and doing an HGETALL for each one, sequentially.
That's 200 round trips, ~100 ms across a network, even though Redis
did ~0.2 ms of work. Pipeline it and the request takes about 1 ms.
Lab
- Run both
redis-benchmarkcommands above and compare. - In Python, time 10,000
SETs in a loop vs in one pipeline vs in pipelines of 500. (Run the script inside thelabscontainer so the network is the Docker network, not the macOS VM boundary.) - Watch
MONITORduring a pipelined run. The commands still appear one by one, because the server still executes them that way.