Transactions in Redis
Learn about transactions and how they help to achieve atomicity within Redis.
We’re all surrounded by transactions. Every time we buy something at a store, transfer money online, or even send a text message, we’re participating in a transaction. Transactions are an essential tool for maintaining the consistency and integrity of data in complex software systems. Without them, it would be much more difficult to ensure that data is always correct and up to date, especially in situations where multiple clients may be accessing the same data at the same time. With the increasing prevalence of distributed systems and cloud computing, transactions have become even more important. They provide a way to coordinate updates to data across multiple nodes in a system, ensuring that all nodes see a consistent view of the data at all times. Whether we realize it or not, transactions play a critical role in many of the software systems we use every day. By understanding how transactions work and how to use them effectively, we can build more reliable, scalable, and consistent software systems.
What is a transaction and atomicity?
A transaction, in simple words, is an action to execute multiple instructions in one go. If any of the instructions in a transaction fails, then all the other instructions that were executed earlier need to
Check whether person A has $100 in their bank account.
If yes, then deduct $100 from person A's bank account. If not, show an error and stop the transaction.
After deduction from person A's account, execute all the business logic to verify that the transaction is valid and person B can receive the money, and finally add $100 to person B's bank account.
Now, assume that something breaks after deduction and person B never receives the money. In such scenarios, it’s expected that the transaction should roll back, and all its changes should be reverted. In our case, the $100 should be credited back to person A's account. This happens due to transactions.
Similarly, in Redis as well, we can create a set of instructions to be executed in one go, and if any one of the instructions fails, then revert the changes done by that transaction. The process of executing the instructions in such a way that they will be executed as a single, indivisible unit, and each transaction either fully completes or doesn’t complete at all (reverting all the changes), is known as atomicity.
Create transactions in Redis
To create a transaction in Redis, we have two commands that can be used:
MULTI
: This lets Redis know that a transaction has been started and any commands executed from now onwards are part of a single transaction. The command doesn’t accept any parameters and returns anOK
message denoting that a transaction block has been started. After executing this command, whatever commands are written on the console are queued to the transaction block and not executed immediately.EXEC
: This lets Redis know that the instructions in the transaction can now be executed in one go and no more instructions are to be added to this single transaction. This command actually executes the commands that are given as part of the transaction. TheEXEC
command doesn’t accept any parameters, and once this command is executed, all the commands that were queued to the transaction block are executed atomically (i.e., either all will be executed or none will be executed at all).
Exercise: Implement a successful transaction
Let’s perform an exercise to implement a successful transaction. We’ll create a transaction with the following commands/instructions:
We execute the command
FLUSHALL
to first empty the Redis data store and then execute the commandMULTI
to begin a transaction block.We add some data to the Redis data store by executing the command
MSET counter 100 price 250 title "Redis transactions"
. Observe that the output is notOK
, which is usually returned by theSET
command. Instead, we get aqueued
message that denotes that the commands are queued to be executed later on.We execute some more commands to be queued in the transaction block:
INCRBY counter 5
,DECR counter
,INCR counter
.Now we start executing the transaction atomically using the command
EXEC
. Observe that the output of this command is a list of outputs of all the individual commands in the transaction block.
Get hands-on with 1400+ tech skills courses.