Use pipelining for speed, MULTI/EXEC to stop other clients running commands in the middle, and Lua when you need to read, decide and write in one atomic step.
Pipelining sends many commands without waiting for each reply. Then it reads all the replies together. This cuts round trips, which are often the biggest cost. It gives no atomicity: other clients' commands can run in between yours.
MULTI/EXEC queues commands and runs them as one block. No other client can run a command in the middle of it. It has two limits:
- You can't use a value read inside the transaction to decide what to do next. Commands only queue until
EXEC, so no results come back before then. - It does not roll back if one command fails while running.
Add WATCH for optimistic check-and-set (see the WATCH question).
Lua scripts (EVAL, or Redis 7 functions with FCALL) run atomically on the server. They can read, branch and write in one step. Use them for conditional logic, like releasing a lock or rate limiting. Keep them short, because a script blocks the whole server while it runs.
You can combine pipelines and transactions. Clients usually send a whole MULTI block in one pipeline.
- Pipelining means fewer round trips, but it is not atomic.
- MULTI/EXEC stops interleaving, but has no rollback and no reading mid-transaction.
- WATCH adds optimistic locking to MULTI/EXEC.
- Lua scripts and functions are atomic and can use conditional logic.
- A long script blocks every other client.