How to decrement a integer value in redis – Redis DECR | DECRBY

5 years ago Lalit Bhagtani 0

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

DECR Command

The DECR command is used to decrement 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 decrement 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 DECR command is as follows :-

Syntax :-

redis host:post> DECR <keyname>

Output :-

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

Example :-

Redis DECR

DECRBY Command

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

Syntax :-

redis host:post> DECRBY <keyname> <decrement>

Output :-

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

Example :-

Redis DECRBY

References :-

  1. DECR Command Docs
  2. DECRBY Command Docs

That’s all for how to decrement 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.

<- String Commands