Optimizing with Pipelining

1. Understanding Pipeline Concept

ConceptDetail
BatchingSend many commands without waiting per-reply
GoalEliminate RTT cost for bulk ops
AtomicityNOT atomic — use MULTI for atomicity

2. Implementing Pipeline in Clients

Example: Pipeline in Jedis

try (Pipeline p = jedis.pipelined()) {
    for (int i = 0; i < 1000; i++) p.set("k:" + i, "v");
    List<Object> results = p.syncAndReturnAll();
}

3. Batching Multiple Commands

Batch SizeThroughput Impact
1Baseline
100~30-50× faster
1000~50-100× faster (LAN)

4. Handling Pipeline Responses

APIBehavior
pipe.sync()Flush and discard replies
pipe.syncAndReturnAll()Flush and collect ordered replies
Response<T>Per-command future

5. Comparing Pipelining vs Transactions

Pipeline

  • Performance optimization
  • Not atomic
  • Other clients interleave
  • Any commands allowed

Transaction (MULTI/EXEC)

  • Atomic execution
  • Server queues all commands
  • WATCH for CAS
  • No interleaving

6. Understanding Round-Trip Time Savings

RTT10K ops sequential10K ops pipelined (batch 100)
0.5 ms5 s~50 ms
10 ms (WAN)100 s~1 s

7. Measuring Performance Gains

redis-benchmark -t set -n 100000 -P 100   # pipelined
redis-benchmark -t set -n 100000          # no pipeline
FlagEffect
-P nPipeline batch size

8. Using Pipeline with Lua Scripts

PatternBenefit
Pipeline of EVALSHACombines atomic op + batching

9. Managing Pipeline Buffer Size

Warning: Huge pipelines can exhaust client/server memory. Keep batches under 10K commands.
SettingPurpose
client-output-buffer-limit normalServer-side buffer cap per client
Client send bufferFlush in chunks to bound memory

10. Avoiding Pipeline Pitfalls

PitfallAvoid By
Cross-slot pipelines in ClusterSplit by slot, use hashtags {tag}
Mixing blocking opsDon't pipeline BLPOP/BRPOP
Out-of-order assumptionReplies match command order