A database transaction refers to a single unit of work comprised of single to multiple queries. In cases, an operation can only be classified as a transaction if there are any changes to the database.
We will not concern ourselves with that, but we will learn how to use the WATCH command alongside Redis transactions for this one.
What are Transactions?
In Redis, transactions are made up of four main commands: WATCH, EXEC, DISCARD, and MULTI.
Using the above commands, you can open a block and add multiple commands all at once. Once completed, you run the commands as one single unit.
For a transaction to be successful, Redis ensures that:
- All the commands specified in a transaction unit are executed consecutively. Hence, the first-come, first run.
- All the commands in a transaction unit MUST execute successfully. If one of the commands in the unit fails, the entire transaction block fails as well. This feature is known as atomic command execution
- Third, commands in a transaction are serialized. Hence, a client cannot be a server while a transaction unit runs.
Redis Create Transaction
You create a transaction unit using the MULTI command. The MULTI command will return ok. You can go ahead and add all the transaction commands one after the other.
Instead of executing the commands, Redis will queue them in insertion until you call them.
An example is as shown below:
Ok
Redis Execute Transaction Unit
As mentioned, Redis will queue the commands in a transaction unit until you manually execute them.
We can do this using the EXEC command. This tells Redis to run all the queued commands in the insert order.
An example usage is as shown below:
QUEUED
127.0.0.1:6379> INCR newkey
QUEUED
127.0.0.1:6379> GET newkey
QUEUED
127.0.0.1:6379>
You will notice that each command executed is queued. A queued command is a command that is scheduled to run after exec is called.
To run it, call EXEC as:
1) ok
2) (integer) 101
3) "101"
This should execute all the commands and returns the resulting values.
Redis Remove Command Queue
Suppose you want to clean your command queue and flush all the scheduled commands? For that, you can use the DISCARD command as shown:
Ok
127.0.0.1:6379> SET newkey "100"
QUEUED
127.0.0.1:6379> INCR newkey
QUEUED
127.0.0.1:6379> GET newkey
QUEUED
127.0.0.1:6379> DISCARD
Ok
After running the DISCARD command, Redis will return Ok and close the transaction unit.
Redis Watch Command
The watch command in Redis allows you to implement the check-and-set feature. The WATCH commands accept Redis keys as parameters and monitor them.
If any of the specified keys are changed before the EXEC command is called, Redis automatically terminates the transaction and returns a Null reply.
Take the example below:
var = var + 1
SET mykey $var
In the example above, we have an operation that increases the value of a key by 1. Of course, this is not an issue if only a single client performs the said operation.
However, if multiple clients attempt to execute the above operation simultaneously, a race condition occurs and returns an invalid value.
We can solve this by watching the key as shown:
var = GET mykey
var = var + 1
MULTI
SET mykey $var
EXEC
Here, if a client operates and the value of the key is changed before executing the transaction, the execution fails.
To remove all watched keys, use the UNWATCH command.
Conclusion
This article discussed using and working with transactions in a Redis database. Check the documentation to discover more.
Thanks for reading!!