Redis is a popular in-memory data store, often used as a database, cache, or message broker. One powerful feature of Redis is its support for transactions, which let you execute multiple commands as a single atomic operation. This means either all the commands in the transaction are executed, or none of them are — no in-between states.
What Is a Redis Transaction?
A transaction groups multiple Redis commands so they can be executed sequentially and atomically. In other words, Redis ensures the commands inside a transaction run all together without any other commands interleaving.
This is particularly useful when you want to:
- Modify multiple keys as part of a consistent update
- Ensure no other clients interfere while your commands run
- Keep data integrity in concurrent environments
Unlike traditional relational databases, Redis transactions don’t support rollbacks on error within the transaction. If an error occurs during the transaction commands, Redis will continue executing the remaining commands. So, it’s important to use Redis transactions carefully.
Core Redis Commands for Transactions
Redis transactions revolve around four main commands:
1. MULTI
- Starts a transaction block.
- After you call MULTI, all subsequent commands are queued instead of executed immediately.
Example:
MULTI
SET key1 "value1"
INCR counter
EXEC
Here, SET key1
and INCR counter
are queued and will run only when you call EXEC
.
2. EXEC
- Executes all the commands queued after
MULTI
. - All queued commands run atomically, in order.
- Returns an array of results corresponding to each command.
3. DISCARD
- Aborts the transaction and clears the queue.
- None of the queued commands run.
- Use DISCARD when you decide not to proceed with the transaction.
4. WATCH
- Monitors one or more keys for changes before starting a transaction.
- If any watched key changes before you execute the transaction, the transaction is aborted.
- This allows you to implement optimistic locking.
Example:
WATCH key1
MULTI
INCR key1
EXEC
If key1
is modified by another client between WATCH
and EXEC
, the EXEC
will fail and return null
, indicating the transaction was aborted.
How Redis Transactions Work in Practice
Here’s a typical flow for using transactions with optimistic locking:
- Use
WATCH
to monitor keys you want to ensure remain unchanged. - Use
MULTI
to start the transaction. - Queue your commands.
- Call
EXEC
to attempt to apply all commands atomically. - If
EXEC
returns null, retry the transaction.
Example: Bank Account Transfer
Imagine you want to transfer money between two accounts atomically.
WATCH account:1 account:2
MULTI
DECRBY account:1 100
INCRBY account:2 100
EXEC
- If either account’s balance changes before
EXEC
, the transaction aborts. - Otherwise, the transfer happens safely.
Wrapping up
Redis transactions provide a simple way to group multiple commands for atomic execution using MULTI
, EXEC
, DISCARD
, and WATCH
. While they lack some features of traditional database transactions, they are essential tools for maintaining data consistency and handling concurrent modifications in Redis.
With a bit of practice, you can use Redis transactions to build more reliable and consistent applications.
If you're a software developer who enjoys exploring different technologies and techniques like this one, check out LiveAPI. It’s a super-convenient tool that lets you generate interactive API docs instantly.
LiveAPI helps you discover, understand and use APIs in large tech infrastructures with ease!
So, if you’re working with a codebase that lacks documentation, just use LiveAPI to generate it and save time!
You can instantly try it out here! 🚀
Top comments (0)
Some comments may only be visible to logged-in visitors. Sign in to view all comments.