Pipeline vs. MULTI

Difficulty: Advanced

Question

How does pipelining in Redis differ from a MULTI/EXEC transaction, and when would you use each or both?

Answer

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.

What pipelining actually does

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:

  • It is not atomic. If command 50 fails, commands 1 to 49 have already run and 51 to 100 still run.
  • It is not isolated. Another client's SET can land between your commands.
  • It is not a server feature at all; the server just processes commands as they arrive.

What MULTI/EXEC guarantees

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:

  1. Errors at queue time (wrong number of arguments, unknown command): EXEC refuses to run the whole transaction (EXECABORT).
  2. Errors at run time (for example 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.

Comparison

PipelineMULTI/EXEC
PurposeFewer round tripsIsolation of a group of commands
Where it livesClientServer
Other clients interleaveYesNo
Rollback on errorNoNo
Can read a value and decideYes (but not atomically)No, use WATCH or Lua

Combining them and the alternatives

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 vs MULTI/EXEC at a glance. Pipelines save network time; transactions give isolation.
PipelineMULTI/EXEC
Main goalCut round tripsRun commands as one unit
Implemented byClient libraryRedis server
InterleavingPossibleNot possible
RollbackNoneNone

Code examples

Pipeline vs transaction with ioredis

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.

Key points

  • Pipelining reduces round trips and syscalls; it gives no atomicity or isolation.
  • MULTI/EXEC runs queued commands with no interleaving, but has no rollback.
  • Queue-time errors abort the whole EXEC; runtime errors fail only that command.
  • WATCH gives optimistic locking; EXEC returns nil if a watched key changed.
  • In Cluster, all keys in a transaction must be in one slot; use hash tags.

Common follow-up questions

  • Why does Redis not support rollback in transactions?
  • How would you atomically decrement stock only if it is above zero?
  • What happens to a pipeline if the connection drops halfway through?
  • How do pipelines work in Redis Cluster when keys are on different nodes?

Interview tips

  • Lead with the one-line distinction: pipeline is about network, MULTI is about isolation.
  • Say clearly that Redis transactions do not roll back; many candidates assume they do.
  • Mention Lua or Functions for read-then-write logic to show you know the limits of MULTI.

Concepts covered

Pipelining, MULTI/EXEC, Atomicity, WATCH, Round-Trip Time

More Redis & Caching questions