Skip to main content

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 the multi() 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:
For dynamic transactions, you can specify types manually:

Error Handling

Default Behavior

By default, if any command fails, the entire transaction throws an error:

Keep Errors Mode

To handle errors individually, use the keepErrors 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

Transactions guarantee atomicity but not isolation during execution. Other clients’ commands can interleave with individual commands within the transaction. For conditional execution based on watched keys, use Redis WATCH/MULTI/EXEC pattern (not directly supported via REST API).

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-exec endpoint
  • 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