Difficulty: Advanced
How does pipelining in Redis differ from a MULTI/EXEC transaction, and when would you use each or both?
Pipelining is a client-side network optimization: the client sends many commands without waiting for each reply, cutting round trips, but other clients' commands can interleave with yours. MULTI/EXEC is a server-side guarantee: queued commands run back to back as one isolated unit, with no other client's command executing in between. They solve different problems and are often combined.
Without pipelining, 100 INCR calls over a 1 ms network link cost about 100 ms of waiting, even though Redis executes each in microseconds. With a pipeline the client writes all 100 commands into the socket at once and reads 100 replies afterwards, so the cost is closer to one round trip. Redis also does fewer read/write system calls, which raises throughput.
What pipelining does not do:
SET can land between your commands.After MULTI, Redis replies QUEUED to each command and holds them. On EXEC, it runs them sequentially and returns an array of results. Because execution is single-threaded, nothing else runs in between.
Two kinds of errors behave differently:
EXEC refuses to run the whole transaction (EXECABORT).INCR on a list, giving WRONGTYPE): only that command fails, the others still execute. There is no rollback.DISCARD abandons the queue. WATCH key adds optimistic locking: if a watched key changes before EXEC, the transaction returns nil and you retry.
| Pipeline | MULTI/EXEC | |
|---|---|---|
| Purpose | Fewer round trips | Isolation of a group of commands |
| Where it lives | Client | Server |
| Other clients interleave | Yes | No |
| Rollback on error | No | No |
| Can read a value and decide | Yes (but not atomically) | No, use WATCH or Lua |
Most clients send the transaction itself through a pipeline (MULTI, commands and EXEC in one round trip), getting both speed and isolation. For example, updating a wallet balance in paise and appending to its ledger list together is a natural MULTI block.
When you need logic based on a read, such as "decrement stock only if it is above zero", use a Lua script or a Redis Function, which run atomically on the server. In Redis Cluster, all keys in a transaction must hash to the same slot, so use hash tags like {order:991}:status and {order:991}:items.
| Pipeline | MULTI/EXEC | |
|---|---|---|
| Main goal | Cut round trips | Run commands as one unit |
| Implemented by | Client library | Redis server |
| Interleaving | Possible | Not possible |
| Rollback | None | None |
import Redis from 'ioredis';
const redis = new Redis(process.env.REDIS_URL);
// Pipeline: one round trip, NOT atomic
const results = await redis
.pipeline()
.incr('page:views:home')
.incr('page:views:pricing')
.get('page:views:home')
.exec();
// results: [[null, 101], [null, 57], [null, '101']]
// MULTI/EXEC: isolated unit, also sent in one round trip
const walletKey = '{wallet:42}:balance_paise';
const ledgerKey = '{wallet:42}:ledger';
const tx = await redis
.multi()
.decrby(walletKey, 25000) // debit Rs 250
.rpush(ledgerKey, JSON.stringify({ amount: -25000, ts: Date.now() }))
.exec();
// Optimistic locking with WATCH: retry if the balance changed
await redis.watch(walletKey);
const balance = Number(await redis.get(walletKey));
if (balance >= 25000) {
const res = await redis.multi().decrby(walletKey, 25000).exec();
if (res === null) console.log('Balance changed, retry');
} else {
await redis.unwatch();
}
The pipeline batches unrelated counters for speed. The wallet update uses MULTI so the debit and ledger entry are never separated by another client, and WATCH aborts the transaction if the balance changed after it was read.
Pipelining, MULTI/EXEC, Atomicity, WATCH, Round-Trip Time