Mỗi Redis command thông thường = 1 round-trip: client gửi request, đợi response, rồi gửi request tiếp theo.
- Với latency 1ms/request, 1000 commands = 1 giây. Pipelining gom nhiều commands vào một network call, gửi cùng lúc, nhận responses về cùng lúc — giảm round-trips xuống còn 1 (hoặc vài lần batch).
Ví dụ:
python
pipe = redis.pipeline()
for i in range(1000):
pipe.set(f'key:{i}', i)
results = pipe.execute() # 1 round-trip thay vì 1000Pipelining không đảm bảo atomicity (khác với MULTI/EXEC).
- Commands trong pipeline có thể bị xen kẽ với commands từ client khác. Khi nào hiệu quả: bulk insert/update, batch GET/SET, warming cache — bất kỳ lúc nào có nhiều commands độc lập nhau. Khi không giúp: một command phụ thuộc kết quả của command trước — phải dùng Lua script hoặc MULTI/EXEC.
- Khi network latency cao (>1ms như cross-datacenter), pipelining có thể tăng throughput 10-100x.
Each standard Redis command requires 1 round-trip: the client sends a request, waits for the response, then sends the next.
- At 1ms per request, 1,000 commands = 1 second. Pipelining batches multiple commands into a single network call, sends them all at once, and receives all responses at once — reducing round-trips to 1 (or a few batches).
- Example:
python
pipe = redis.pipeline()
for i in range(1000):
pipe.set(f'key:{i}', i)
results = pipe.execute() # 1 round-trip instead of 1000Pipelining does not guarantee atomicity (unlike MULTI/EXEC).
- Commands in a pipeline can be interleaved with commands from other clients. When it is effective: bulk inserts/updates, batch GET/SET, cache warming — any time you have many independent commands. When it does not help: when one command depends on the result of a previous one — use a Lua script or MULTI/EXEC instead.
- With high network latency (> 1ms, e.g., cross-datacenter), pipelining can improve throughput by 10-100x.