EDUCBA

EDUCBA

MENUMENU
  • Blog
  • Free Courses
  • All Courses
  • All in One Bundle
  • Login
Home Software Development Software Development Tutorials Redis Tutorial Redis EXISTS

Redis EXISTS

Introduction to Redis EXISTS

Redis exists command is used to check whether the specified key exists or not in the redis database server. In redis from version 3.0, we have the possibility to specify multiple keys instead of using a single key. In the same case, it will return the total number of keys that exist on the database server. To return 1 and 0 for the single key is a special case for usage.

Redis EXISTS

Key Takeaways

  • Redis exists is a very useful command which is used to check whether the key exists or not onto the database server.
  • At the time of executing the redis exists command it will return 0 and an integer number value in the output. If the key does not exist then it will return a 0 value.

What is Redis EXISTS?

Exist in Redis is used to check whether or not the specified key exists on the database server. If we write one key multiple times, the exist command counts the key multiple times. The Exist command returns a 0 and an integer value. If the specified key does not exist on the server, the output will be zero. If the specified key is found on the server, the output will be an integer value.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

If we enter a number of keys in the exist command, it will return the number of keys that exist on the server. We use the redis exist command to check the existence of one or more keys that were present on the database server. The exist command in Redis is used to determine the number of keys on the database server.

How to Check Redis EXIST in the Key?

The below steps show how we can check that redis exists by using the key as follows:

To use redis exist we need to install redis on our system.

1. While using the redis exists command with the key we need to install the redis in our system. In the below example, we install redis on the ubuntu system.

Command:

# apt install redis-server

Output:

Install Redis EXISTS

2. While installing the redis server on the ubuntu server, we need to start the same. The below example shows how we can start the redis server.

Command:

# redis-server

Output:

Starting Redis EXISTS

3. We need to login to the database server while starting the server to check if the key exists in Redis. The below example shows to login into the database server as follows.

Command:

# redis-cli

Output:

Check if Redis EXISTS

4. While logging in the database server, we are creating the key using the set command as follows.

Command:

SET key101 val1
SET key 102 val2

Output:

Creating the key

5. In this step, we are using the single key with the redis exist command to check the specified key as follows.

Command:

EXISTS key101

Output:

Redis EXISTS - Specified Keys

6. Now in this step we are using the two keys with redis exist command to check the specified key as follows.

Command:

EXISTS key101 key102

Output:

Redis EXISTS Command

Redis EXISTS Keys

We define the specified key while using the exists key to determine whether or not the key exists on the database server. The following syntax shows redis exist key. We recommend using the key name when using the redis exists command.

Syntax:

EXISTS name_of_key;

In the above syntax exist command is used in redis to check the existence of the key in the database server. At the time of using exists command, we need to use the key name with the redis exists command. The below example shows redis will return a multiple number of keys when we define the same key in our command.

Command:

SET key103 val3
SET key104 val4
EXISTS key103 key103 key103

Output:

Redis EXISTS - Multiple Keys

In the above example, we can see that we have used key103 three times, so it will show the output as 3 times the integer value. If suppose we are defining the wrong key value then it will show the output as 0 integer value as follows. In the below example, we have used key110 which did not exist in the database so it will return a 0 value as output.

Command:

EXISTS key110

Output:

Redis EXISTS - 0 Value

Redis EXISTS Command

The exists command in Redis allows us to determine whether or not a specified key exists in the database. The following example shows how to use the exists command on the newly created key. We created the key105 by using the set command, and then we used the exists command on the specified key to check its existence. We can see that when we use the key105 key with exists command, it returns the integer 1 value in the output.

Command:

SET key105 val1
EXISTS key105

Output:

Redis EXISTS - Integer Value

In the example below, we are passing multiple keys in a single exists command. When multiple keys are passed, it will return the value of the key that was present in the key. We created key names key11 and key12, but we used the exists command to define three key names: key11, key12, and key13. Because key13 does not exist in our database, it will return two values in the output.

Command:

SET key11 val1
SET key12 val12
EXISTS key11 key12 key13

Output:

passing multiple keys

In the below example, we are using an already created key from the database server. We are using mylist key as follows.

Command:

EXISTS mylist

Output:

Redis EXISTS mylist key

Examples of Redis EXISTS

Below examples shows that redis exists command as follows:

Example #1

Here we are creating key21 and the same we are using in redis exists.

Command:

SET key21 val1
EXISTS key21

Output:

creating key21

Example #2

In this example we are creating three keys in single exists command as follows. We are creating key22, key23 and key24, and the same we are using in exists commands as follows.

Command:

SET key22 val1
SET key23 val1
SET key24 val1
EXISTS key22 key23 key24

Output:

creating key22, key23 and key24

Example #3

In this example, we are using a key that does not exists in the database. We are using the key name key31, so it will show the output as zero as follows.

Command:

EXISTS key31

Output:

key name as key31

Example #4

In this example, we are using three keys but it will show the output as 1 because two keys are not present in DB server.

Command:

SET key41 val1
EXISTS key41 key42 key43

Output:

two keys are not present

FAQs

Given below are the FAQs mentioned:

Q1. What is the use of redis exists command?

Answer: Redis exists command is used to get the value of existing keys from the database. It retrieves the existing key number.

Q2. Which command we are using with exist command to create key?

Answer: For creating the new key, we use the set command in combination with the exists command. We can use the key created with the exists command.

Q3. Which integer value is returned by the Redis exist command?

Answer: If the key is not present, the Redis exist command returns 0; otherwise, it returns an integer number if the key is present.

Conclusion

While using exist in redis it is used to check whether the specified key exists or not in the database server. If suppose we have written one key multiple times then exist command counts the key multiple times. Redis exists command is used to check whether the specified key exists or not in the redis database server.

Recommended Articles

This is a guide to Redis EXISTS. Here we discuss the introduction, how to check redis exists in the key, examples, 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
All in One Excel VBA Bundle
500+ Hours of HD Videos
15 Learning Paths
120+ Courses
Verifiable Certificate of Completion
Lifetime Access
Financial Analyst Masters Training Program
1000+ Hours of HD Videos
43 Learning Paths
250+ Courses
Verifiable Certificate of Completion
Lifetime Access
All in One Data Science Bundle
1500+ Hour of HD Videos
80 Learning Paths
360+ Courses
Verifiable Certificate of Completion
Lifetime Access
All in One Software Development Bundle
3000+ Hours of HD Videos
149 Learning Paths
600+ Courses
Verifiable Certificate of Completion
Lifetime Access
Primary Sidebar
All in One Software Development Bundle3000+ Hours of HD Videos | 149 Learning Paths | 600+ Courses | Verifiable Certificate of Completion | Lifetime Access
Financial Analyst Masters Training Program1000+ Hours of HD Videos | 43 Learning Paths | 250+ Courses | Verifiable Certificate of Completion | Lifetime Access
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

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*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

🚀 Hurry! - Any Learning Path @ $19 | OFFER ENDING IN ENROLL NOW