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 INCR
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 INCR

Introduction to Redis INCR

Redis INCR is used to increment the number that was previously stored in the key. If the key does not exist, it will be set to zero before any operation is performed. If the query contains an incorrect value, an error such as the wrong data type or a string representing the integer value is returned. The redis incr operations are limited to signed integers of 64 bits and will include string operations.

Redis INCR

Key Takeaways

  • Incr command is useful when we need to increment the value of the key with 1 in a specified interval of time.
  • By using incr command we are incrementing the key value with one. If suppose key does not exist then it will create new key.

What is Redis INCR?

The redis incr command is used to increment the value specified in the key. If the key does not exist when the value is incremented, it will be created. The Redis incr command returns an error in output if we provide incorrect input, such as a string value instead of an integer value.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

The Redis incr command only accepts integer values; if we provide a string value, we will receive an error stating that string values are not permitted when using the incr command. We can use multiple values to increment our key in Redis. If we have set our key value to 1 and need to increment it by 1 to make it 2, we can use redis incr at the same time. We can also increment the value of key as per value.

How to Use Redis INCR?

To use the redis incr command we need to install the same in our system.

1. In this step we are installing the redis server in our system. The below example shows to install the redis as follows.

Command:

# apt install redis-server

Output:

Install Redis INCR Server

2. In Ubuntu server, while installing, the redis server does not start automatically. To start the same we need to execute the below command. While starting the redis server it will accept the redis connections.

Command:

# redis-server

Output:

Redis INCR Connections

3. Now in this step we are login in the redis server by using the below command. We are connecting to the local host server.

Code:

# redis-cli

Output:

connecting to the localhost

4. While connecting to the redis server, now we are creating the key by using the set command as follows.

Command:

SET incr_key 101

Output:

we are creating the key

5. After setting the value of the key now in this step we are incrementing the key value by 1 as follows.

Command:

INCR incr_key

Output:

Redis INCR Key Value

6. After executing the incr command, now in this step we are checking the current value of the key.

Command:

GET incr_key

Output:

checking the current value

Redis String INCR Command

Basically, redis string incr command is used to increment the value of the key by 1. The below syntax shows the redis string incr command as follows:

Syntax:

INCR key_name

In the above syntax incr command is used in redis to increment the value of the key which we have defined. The key name parameter is defined as the name of the key that we used with the incr command.

The below example shows the redis incr command. We are creating the key name as incr_key1 and assigning the value to the key as 10. Then we are incrementing the value with 1 using incr command then we are using get method to check the current value of the key.

Command:

SET incr_key1 10
INCR incr_key1
GET incr_key1

Output:

Redis INCR - Get Method

In the below example, if we are not creating any key at the time of using the incr command then we can see that it will create the new key.

Command:

INCR incr_key2
GET incr_key2

Output:

Redis INCR - Create a new key

If we have defined the string value at the time of creating key, then the incr command is returning an error as follows.

Command:

SET incr_key4 val1
INCR incr_key4

Output:

returning error

Basic Points of Redis INCR

Given below are the basic points of redis INCR:

1. Redis DECOR Command

This command is used to decrement the number which was stored in the key. If the key does not exist in DB then it will set the value of the key as zero. If the key contains the wrong type value then the decr command returns an error.

Below syntax shows the decr command as follows:

Syntax:

DECR key_name

The below example shows the redis decor as follows. We are creating a key by using the set command then we are using the decr command to decrement the key value by 1 as follows.

Command:

SET key1 10
DECR key1
GET key1

Output:

Redis INCR - decr command

In the below example, we are creating the key by using the decr command because it is not present.

Command:

DECR key_decor
GET key_decor

Output:

Redis INCR - Creating the key

2. Redis INCRBY Command

Redis incrby command is used to increment the key value by increment. Suppose the key does not exist then it will be set as zero before performing any operation on the key. The below syntax shows the incrby command as follows.

Syntax:

INCRBY key_name integer_val

The below example shows redis incrby command. We are using the key name as incby_key and incrementing the value of a key by 10 as follows.

Command:

SET incrby_key 10
INCRBY incrby_key 10
GET incrby_key

Output:

Redis INCR - Incrementing key value

3. Redis DECRBY Command

Redis decrby command is used to decrement the key value by decrement. Suppose the key does not exist, then it will be set as zero before performing any operation on the key. The below syntax shows decrby command as follows.

Syntax:

DECRBY key_name integer_val

The below example shows the redis decrby command. We are using the key name as decrby_key and decrementing the value of the key by 5 as follows.

Command:

SET decrby_key 10
DECRBY decrby_key 5
GET decrby_key

Output:

decrementing the value

4. Redis INCRBYFLOAT Command

This command is used to represent the floating point number that was stored in the key and the same is specified by increment. The below syntax shows incrbyfloat command as follows.

Syntax:

INCRBYFLOAT key_name float_val

The below example shows the incrbyfloat command in redis. We are setting the value as a floating point number as follows.

Command:

SET key10 10.5
INCRBYFLOAT key10 20.5
GET key10

Output:

Setting the value

5. Redis Closing Command

The redis closing command is used to close the connection of redis. To close the redis connection we are using the quit command. At the time of executing this command connection is closed as well as all the operation is closed. The below syntax shows the closing command as follows.

Syntax:

QUIT

The below example shows how we can close the connection in redis as follows. We are closing the connection using the quit command.

Command:

QUIT

Output:

Quit Command

FAQs

Given below are the FAQs mentioned:

Q1. What is the use of the incr command in redis?

Answer: The incr command in redis is used to increment the value of the key by one. We need to specify the integer value in the key at the time of creation.

Q2. Which argument is accepted in the redis incr command?

Answer: Redis incr command accepts the key_name at the time of executing the same. If the key is created, it will increment the value of one. If the key is not created, then it will create new key.

Q3. What is the difference between incr of incrby command in redis?

Answer: Incr command increments the key value by one. Incrby command is used to increment the value as per the value that we have defined in the key.

Conclusion

Redis incr command only accepts integer values; if we provide a string value, we will receive an error stating that string values are not permitted when using the incr command. Redis INCR is used to increment the number that was previously stored in the key. If the key does not exist, it will be set to zero before any operation is performed.

Recommended Articles

This is a guide to Redis INCR. Here we discuss the introduction, use, and basic points of redis INCR and FAQs respectively. 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
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
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