Redis / Intermediate: application correctness and coordination / Lesson 8
Pipelining, round trips and bounded work
Reduce request overhead without confusing batching with isolation. Budget command count, reply bytes and application concurrency before increasing pipeline size.
On this page
Lesson goals
- Distinguish pipelining, multi-key commands and transactions.
- Model latency assumptions and bounded resource demand.
Pipelining changes waiting, not the data contract
A client normally sends a command and waits for its reply before the next dependent step. Pipelining can send several independent commands without waiting for each intermediate reply, reducing repeated network and client overhead. It does not make a batch a Redis transaction, stop other clients from interleaving work, or give every command the same business snapshot. A script or transaction addresses different coordination requirements. Confirm the selected client API because some libraries call both transactional and nontransactional batching a pipeline, with defaults that must be inspected.
Separate logical commands from actual transport batching
The Redis sequence below reads three known string keys and asks for the same values through MGET. MGET is one multi-key command, whereas three GET commands become a pipeline only when a client actually sends them through the appropriate batching API. Merely placing lines in a document does not prove pipelining. The expected values assume a prepared synthetic fixture with strings A, B and C. In Cluster, multi-key command eligibility depends on slot rules, and managed proxy products can have different documented behavior. Do not generalize a standalone demonstration to every topology.
GET lab:batch:a
GET lab:batch:b
GET lab:batch:c
MGET lab:batch:a lab:batch:b lab:batch:c
A
B
C
[A, B, C]
Expected individual values are A, B, C, and the MGET result is [A, B, C] for a compatible topology and stated fixture. No network batching was performed.
Do not batch away a required decision point
If command two depends on the result of command one, blind pipelining cannot compute that missing result in advance. Read-modify-write logic may require a suitable atomic command, WATCH protocol or bounded server-side script. Likewise, an error in one reply does not roll back earlier pipeline effects. Inspect every reply and define partial-success recovery. A connection failure can leave some sent commands applied even though the client lacks their responses, so blindly replaying a non-idempotent batch can duplicate work. Keep event identity and uncertainty handling separate from transport optimization.
Use arithmetic to state assumptions, not benchmark claims
This offline model assumes four commands, one fixed round-trip cost and one fixed processing cost per command. It predicts how removing three serial waits can reduce the modeled total, but ignores real scheduling, buffering, TLS, payload size, errors and server contention. The integers are invented microseconds, not measured Redis performance. A real evaluation needs comparable workload, data and client behavior under an approved isolated test. Never run a load generator against a configured service merely to reproduce a theoretical speedup. The model explains why batching might help, not how large a production batch should be.
commands = 4
rtt_us = 2000
processing_us = 100
serial_us = commands * (rtt_us + processing_us)
one_batch_us = rtt_us + commands * processing_us
assert serial_us == 8400
assert one_batch_us == 2400
The declared model yields 8400 versus 2400 microseconds. These are computed hypothetical totals, not observed timings.
Bound replies and preserve backpressure
A pipeline queues commands and replies, so a huge batch can consume client and server memory and create unfair latency for other work. Bound batch count and byte size, drain replies promptly and preserve the application’s concurrency budget. Large responses or expensive commands can dominate despite fewer round trips. Choose a batch size from evidence and service objectives, not a copied universal constant. Monitor end-to-end latency, throughput, pending requests and partial failures together, and verify that client cancellation and shutdown do not silently discard response handling or leave uncertain writes unrecorded.
Common errors and fixes
A pipeline is assumed to be isolated
- Why it happens
- Transport batching was confused with a transaction boundary.
- How to fix it
- Inspect the client API and choose explicit coordination when required.
- Prevent it next time
- Test interleavings and library defaults separately.
A large batch increases tail latency
- Why it happens
- Reply bytes and queued work exceeded the intended budget.
- How to fix it
- Reduce and measure bounded batches with backpressure.
- Prevent it next time
- Track pending bytes and requests as well as command count.
A replayed batch repeats increments
- Why it happens
- Uncertain write outcomes were blindly resubmitted.
- How to fix it
- Reconcile business event identity and use documented retry semantics.
- Prevent it next time
- Include partial-send and lost-response failure tests.
Check your understanding
1. Three GET commands are written on adjacent lines. Is that evidence they used one network round trip?
Show worked solution
No. Transport behavior depends on the client’s batching implementation and when it sends and drains requests. Adjacent source or transcript lines can still be executed synchronously one at a time. Record the actual client API and compare it with a single multi-key command only under compatible topology and value semantics.
2. A pipeline writes two counters; the first succeeds and the second has a type error. Should the first be assumed undone?
Show worked solution
No. Ordinary pipelining supplies no rollback. Inspect both outcomes, preserve uncertainty and reconcile the business effect. Even MULTI/EXEC has specific runtime-error behavior rather than SQL-style rollback, so the correct design must match the actual invariant instead of changing the batching label.
Key takeaways
- Pipelining reduces waits but does not create isolation or rollback.
- Measure bounded command and reply budgets with the real client contract.