How to increment a integer value in redis – Redis INCR | INCRBY

1 month ago Lalit Bhagtani 0

In this tutorial, we will learn about how to increment the string representing a integer value stored at a key in redis datastore, by using redis INCR and INCRBY commands.

INCR Command :-

The INCR command is used to increment the string representing a integer value stored at the specified key by one. If the key does not exist, it is first created and set to 0 before performing the increment operation. If the key exist but value stored at the key is of wrong datatype ( not string datatype ) or contains a string value that can not be represented as a integer then error is returned. This operation is limited to 64 bit signed integers. The syntax of redis INCR command is as follows :-

Syntax :-

redis host:post> INCR <keyname>

Output :-

- (integer) reply, representing the value of the key after the increment operation.

Example :-

Redis INCR

INCRBY Command :-

The INCRBY command is used to increment the string representing a integer value stored at the key by a specified value. This command is very similar to INCR command with difference is that, in INCRBY integer value is increased by a specified value while in INCR integer value is always increased by one. The syntax of redis INCRBY command is as follows :-

Syntax :-

redis host:post> INCRBY <keyname> <increment>

Output :-

- (integer) reply, representing the value of the key after the increment operation.

Example :-

Redis INCRBY

References :-

  1. INCR Command Docs
  2. INCRBY Command Docs

That’s all for how to increment the string representing a integer value stored in redis datastore. If you liked it, please share your thoughts in comments section and share it with others too.