Updated February 15, 2023
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.
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.
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:
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:
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:
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:
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:
6. After executing the incr command, now in this step we are checking the current value of the key.
Command:
GET incr_key
Output:
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:
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:
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:
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:
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:
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:
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:
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:
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:
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 –