Overview
Transactions in Upstash Redis allow you to execute multiple commands atomically. All commands in a transaction are serialized and executed sequentially, guaranteeing that they run as a single isolated operation.The
multi() method creates a transaction pipeline. Unlike regular pipelines, transactions guarantee atomicity - either all commands succeed or none do.Creating a Transaction
Use themulti() method to create a transaction:
Basic Usage
Chaining Commands
You can chain commands together and execute them atomically:Building Transactions Dynamically
You can build transactions incrementally before executing:Type Safety
When chaining commands, TypeScript can infer return types:Error Handling
Default Behavior
By default, if any command fails, the entire transaction throws an error:Keep Errors Mode
To handle errors individually, use thekeepErrors option:
Practical Examples
Atomic Counter Update
Batch User Creation
Transfer Between Accounts
Complex Data Updates
Transaction Length
You can check how many commands are in a transaction before executing:Important Notes
Differences from Redis MULTI/EXEC
- Commands are executed via HTTP REST API, not native Redis protocol
- All commands are sent in a single HTTP request to the
/multi-execendpoint - WATCH/DISCARD commands are not supported
- Commands execute atomically on the server side
See Also
- Pipelines - Send multiple commands without atomicity guarantees
- Error Handling - Handle errors in your Redis operations