Updated February 17, 2023
Introduction to Redis WATCH
Redis watch works while executing the exec command. In other words, redis only performs the transaction if the keys that were being watched are not modified at the same time the transaction is not executing. We can invoke the watch command multiple times, and a single watch can involve multiple keys. Watch is simply used to monitor changes that began at the point where watch was called, and exec is also involved.
Key Takeaways
- The redis watch command will allow us to implement the set and check features. The watch command accepts redis keys as input.
- After execution of watch command, it will return ok as an output. It will monitor the redis key after accepting it with the watch command.
What is Redis WATCH?
Redis transaction allows the execution of commands in one step and this is centered around the command of exec, multi, and watch. All the redis commands in the transaction are executed and serialized sequentially. The request sent by another client is not being saved in the middle of the redis transaction’s execution. This ensures that the command does not execute into the isolation operation.
In redis, we are calling watch several times. Simply put, we must call the watch in order to effect the changes that were initiated by the call. We can send a large number of keys in a single watch call by using a redis watch. When exec is called, all keys are unwatched.
What are Transactions?
The redis transaction allows multiple commands to be executed in a single set. The execution will be triggered by the exec command in the redis transaction. If our client loses connection to the redis server before executing the exec command, no operations will be performed. Using the multi command, we enter the redis transaction. The multi command returns ok. We can execute multiple commands by using multi commands.
When executing multiple commands, Redis does not execute a single command at a time; instead, it executes all commands after executing the exec command. In the below example, we can see how a transaction works by using redis watch. In the below example, we can see that we have started the transaction by using multi command, after executing multi command we are executing the set and get command. Then we are executing the exec command to stop the transaction as follows.
Command:
MULTI
SET redis_key val1
GET redis_key
exec
Output:
It is possible to encounter two types of errors when using redis transactions. The command fails while using queued, so there will be an error while executing the exec command. For the specified instance, the command is either syntactically incorrect or contains a critical condition such as out of memory.
How to Use Redis WATCH Transaction?
The single client will perform the operation in a specified time. The watch transaction is a command that makes redis transaction execution conditional. We only ask Redis to perform transactions if the watched keys are changed. It will include client-made modifications such as write and redis commands. If keys were modified while it was being watched and an exec was received, the entire transaction would be aborted.
We are calling the watch command multiple times; we simply need to watch the calls that are in effect to watch for changes from the specified call at the time when the exec is called. We can also send multiple keys into the same watch call. When exec is called, all keys are unwatched, regardless of whether the transaction is aborted or not. When we disconnect the client connection, all commands become unwatched. The below example shows how we can use the redis watch transaction as follows.
Command:
WATCH redis_key1
MULTI
ZREM zset ele
EXEC
Output:
In the above example, we can see that first we are using the watch command and defining the key name as redis_key1 then we are starting the transaction using multi command then we are executing the zrem command to start the transaction and ending the same by using the exec command.
Redis Create Transaction
When the transaction is created, the multi command returns OK in the output and the transaction is started. We are executing multiple commands one by one after executing the multi command. Redis queues all commands into the transaction unit that we are manually executing. We’re doing the same thing by using the exec command. It will instruct Redis to execute all commands queued in the order of insertion.
The below example shows how we can create the transaction by using the multi commands.
Command:
MULTI
Output:
After creating the transaction now, we are executing the multiple commands, after executing the multiple commands we are stopping the transaction by using the exec command.
Command:
SET key1 val1
GET key1
EXEC
Output:
Redis WATCH Command
Redis transactions are not unlike those found in relational databases. The redis transaction refers to a single unit of work made up of multiple queries. If any changes are made to the database, the operation is classified as a transaction.
The command enables us to implement the set and check feature. We are monitoring the watch command, which accepts redis keys as parameters. If any keys are changed before the exec is called, Redis will automatically terminate the transactions and return null in response.
The following example demonstrates how the redis watch command works. First, we execute the watch command, then we start the transaction, and finally, we close the same transaction.
Command:
WATCH key2
MULTI
SET key2 val
EXEC
Output:
In the below example, we are using set and get commands after the execution of the watch command.
Command:
WATCH key2
MULTI
SET key2 val
GET key2
EXEC
Output:
Redis WATCH Statement
All of the commands specified in the Redis watch statement are specified in the unit of transaction that we are executing. All of the commands in the transaction unit are executing properly. All of the statements in the transaction unit are successfully executing; if one command fails, the entire transaction block statement fails.
When the client cannot connect to the server and the transaction unit test is not running, we serialize the third command into the transaction. The following is an example of a redis watch statement.
Command:
WATCH key5
MULTI
SET key5
GET key5
EXEC
Output:
FAQs
Given below are the FAQs mentioned:
Q1. What is the use of watch command in redis?
Answer: In redis watch command is used to implement the feature of set and check. The watch command in redis will accept the key which we need to define with the watch command.
Q2. What is the use of redis transactions?
Answer: Redis transaction is used to execute multiple commands in a single set. We can execute different types of commands in a single set.
Q3. How do we use the redis watch command?
Answer: We are using a command by defining the key. After executing the watch command we are starting and executing the redis transaction.
Conclusion
The request sent by another client is not being saved in the middle of the redis transaction’s execution. It works while executing the exec command; in other words, redis only performs the transaction if the keys being watched are not modified while the transaction is not executing.
Recommended Articles
This is a guide to Redis WATCH. Here we discuss the introduction, use, redis WATCH command, statement, and FAQs. You may also have a look at the following articles to learn more –