EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login
Home Software Development Software Development Tutorials Redis Tutorial Redis WATCH
Secondary Sidebar
Redis Tutorial
  • Redis
    • Redis Pubsub
    • Redis GUI
    • Redis Database
    • Redis Get All Keys
    • Redis TTL
    • Redis EXPIRE
    • Redis Version
    • Redis EXPIRE
    • Redis List
    • Redis CLI Commands
    • Redis HSET
    • Redis FLUSHDB
    • Redis Key
    • Redis Persistence
    • Redis FLUSHALL
    • Redis pipeline
    • Redis SADD
    • Redis ZADD
    • Redis ZSET
    • Redis Get
    • Redis Replication
    • Redis Delete All Keys
    • Redis Use Cases
    • Redis HMSET
    • Redis Transactions
    • Redis Monitor
    • Redis HGET
    • Redis SETEX
    • Redis Rails
    • RedisJSON
    • Redis Get Key
    • Redis Auth
    • Redis gem
    • Redis Key Count
    • RedisTimeSeries
    • Redis Timeout
    • Redis LPUSH
    • Redis Architecture
    • Redis API
    • Redis Message Queue
    • Redis RPUSH
    • Redis Protocol
    • Redis Data Types
    • Redis Namespace
    • Redis ZRANGE
    • Redis Master Slave
    • Redis Memory Usage
    • Redis WATCH
    • Redis URL
    • Redis Key Value
    • Redis INCR
    • Redis EXISTS
    • Redis INFO
    • Redis HMGET
    • Redis Operator
    • Redis NOAUTH Authentication Required
    • Redis LRANGE
    • Redis RDB
    • Redis TLS
    • Redis HDEL
    • Redis WebSocket
    • Redis HGETALL
    • Redis GETSET
    • Node-RED WebSocket

Redis WATCH

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.

Redis WATCH

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.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

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:

Redis WATCH - exec command

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:

Redis WATCH Transaction

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:

create the transaction by using multi command

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 - Executing Multiple commands

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:

Start the transaction

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:

set and get commands

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:

Redis WATCH Command Example

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 –

  1. Redis Get All Keys
  2. Redis GUI
  3. Redis Pubsub
  4. Redis Version
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more