Redis › Transactions

What is a Redis transaction, and how do the MULTI and EXEC commands work together to create one?

Beginner

Answer

A Redis transaction lets you group a series of commands together so they are executed sequentially and atomically as a single unit, starting the group with MULTI, queuing each command that follows, and finally running them all at once with EXEC, ensuring no other client's commands can be interleaved in the middle of your sequence.
MULTI
SET account:1:balance 500
DECRBY account:1:balance 100
EXEC
Real-world example A banking application uses a transaction to update two related account balances together, guaranteeing that both updates happen as one uninterrupted operation from the perspective of every other client.

Common follow-ups: What happens if one command inside a transaction fails while it is being queued?;Can you cancel a transaction before calling EXEC?