0
0
Redisquery~5 mins

Transaction execution model in Redis

Choose your learning style9 modes available
Introduction

Transactions let you run multiple commands together as one group. This helps keep your data safe and consistent.

When you want to update several keys at once without interruption.
When you need to make sure all commands succeed or none do.
When you want to avoid other clients changing data while you work.
When you want to group commands to run in order without interference.
Syntax
Redis
MULTI
COMMAND1
COMMAND2
...
EXEC

MULTI starts the transaction.

EXEC runs all queued commands together.

Examples
This sets two keys together as one transaction.
Redis
MULTI
SET key1 value1
SET key2 value2
EXEC
This increments a counter twice in one transaction.
Redis
MULTI
INCR counter
INCR counter
EXEC
Sample Program

This transaction sets two fields for user 1 together.

Redis
MULTI
SET user:1:name Alice
SET user:1:age 30
EXEC
OutputSuccess
Important Notes

Commands inside MULTI are queued and only run when EXEC is called.

If any command fails, EXEC still runs all commands; Redis does not rollback.

Use WATCH before MULTI to monitor keys and abort if they change.

Summary

Transactions group commands to run together safely.

Use MULTI to start and EXEC to run the commands.

Redis transactions do not rollback on errors automatically.