How to Use the Redis INCRBY command

Have a Database Problem? Speak with an Expert for Free
Get Started >>

Introduction

When you’re working with data in Redis, it’s important to understand the commands available to access and manipulate values. For example, you may need to know how to increment a Redis value by a certain amount. This operation can be performed with the help of the INCRBY command. In this article, we’ll take a closer look at the Redis INCRBY command and review some examples that illustrate how to use it.

Prerequisites

Before diving into the examples in this tutorial, make sure you have a Redis server running on your device. You can verify the version of Redis installed on your machine by executing the command redis-server --version.

String in Redis

All values are stored as strings in Redis, even if they represent an integer. A Redis string is binary-safe– this means it’s composed of a series of bytes and can contain any kind of data. The length of a string value in Redis can be up to 512MB. Although Redis strings are used to represent integers, they can still be incremented with the use of the INCRBY command.

To run the Redis command-line interface, execute the following command:

1
redis-cli

Let’s look at an example that shows how to use the Redis INCRBY command. To begin, we’ll set the key count, which has an integer value of 100:

1
2
3
4
127.0.0.1:6379> SET count 100
OK
127.0.0.1:6379> GET count
"100"

The Redis INCRBY Command

If you have a key stored with a value that represents an integer, the INCRBY command can be used to increment the integer with a specified value. We’ll use the INCRBY command to increment COUNT by 50:

1
2
127.0.0.1:6379> INCRBY count 50
(integer) 150

The command will return an error if the key value contains an incorrect data type, such as a string that can’t be represented as an integer. We can see how this works in the example below:

1
2
127.0.0.1:6379> INCRBY greetings
(error) ERR wrong number of arguments for 'incrby' command

The Redis INCRBYFLOAT Command

Using the INCRBYFLOAT command will increment a string that represents a floating number stored at a key in Redis. It will increment the value by the specified amount:

1
2
3
4
127.0.0.1:6379> SET float 5.5
OK
127.0.0.1:6379> INCRBYFLOAT float 17.2
"22.7"

You can also use INCRBYFLOAT to increment a whole integer value by a floating-point value:

1
2
127.0.0.1:6379> INCRBYFLOAT count .3
"150.3"

The Hash in Redis

The next two commands we’ll discuss involve incrementing values stored in hashes, so let’s take a minute to define this important Redis data type. A hash can be thought of as a map, connecting string field names with their respective string values. Put simply, Redis hashes are containers of fields and their values. They’re a great tool for representing an entity as a Redis data structure.

To create a hash in Redis, we use the first command shown below. The second command shows how to retrieve a value stored at a key:

1
2
3
4
127.0.0.1:6379> HSET hash_key field 2
(integer) 1
127.0.0.1:6379> HGET hash_key field
"2"

The Redis HINCRBY Command

The HINCRBY command is used to increment the stored integer in a hash field with a specified value:

1
2
127.0.0.1:6379> HINCRBY hash_key field 8
(integer) 10

The Redis Hincrbyfloat Command

The HINCRBYFLOAT command is used to increment the field value stored at a hash key by a specific number:

1
2
127.0.0.1:6379> HINCRBYFLOAT hash_key field 5.5
"15.5"

NOTE: If you supply a negative integer or floating-point value in any of these commands, the command will decrement the key instead of adding the value.

Conclusion

If you’re planning to use Redis to store and manage data, it’s important to know how to increment values. The INCRBY command allows users to specify the amount by which they want to increment an integer value; other related commands allow users to increment floating-point values and values stored in hashes. In this article, we showed you how to use the Redis INCRBY commands along with these related commands, and we provided examples that demonstrate how to use each one. With these examples to get you started, you’ll be prepared to use the INCRBY command in your own Redis environment.

Pilot the ObjectRocket Platform Free!

Try Fully-Managed CockroachDB, Elasticsearch, MongoDB, PostgreSQL (Beta) or Redis.

Get Started

Keep in the know!

Subscribe to our emails and we’ll let you know what’s going on at ObjectRocket. We hate spam and make it easy to unsubscribe.