Redis allows you to use many commands to create and administrate your database.
There is especially one useful feature which which allows you to execute a series of commands as a single unit of work:
The Redis transactions!
In this post will check how it works how it can be useful for your daily work.
How do transactions work?
Transactions allows to send a block of commands, using the the MULTI command.
All the commands will be queued the when using the EXEC command it will be executed.
A request sent by another client will never be served in the middle of the execution of a Redis Transaction. “This guarantees that the commands are executed as a single isolated operation“
Here is a quick example :
In a row, we created a key “PLAYERS” with the player name and their associated number, then we wanted it to be displayed using the command.
The EXEC command will commit all these actions.
When using the MULTI command, the inputs are not executed until the EXEC command is done.
What are the benefits of Redis transactions?
First one is that transactions are atomic. In other words all of the commands in the transaction are completed or none of them are.
This is an essential point for ensuring the consistency of your data, so that you don’t end up in a situation where certain commands in the transaction have been executed, but not others.
Other good point, transactions in Redis are very fast.
Remember Redis works as an in-memory database, it’s a reason it can execute transactions very quickly.
This makes it ideal for use cases where you need to make multiple changes to your data in a short period of time.
Are rollbacks possible?
Unfortunately not, but there is a good reason:
Supporting rollbacks would have a significant impact on the simplicity and performance of Redis.
How to discard a transaction?
You can use the DISCARD command to abort your transaction.
After that all it will not execute the commands within the transaction and your data will be restored to normal.
Here is an example:
- As you can see, the NUMBERS value remained the same 123456789
Watch main function is to monitor keys, this command will make the
This includes modifications made by the client, like write commands, and by Redis itself, like expiration or eviction.
Below an example of watch command:
- 1st user watch the key then start the transaction by updating the key value:
- In the mean time, the second user modifies the key with another value:
- When the 1st user issues the EXEC command, the result is an error:
- When we see the value of NUMBERS the modification was done by the 2nd user:
- You see now how the WATCH command prevents the EXEC from running
What about Optimistic locking?
Redis is able to use a check and set method to lock the Redis transaction.
For that we will use the WATCH command.
I invite you to check the Redis site to see some use cases on how optimistic locking is working
Another solution is to use Redis scripts which are also transactional.
That means that you can do the same actions as with a Redis Transaction,and usually the script will be both simpler and faster.
You can have more info about scripts by following this link
Now you know how to perform transactions in Redis, a useful feature to keep data consistency and in a very fast way 🙂