Redis Command: DECR

Summary

Command NameDECR
UsageDecrement integer value
Group string
ACL Category@write
@string
@fast
Time ComplexityO(1)
FlagWRITE
DENYOOM
FAST
Arity2

Signature

DECR <key>

Usage

Decrease the value stored in <key> by One(1).

Notes

  • If the key does not exist then this command will set the key and the value will be Zero(0), before performing the operation. And after the operation is performed, the value will be negative one(-1).

Arguments

ParameterDescriptionNameType
<key>The key namekeykey

Return Value

Returns value after decrement is performed.

Return valueCase for the return valueType
Integer valueThe value of key after the incrementinteger
errorWhen the saved value for the key is not a valid integer value
or the increased value exceeds the max allowed value
error

Notes

  • If the value is not an integer, like a string that can not be covered to an integer, or a list, set, etc. then this command will return an error. Error message will be as below-
    (error) ERR value is not an integer or out of range
  • If the value exceeds the minimum allowed integer value of 64-bit signed integer, then we get following error message-
    (error) ERR increment or decrement would overflow

Examples

Here are a few examples of the DECR command usage-

# Redis DECR command examples

# Set the value of user:23:score key to 85
127.0.0.1:6379> set user:23:score 85
OK

# decreament value of user:23:score
127.0.0.1:6379> decr user:23:score
(integer) 84

# Check value of user:23:score key
127.0.0.1:6379> get user:23:score
"84"

# Check type of user:23:score
127.0.0.1:6379> type user:23:score
string



# Check if some key named "unknownkey" exists
# it does not exist yet
127.0.0.1:6379> get unknownkey
(nil)

# Try to decreament the value of "unknownkey" using decr command
# The value of "unknownkey" is decreamented to 1
127.0.0.1:6379> decr unknownkey
(integer) -1

# Check the value of "unknownkey"
127.0.0.1:6379> get unknownkey
"-1"



# Set a string vlaue to sitename key
127.0.0.1:6379> set sitename bigboxcode
OK

# Try to apply DECR command to sitename
# We get an error as the value in sitename key is not an integer
127.0.0.1:6379> decr sitename
(error) ERR value is not an integer or out of range



# Max value of allowed integer for 64-bit integer is 9,223,372,036,854,775,807
# Let's set the value of key "mymaxtest" to a value more than that
127.0.0.1:6379> set mymaxtest 9223372036854775810
OK

# Let's decreament the vlaue of "mymaxtest"
# We get an error
127.0.0.1:6379> decr mymaxtest
(error) ERR value is not an integer or out of range



# Min value allowed as 64-bit int is -9,223,372,036,854,775,808
# Lets set a value close to that, -9,223,372,036,854,775,807
127.0.0.1:6379> set mymintest  -9223372036854775807
OK

# Try to decr the value, it will work as it is still in range
127.0.0.1:6379> decr mymintest
(integer) -9223372036854775808

# If we try to decrease once again we get error
127.0.0.1:6379> decr mymintest
(error) ERR increment or decrement would overflow

Notes

  • Redis does not have a specific data type for integer. Any integer value is saved as string.
  • Before performing the DECR operation the string value is converted to a 64-bit signed integer(base 10).
  • As the min value of a 64-bit signed integer is –9,223,372,036,854,775,808. So if DECR is performed on a value less than that value, then an error is thrown, with message-
    (error) ERR increment or decrement would overflow
  • Max value that we can perform this operation is 9,223,372,036,854,775,807.

Code Implementations

Here are the usage examples of the Redis DECR command in different programming languages.

// Redis DECR command example in Golang

package main

import (
	"context"
	"fmt"

	"github.com/redis/go-redis/v9"
)

var rdb *redis.Client
var ctx context.Context

func init() {
	rdb = redis.NewClient(&redis.Options{
		Addr:     "localhost:6379",
		Username: "default",
		Password: "",
		DB:       0,
	})

	ctx = context.Background()
}

func main() {

	/**
	* Set the value of user:23:score key to 85
	*
	* Command: set user:23:score 85
	* Result: OK
	 */
	commandResult, err := rdb.Set(ctx, "user:23:score", "85", 0).Result()

	if err != nil {
		fmt.Println("Command: set user:23:score 85 | Error: " + err.Error())
	}

	fmt.Println("Command: set user:23:score 85 | Result: " + commandResult)

	/**
	* decreament value of user:23:score
	*
	* Command: decr user:23:score
	* Result: (integer) 84
	 */
	decrResult, err := rdb.Decr(ctx, "user:23:score").Result()

	if err != nil {
		fmt.Println("Command: decr user:23:score | Error: " + err.Error())
	}

	fmt.Printf("Command: decr user:23:score | Result: %v\n", decrResult)

	/**
	* Check value of user:23:score key
	*
	* Command: get user:23:score
	* Result: "84"
	 */
	commandResult, err = rdb.Get(ctx, "user:23:score").Result()

	if err != nil {
		fmt.Println("Command: get user:23:score | Error: " + err.Error())
	}

	fmt.Println("Command: get user:23:score | Result: " + commandResult)

	/**
	* Check type of user:23:score
	*
	* Command: type user:23:score
	* Result: string
	 */
	commandResult, err = rdb.Type(ctx, "user:23:score").Result()

	if err != nil {
		fmt.Println("Command: type user:23:score | Error: " + err.Error())
	}

	fmt.Println("Command: type user:23:score | Result: " + commandResult)

	/**
	* Check if some key named "unknownkey" exists
	* it does not exist yet
	*
	* Command: get unknownkey
	* Result: (nil)
	 */
	commandResult, err = rdb.Get(ctx, "unknownkey").Result()

	if err != nil {
		fmt.Println("Command: get unknownkey | Error: " + err.Error())
	}

	fmt.Println("Command: get unknownkey | Result: " + commandResult)

	/**
	* Try to decreament the value of "unknownkey" using decr command
	* The value of "unknownkey" is decreamented to 1
	*
	* Command: decr unknownkey
	* Result: (integer) -1
	 */
	decrResult, err = rdb.Decr(ctx, "unknownkey").Result()

	if err != nil {
		fmt.Println("Command: decr unknownkey | Error: " + err.Error())
	}

	fmt.Printf("Command: decr unknownkey | Result: %v\n", decrResult)

	/**
	* Check the value of "unknownkey"
	*
	* Command: get unknownkey
	* Result: "-1"
	 */
	commandResult, err = rdb.Get(ctx, "unknownkey").Result()

	if err != nil {
		fmt.Println("Command: get unknownkey | Error: " + err.Error())
	}

	fmt.Println("Command: get unknownkey | Result: " + commandResult)

	/**
	* Set a string vlaue to sitename key
	*
	* Command: set sitename bigboxcode
	* Result: OK
	 */
	commandResult, err = rdb.Set(ctx, "sitename", "bigboxcode", 0).Result()

	if err != nil {
		fmt.Println("Command: set sitename bigboxcode | Error: " + err.Error())
	}

	fmt.Println("Command: set sitename bigboxcode | Result: " + commandResult)

	/**
	* Try to apply DECR command to sitename
	* We get an error as the value in sitename key is not an integer
	*
	* Command: decr sitename
	* Result: (error) ERR value is not an integer or out of range
	 */
	decrResult, err = rdb.Decr(ctx, "sitename").Result()

	if err != nil {
		fmt.Println("Command: decr sitename | Error: " + err.Error())
	}

	fmt.Printf("Command: decr sitename | Result: %v\n", decrResult)

	/**
	* Max value of allowed integer for 64-bit integer is 9,223,372,036,854,775,807
	* Let's set the value of key "mymaxtest" to a value more than that
	*
	* Command: set mymaxtest 9223372036854775810
	* Result: OK
	 */
	commandResult, err = rdb.Set(ctx, "mymaxtest", "9223372036854775810", 0).Result()

	if err != nil {
		fmt.Println("Command: set mymaxtest 9223372036854775810 | Error: " + err.Error())
	}

	fmt.Println("Command: set mymaxtest 9223372036854775810 | Result: " + commandResult)

	/**
	* Let's decreament the vlaue of "mymaxtest"
	* We get an error
	*
	* Command: decr mymaxtest
	* Result: (error) ERR value is not an integer or out of range
	 */
	decrResult, err = rdb.Decr(ctx, "mymaxtest").Result()

	if err != nil {
		fmt.Println("Command: decr mymaxtest | Error: " + err.Error())
	}

	fmt.Printf("Command: decr mymaxtest | Result: %v\n", decrResult)

	/**
	* Min value allowed as 64-bit int is -9,223,372,036,854,775,808
	* Lets set a value close to that, -9,223,372,036,854,775,807
	*
	* Command: set mymintest  -9223372036854775807
	* Result: OK
	 */
	commandResult, err = rdb.Set(ctx, "mymintest", "-9223372036854775807", 0).Result()

	if err != nil {
		fmt.Println("Command: set mymintest  -9223372036854775807 | Error: " + err.Error())
	}

	fmt.Println("Command: set mymintest  -9223372036854775807 | Result: " + commandResult)

	/**
	* Try to decr the value, it will work as it is still in range
	*
	* Command: decr mymintest
	* Result: (integer) -9223372036854775808
	 */
	decrResult, err = rdb.Decr(ctx, "mymintest").Result()

	if err != nil {
		fmt.Println("Command: decr mymintest | Error: " + err.Error())
	}

	fmt.Printf("Command: decr mymintest | Result: %v\n", decrResult)

	/**
	* If we try to decrease once again we get error
	*
	* Command: decr mymintest
	* Result: (error) ERR increment or decrement would overflow
	 */
	decrResult, err = rdb.Decr(ctx, "mymintest").Result()

	if err != nil {
		fmt.Println("Command: decr mymintest | Error: " + err.Error())
	}

	fmt.Printf("Command: decr mymintest | Result: %v\n", decrResult)

}

Output:

Command: set user:23:score 85 | Result: OK

Command: decr user:23:score | Result: 84
Command: get user:23:score | Result: 84
Command: type user:23:score | Result: string

Command: get unknownkey | Error: redis: nil
Command: get unknownkey | Result:

Command: decr unknownkey | Result: -1
Command: get unknownkey | Result: -1

Command: set sitename bigboxcode | Result: OK

Command: decr sitename | Error: ERR value is not an integer or out of range
Command: decr sitename | Result: 0

Command: set mymaxtest 9223372036854775810 | Result: OK

Command: decr mymaxtest | Error: ERR value is not an integer or out of range
Command: decr mymaxtest | Result: 0

Command: set mymintest  -9223372036854775807 | Result: OK

Command: decr mymintest | Result: -9223372036854775808
Command: decr mymintest | Error: ERR increment or decrement would overflow
Command: decr mymintest | Result: 0

Notes

  • Use method “Decr” from package go-redis.
  • Signature of the method is-
    Decr(ctx context.Context, key string) *IntCmd
// Redis DECR command example in JavaScript(NodeJS)

import { createClient } from 'redis';

// Create redis client
const redisClient = createClient({
    url: 'redis://default:@localhost:6379'
});

redisClient.on('error', err => console.log('Error while connecting to Redis', err));

// Connect Redis client
await redisClient.connect();


/**
 * Set the value of user:23:score key to 85
 *
 * Command: set user:23:score 85
 * Result: OK
 */
let commandResult = await redisClient.set("user:23:score", "85");

console.log("Command: set user:23:score 85 | Result: " + commandResult);

/**
 * decreament value of user:23:score
 *
 * Command: decr user:23:score
 * Result: (integer) 84
 */
commandResult = await redisClient.decr("user:23:score");

console.log("Command: decr user:23:score | Result: " + commandResult);

/**
 * Check value of user:23:score key
 *
 * Command: get user:23:score
 * Result: "84"
 */
commandResult = await redisClient.get("user:23:score");

console.log("Command: get user:23:score | Result: " + commandResult);

/**
 * Check type of user:23:score
 *
 * Command: type user:23:score
 * Result: string
 */
commandResult = await redisClient.type("user:23:score");

console.log("Command: type user:23:score | Result: " + commandResult);


/**
 * Check if some key named "unknownkey" exists
 * it does not exist yet
 *
 * Command: get unknownkey
 * Result: (nil)
 */
commandResult = await redisClient.get("unknownkey");

console.log("Command: get unknownkey | Result: " + commandResult);

/**
 * Try to decreament the value of "unknownkey" using decr command
 * The value of "unknownkey" is decreamented to 1
 *
 * Command: decr unknownkey
 * Result: (integer) -1
 */
commandResult = await redisClient.decr("unknownkey");

console.log("Command: decr unknownkey | Result: " + commandResult);

/**
 * Check the value of "unknownkey"
 *
 * Command: get unknownkey
 * Result: "-1"
 */
commandResult = await redisClient.get("unknownkey");

console.log("Command: get unknownkey | Result: " + commandResult);


/**
 * Set a string vlaue to sitename key
 *
 * Command: set sitename bigboxcode
 * Result: OK
 */
commandResult = await redisClient.set("sitename", "bigboxcode");

console.log("Command: set sitename bigboxcode | Result: " + commandResult);

/**
 * Try to apply DECR command to sitename
 * We get an error as the value in sitename key is not an integer
 *
 * Command: decr sitename
 * Result: (error) ERR value is not an integer or out of range
 */
try {
    commandResult = await redisClient.decr("sitename");

    console.log("Command: decr sitename | Result: " + commandResult);
} catch (e) {
    console.log("Command: decr sitename | Error: " + e);
}


/**
 * Max value of allowed integer for 64-bit integer is 9,223,372,036,854,775,807
 * Let's set the value of key "mymaxtest" to a value more than that
 *
 * Command: set mymaxtest 9223372036854775810
 * Result: OK
 */
commandResult = await redisClient.set("mymaxtest", "9223372036854775810");

console.log("Command: set mymaxtest 9223372036854775810 | Result: " + commandResult);

/**
 * Let's decreament the vlaue of "mymaxtest"
 * We get an error
 *
 * Command: decr mymaxtest
 * Result: (error) ERR value is not an integer or out of range
 */
try {
    commandResult = await redisClient.decr("mymaxtest");

    console.log("Command: decr mymaxtest | Result: " + commandResult);
} catch (e) {
    console.log("Command: decr mymaxtest | Error: " + e);
}


/**
 * Min value allowed as 64-bit int is -9,223,372,036,854,775,808
 * Lets set a value close to that, -9,223,372,036,854,775,807
 *
 * Command: set mymintest  -9223372036854775807
 * Result: OK
 */
commandResult = await redisClient.set("mymintest", "-9223372036854775807");

console.log("Command: set mymintest  -9223372036854775807 | Result: " + commandResult);

/**
 * Try to decr the value, it will work as it is still in range
 *
 * Command: decr mymintest
 * Result: (integer) -9223372036854775808
 */
commandResult = await redisClient.decr("mymintest");

console.log("Command: decr mymintest | Result: " + commandResult);

/**
 * If we try to decrease once again we get error
 *
 * Command: decr mymintest
 * Result: (error) ERR increment or decrement would overflow
 */
try {
    commandResult = await redisClient.decr("mymintest");

    console.log("Command: decr mymintest | Result: " + commandResult);
} catch (e) {
    console.log("Command: decr mymintest | Error: " + e);
}

process.exit(0);

Output:

Command: set user:23:score 85 | Result: OK

Command: decr user:23:score | Result: 84
Command: get user:23:score | Result: 84
Command: type user:23:score | Result: string

Command: get unknownkey | Result: null

Command: decr unknownkey | Result: -1
Command: get unknownkey | Result: -1

Command: set sitename bigboxcode | Result: OK

Command: decr sitename | Error: Error: ERR value is not an integer or out of range

Command: set mymaxtest 9223372036854775810 | Result: OK

Command: decr mymaxtest | Error: Error: ERR value is not an integer or out of range

Command: set mymintest  -9223372036854775807 | Result: OK

Command: decr mymintest | Result: -9223372036854776000
Command: decr mymintest | Error: Error: ERR increment or decrement would overflow

Notes

  • Use method “decr” of the package node-redis.
// Redis DECR command example in Java

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

public class Decr {
    public static void main(String[] args) {
        JedisPool jedisPool = new JedisPool("localhost", 6379);

        try (Jedis jedis = jedisPool.getResource()) {

            /**
             * Set the value of user:23:score key to 85
             *
             * Command: set user:23:score 85
             * Result: OK
             */
            String commandResult = jedis.set("user:23:score", "85");

            System.out.println("Command: set user:23:score 85 | Result: " + commandResult);

            /**
             * decreament value of user:23:score
             *
             * Command: decr user:23:score
             * Result: (integer) 84
             */
            long decrResult = jedis.decr("user:23:score");

            System.out.println("Command: decr user:23:score | Result: " + decrResult);

            /**
             * Check value of user:23:score key
             *
             * Command: get user:23:score
             * Result: "84"
             */
            commandResult = jedis.get("user:23:score");

            System.out.println("Command: get user:23:score | Result: " + commandResult);

            /**
             * Check type of user:23:score
             *
             * Command: type user:23:score
             * Result: string
             */
            commandResult = jedis.type("user:23:score");

            System.out.println("Command: type user:23:score | Result: " + commandResult);


            /**
             * Check if some key named "unknownkey" exists
             * it does not exist yet
             *
             * Command: get unknownkey
             * Result: (nil)
             */
            commandResult = jedis.get("unknownkey");

            System.out.println("Command: get unknownkey | Result: " + commandResult);

            /**
             * Try to decreament the value of "unknownkey" using decr command
             * The value of "unknownkey" is decreamented to 1
             *
             * Command: decr unknownkey
             * Result: (integer) -1
             */
            decrResult = jedis.decr("unknownkey");

            System.out.println("Command: decr unknownkey | Result: " + decrResult);

            /**
             * Check the value of "unknownkey"
             *
             * Command: get unknownkey
             * Result: "-1"
             */
            commandResult = jedis.get("unknownkey");

            System.out.println("Command: get unknownkey | Result: " + commandResult);


            /**
             * Set a string vlaue to sitename key
             *
             * Command: set sitename bigboxcode
             * Result: OK
             */
            commandResult = jedis.set("sitename", "bigboxcode");

            System.out.println("Command: set sitename bigboxcode | Result: " + commandResult);

            /**
             * Try to apply DECR command to sitename
             * We get an error as the value in sitename key is not an integer
             *
             * Command: decr sitename
             * Result: (error) ERR value is not an integer or out of range
             */
            try {
            decrResult = jedis.decr("sitename");

            System.out.println("Command: decr sitename | Result: " + decrResult);
            } catch (Exception e) {
                System.out.println("Command: decr sitename | Error: " + e.getMessage());
            }


            /**
             * Max value of allowed integer for 64-bit integer is 9,223,372,036,854,775,807
             * Let's set the value of key "mymaxtest" to a value more than that
             *
             * Command: set mymaxtest 9223372036854775810
             * Result: OK
             */
            commandResult = jedis.set("mymaxtest", "9223372036854775810");

            System.out.println("Command: set mymaxtest 9223372036854775810 | Result: " + commandResult);

            /**
             * Let's decreament the vlaue of "mymaxtest"
             * We get an error
             *
             * Command: decr mymaxtest
             * Result: (error) ERR value is not an integer or out of range
             */
            try {
                decrResult = jedis.decr("mymaxtest");

                System.out.println("Command: decr mymaxtest | Result: " + decrResult);
            } catch (Exception e) {
                System.out.println("Command: decr mymaxtest | Error: " + e.getMessage());
            }


            /**
             * Min value allowed as 64-bit int is -9,223,372,036,854,775,808
             * Lets set a value close to that, -9,223,372,036,854,775,807
             *
             * Command: set mymintest  -9223372036854775807
             * Result: OK
             */
            commandResult = jedis.set("mymintest", "-9223372036854775807");

            System.out.println("Command: set mymintest  -9223372036854775807 | Result: " + commandResult);

            /**
             * Try to decr the value, it will work as it is still in range
             *
             * Command: decr mymintest
             * Result: (integer) -9223372036854775808
             */
            decrResult = jedis.decr("mymintest");

            System.out.println("Command: decr mymintest | Result: " + decrResult);

            /**
             * If we try to decrease once again we get error
             *
             * Command: decr mymintest
             * Result: (error) ERR increment or decrement would overflow
             */
            try {
                decrResult = jedis.decr("mymintest");

                System.out.println("Command: decr mymintest | Result: " + decrResult);
            } catch (Exception e) {
                System.out.println("Command: decr mymintest | Error: " + e.getMessage());
            }

        }

        jedisPool.close();

    }
}

Output:

Command: set user:23:score 85 | Result: OK

Command: decr user:23:score | Result: 84
Command: get user:23:score | Result: 84
Command: type user:23:score | Result: string

Command: get unknownkey | Result: null

Command: decr unknownkey | Result: -1
Command: get unknownkey | Result: -1

Command: set sitename bigboxcode | Result: OK

Command: decr sitename | Error: ERR value is not an integer or out of range

Command: set mymaxtest 9223372036854775810 | Result: OK

Command: decr mymaxtest | Error: ERR value is not an integer or out of range

Command: set mymintest  -9223372036854775807 | Result: OK

Command: decr mymintest | Result: -9223372036854775808
Command: decr mymintest | Error: ERR increment or decrement would overflow

Notes

  • Use method “decr” from Jedis package.
  • Signature of the “decr” is-
    public long decr(final String key)
using StackExchange.Redis;

namespace Decr
{
    internal class Program
    {
        static void Main(string[] args)
        {
            ConnectionMultiplexer redis = ConnectionMultiplexer.Connect("localhost");
            IDatabase rdb = redis.GetDatabase();

            /**
             * Set the value of user:23:score key to 85
             *
             * Command: set user:23:score 85
             * Result: OK
             */
            bool setCommandResult = rdb.StringSet("user:23:score", "85");
            Console.WriteLine("Command: set user:23:score 85 | Result: " + setCommandResult);

            /**
             * decreament value of user:23:score
             *
             * Command: decr user:23:score
             * Result: (integer) 84
             */
            long decrResult = rdb.StringDecrement("user:23:score");

            Console.WriteLine("Command: decr user:23:score | Result: " + decrResult);

            /**
             * Check value of user:23:score key
             *
             * Command: get user:23:score
             * Result: "84"
             */
            RedisValue getCommandResult = rdb.StringGet("user:23:score");
            Console.WriteLine("Command: get user:23:score | Result: " + getCommandResult);

            /**
             * Check type of user:23:score
             *
             * Command: type user:23:score
             * Result: string
             */
            RedisType typeResult = rdb.KeyType("user:23:score");
            Console.WriteLine("Command: type user:23:score | Result: " + typeResult);


            /**
             * Check if some key named "unknownkey" exists
             * it does not exist yet
             *
             * Command: get unknownkey
             * Result: (nil)
             */
            getCommandResult = rdb.StringGet("unknownkey");

            Console.WriteLine("Command: get unknownkey | Result: " + getCommandResult);

            /**
             * Try to decreament the value of "unknownkey" using decr command
             * The value of "unknownkey" is decreamented to 1
             *
             * Command: decr unknownkey
             * Result: (integer) -1
             */
            decrResult = rdb.StringDecrement("unknownkey");

            Console.WriteLine("Command: decr unknownkey | Result: " + decrResult);

            /**
             * Check the value of "unknownkey"
             *
             * Command: get unknownkey
             * Result: "-1"
             */
            getCommandResult = rdb.StringGet("unknownkey");

            Console.WriteLine("Command: get unknownkey | Result: " + getCommandResult);


            /**
             * Set a string vlaue to sitename key
             *
             * Command: set sitename bigboxcode
             * Result: OK
             */
            setCommandResult = rdb.StringSet("sitename", "bigboxcode");

            Console.WriteLine("Command: set sitename bigboxcode | Result: " + setCommandResult);

            /**
             * Try to apply DECR command to sitename
             * We get an error as the value in sitename key is not an integer
             *
             * Command: decr sitename
             * Result: (error) ERR value is not an integer or out of range
             */
            try
            {
                decrResult = rdb.StringDecrement("sitename");

                Console.WriteLine("Command: decr sitename | Result: " + decrResult);
            }
            catch (Exception e)
            {
                Console.WriteLine("Command: decr sitename | Error: " + e.Message);
            }


            /**
             * Max value of allowed integer for 64-bit integer is 9,223,372,036,854,775,807
             * Let's set the value of key "mymaxtest" to a value more than that
             *
             * Command: set mymaxtest 9223372036854775810
             * Result: OK
             */
            setCommandResult = rdb.StringSet("mymaxtest", "9223372036854775810");

            Console.WriteLine("Command: set mymaxtest 9223372036854775810 | Result: " + setCommandResult);

            /**
             * Let's decreament the vlaue of "mymaxtest"
             * We get an error
             *
             * Command: decr mymaxtest
             * Result: (error) ERR value is not an integer or out of range
             */
            try
            {
                decrResult = rdb.StringDecrement("mymaxtest");

                Console.WriteLine("Command: decr mymaxtest | Result: " + decrResult);
            }
            catch (Exception e)
            {
                Console.WriteLine("Command: decr mymaxtest | Error: " + e.Message);
            }


            /**
             * Min value allowed as 64-bit int is -9,223,372,036,854,775,808
             * Lets set a value close to that, -9,223,372,036,854,775,807
             *
             * Command: set mymintest  -9223372036854775807
             * Result: OK
             */
            setCommandResult = rdb.StringSet("mymintest", "-9223372036854775807");

            Console.WriteLine("Command: set mymintest  -9223372036854775807 | Result: " + setCommandResult);

            /**
             * Try to decr the value, it will work as it is still in range
             *
             * Command: decr mymintest
             * Result: (integer) -9223372036854775808
             */
            decrResult = rdb.StringDecrement("mymintest");

            Console.WriteLine("Command: decr mymintest | Result: " + decrResult);

            /**
             * If we try to decrease once again we get error
             *
             * Command: decr mymintest
             * Result: (error) ERR increment or decrement would overflow
             */
            try
            {
                decrResult = rdb.StringDecrement("mymintest");

                Console.WriteLine("Command: decr mymintest | Result: " + decrResult);
            }
            catch (Exception e)
            {
                Console.WriteLine("Command: decr mymintest | Error: " + e.Message);
            }
        }
    }
}

Output:

Command: set user:23:score 85 | Result: True

Command: decr user:23:score | Result: 84
Command: get user:23:score | Result: 84
Command: type user:23:score | Result: String

Command: get unknownkey | Result:

Command: decr unknownkey | Result: -1
Command: get unknownkey | Result: -1

Command: set sitename bigboxcode | Result: True

Command: decr sitename | Error: ERR value is not an integer or out of range

Command: set mymaxtest 9223372036854775810 | Result: True

Command: decr mymaxtest | Error: ERR value is not an integer or out of range

Command: set mymintest  -9223372036854775807 | Result: True

Command: decr mymintest | Result: -9223372036854775808
Command: decr mymintest | Error: ERR increment or decrement would overflow

Notes

  • Use the method “StringdEcreament” from StackExchange.Redis.
  • Signature of the method is-
    long StringDecrement(RedisKey key, long value = 1, CommandFlags flags = CommandFlags.None)
<?php
// Redis DECR command example in PHP

require 'vendor/autoload.php';

// Connect to Redis
$redisClient = new Predis\Client([
    'scheme' => 'tcp',
    'host' => 'localhost',
    'port' => 6379,
]);

/**
 * Set the value of user:23:score key to 85
 *
 * Command: set user:23:score 85
 * Result: OK
 */
$commandResult = $redisClient->set("user:23:score", "85");

echo "Command: set user:23:score 85 | Result: " . $commandResult . "\n";

/**
 * decreament value of user:23:score
 *
 * Command: decr user:23:score
 * Result: (integer) 84
 */
$commandResult = $redisClient->decr("user:23:score");

echo "Command: decr user:23:score | Result: " . $commandResult . "\n";

/**
 * Check value of user:23:score key
 *
 * Command: get user:23:score
 * Result: "84"
 */
$commandResult = $redisClient->get("user:23:score");

echo "Command: get user:23:score | Result: " . $commandResult . "\n";

/**
 * Check type of user:23:score
 *
 * Command: type user:23:score
 * Result: string
 */
$commandResult = $redisClient->type("user:23:score");

echo "Command: type user:23:score | Result: " . $commandResult . "\n";


/**
 * Check if some key named "unknownkey" exists
 * it does not exist yet
 *
 * Command: get unknownkey
 * Result: (nil)
 */
$commandResult = $redisClient->get("unknownkey");

echo "Command: get unknownkey | Result: " . $commandResult . "\n";

/**
 * Try to decreament the value of "unknownkey" using decr command
 * The value of "unknownkey" is decreamented to 1
 *
 * Command: decr unknownkey
 * Result: (integer) -1
 */
$commandResult = $redisClient->decr("unknownkey");

echo "Command: decr unknownkey | Result: " . $commandResult . "\n";

/**
 * Check the value of "unknownkey"
 *
 * Command: get unknownkey
 * Result: "-1"
 */
$commandResult = $redisClient->get("unknownkey");

echo "Command: get unknownkey | Result: " . $commandResult . "\n";


/**
 * Set a string vlaue to sitename key
 *
 * Command: set sitename bigboxcode
 * Result: OK
 */
$commandResult = $redisClient->set("sitename", "bigboxcode");

echo "Command: set sitename bigboxcode | Result: " . $commandResult . "\n";

/**
 * Try to apply DECR command to sitename
 * We get an error as the value in sitename key is not an integer
 *
 * Command: decr sitename
 * Result: (error) ERR value is not an integer or out of range
 */
try {
    $commandResult = $redisClient->decr("sitename");

    echo "Command: decr sitename | Result: " . $commandResult . "\n";
} catch (\Exception $e) {
    echo "Command: decr sitename | Error: " . $e->getMessage() . "\n";
}


/**
 * Max value of allowed integer for 64-bit integer is 9,223,372,036,854,775,807
 * Let's set the value of key "mymaxtest" to a value more than that
 *
 * Command: set mymaxtest 9223372036854775810
 * Result: OK
 */
$commandResult = $redisClient->set("mymaxtest", "9223372036854775810");

echo "Command: set mymaxtest 9223372036854775810 | Result: " . $commandResult . "\n";

/**
 * Let's decreament the vlaue of "mymaxtest"
 * We get an error
 *
 * Command: decr mymaxtest
 * Result: (error) ERR value is not an integer or out of range
 */
try {
    $commandResult = $redisClient->decr("mymaxtest");

    echo "Command: decr mymaxtest | Result: " . $commandResult . "\n";
} catch (\Exception $e) {
    echo "Command: decr mymaxtest | Error: " . $e->getMessage() . "\n";
}


/**
 * Min value allowed as 64-bit int is -9,223,372,036,854,775,808
 * Lets set a value close to that, -9,223,372,036,854,775,807
 *
 * Command: set mymintest  -9223372036854775807
 * Result: OK
 */
$commandResult = $redisClient->set("mymintest", "-9223372036854775807");

echo "Command: set mymintest  -9223372036854775807 | Result: " . $commandResult . "\n";

/**
 * Try to decr the value, it will work as it is still in range
 *
 * Command: decr mymintest
 * Result: (integer) -9223372036854775808
 */
$commandResult = $redisClient->decr("mymintest");

echo "Command: decr mymintest | Result: " . $commandResult . "\n";

/**
 * If we try to decrease once again we get error
 *
 * Command: decr mymintest
 * Result: (error) ERR increment or decrement would overflow
 */
try {
    $commandResult = $redisClient->decr("mymintest");

    echo "Command: decr mymintest | Result: " . $commandResult . "\n";
} catch (\Exception $e) {
    echo "Command: decr mymintest | Error: " . $e->getMessage() . "\n";
}

Output:

Command: set user:23:score 85 | Result: OK

Command: decr user:23:score | Result: 84
Command: get user:23:score | Result: 84
Command: type user:23:score | Result: string

Command: get unknownkey | Result:

Command: decr unknownkey | Result: -1
Command: get unknownkey | Result: -1

Command: set sitename bigboxcode | Result: OK

Command: decr sitename | Error: ERR value is not an integer or out of range
Command: set mymaxtest 9223372036854775810 | Result: OK

Command: decr mymaxtest | Error: ERR value is not an integer or out of range

Command: set mymintest  -9223372036854775807 | Result: OK

Command: decr mymintest | Result: -9223372036854775808
Command: decr mymintest | Error: ERR increment or decrement would overflow

Notes

  • Use the method “decr” of predis.
  • Signature of the method is-
    decr(string $key): int
# Redis DECR command example in Python

import redis
import time

# Create Redis client
redisClient = redis.Redis(host='localhost', port=6379,
                          username='default', password='',
                          decode_responses=True)


# Set the value of user:23:score key to 85
# Command: set user:23:score 85
# Result: OK
commandResult = redisClient.set("user:23:score", "85")

print("Command: set user:23:score 85 | Result: {}".format(commandResult))

# decreament value of user:23:score
# Command: decr user:23:score
# Result: (integer) 84
commandResult = redisClient.decr("user:23:score")

print("Command: decr user:23:score | Result: {}".format(commandResult))

# Check value of user:23:score key
# Command: get user:23:score
# Result: "84"
commandResult = redisClient.get("user:23:score")

print("Command: get user:23:score | Result: {}".format(commandResult))

# Check type of user:23:score
# Command: type user:23:score
# Result: string
commandResult = redisClient.type("user:23:score")

print("Command: type user:23:score | Result: {}".format(commandResult))


# Check if some key named "unknownkey" exists
# it does not exist yet
# Command: get unknownkey
# Result: (nil)
commandResult = redisClient.get("unknownkey")

print("Command: get unknownkey | Result: {}".format(commandResult))

# Try to decreament the value of "unknownkey" using decr command
# The value of "unknownkey" is decreamented to 1
# Command: decr unknownkey
# Result: (integer) -1
commandResult = redisClient.decr("unknownkey")

print("Command: decr unknownkey | Result: {}".format(commandResult))

# Check the value of "unknownkey"
# Command: get unknownkey
# Result: "-1"
commandResult = redisClient.get("unknownkey")

print("Command: get unknownkey | Result: {}".format(commandResult))


# Set a string vlaue to sitename key
# Command: set sitename bigboxcode
# Result: OK
commandResult = redisClient.set("sitename", "bigboxcode")

print("Command: set sitename bigboxcode | Result: {}".format(commandResult))

# Try to apply DECR command to sitename
# We get an error as the value in sitename key is not an integer
# Command: decr sitename
# Result: (error) ERR value is not an integer or out of range
try:
    commandResult = redisClient.decr("sitename")

    print("Command: decr sitename | Result: {}".format(commandResult))
except Exception as error:
    print("Command: decr sitename | Error: ", error)


# Max value of allowed integer for 64-bit integer is 9,223,372,036,854,775,807
# Let's set the value of key "mymaxtest" to a value more than that
# Command: set mymaxtest 9223372036854775810
# Result: OK
commandResult = redisClient.set("mymaxtest", "9223372036854775810")

print("Command: set mymaxtest 9223372036854775810 | Result: {}".format(commandResult))

# Let's decreament the vlaue of "mymaxtest"
# We get an error
# Command: decr mymaxtest
# Result: (error) ERR value is not an integer or out of range
try:
    commandResult = redisClient.decr("mymaxtest")

    print("Command: decr mymaxtest | Result: {}".format(commandResult))
except Exception as error:
    print("Command: decr mymaxtest | Error: ", error)


# Min value allowed as 64-bit int is -9,223,372,036,854,775,808
# Lets set a value close to that, -9,223,372,036,854,775,807
# Command: set mymintest  -9223372036854775807
# Result: OK
commandResult = redisClient.set("mymintest", "-9223372036854775807")

print("Command: set mymintest  -9223372036854775807 | Result: {}".format(commandResult))

# Try to decr the value, it will work as it is still in range
# Command: decr mymintest
# Result: (integer) -9223372036854775808
commandResult = redisClient.decr("mymintest")

print("Command: decr mymintest | Result: {}".format(commandResult))

# If we try to decrease once again we get error
# Command: decr mymintest
# Result: (error) ERR increment or decrement would overflow
try:
    commandResult = redisClient.decr("mymintest")

    print("Command: decr mymintest | Result: {}".format(commandResult))
except Exception as error:
    print("Command: decr mymintest | Error: ", error)

Output:

Command: set user:23:score 85 | Result: True

Command: decr user:23:score | Result: 84
Command: get user:23:score | Result: 84
Command: type user:23:score | Result: string

Command: get unknownkey | Result: None

Command: decr unknownkey | Result: -1
Command: get unknownkey | Result: -1

Command: set sitename bigboxcode | Result: True

Command: decr sitename | Error:  value is not an integer or out of range

Command: set mymaxtest 9223372036854775810 | Result: True

Command: decr mymaxtest | Error:  value is not an integer or out of range

Command: set mymintest  -9223372036854775807 | Result: True

Command: decr mymintest | Result: -9223372036854775808
Command: decr mymintest | Error:  increment or decrement would overflow

Notes

  • Use method “decr” from redis-py, for the Redis DECR command usage.
  • In the redis-py package decr and decrby has the same definition. decr is defined as-
    decr = decrby
    Here is the definition of incrby method-
    def decrby(self, name: KeyT, amount: int = 1) -> ResponseT
    if we just ignore passing the last param “amount” then we will active the behavior of “incr”.
# Redis DECR command example in Ruby

require 'redis'

redis = Redis.new(host: "localhost", port: 6379)


# Set the value of user:23:score key to 85
# Command: set user:23:score 85
# Result: OK
commandResult = redis.set("user:23:score", "85")

print("Command: set user:23:score 85 | Result: ", commandResult, "\n")

# decreament value of user:23:score
# Command: decr user:23:score
# Result: (integer) 84
commandResult = redis.decr("user:23:score")

print("Command: decr user:23:score | Result: ", commandResult, "\n")

# Check value of user:23:score key
# Command: get user:23:score
# Result: "84"
commandResult = redis.get("user:23:score")

print("Command: get user:23:score | Result: ", commandResult, "\n")

# Check type of user:23:score
# Command: type user:23:score
# Result: string
commandResult = redis.type("user:23:score")

print("Command: type user:23:score | Result: ", commandResult, "\n")


# Check if some key named "unknownkey" exists
# it does not exist yet
# Command: get unknownkey
# Result: (nil)
commandResult = redis.get("unknownkey")

print("Command: get unknownkey | Result: ", commandResult, "\n")

# Try to decreament the value of "unknownkey" using decr command
# The value of "unknownkey" is decreamented to 1
# Command: decr unknownkey
# Result: (integer) -1
commandResult = redis.decr("unknownkey")

print("Command: decr unknownkey | Result: ", commandResult, "\n")

# Check the value of "unknownkey"
# Command: get unknownkey
# Result: "-1"
commandResult = redis.get("unknownkey")

print("Command: get unknownkey | Result: ", commandResult, "\n")


# Set a string vlaue to sitename key
# Command: set sitename bigboxcode
# Result: OK
commandResult = redis.set("sitename", "bigboxcode")

print("Command: set sitename bigboxcode | Result: ", commandResult, "\n")

# Try to apply DECR command to sitename
# We get an error as the value in sitename key is not an integer
# Command: decr sitename
# Result: (error) ERR value is not an integer or out of range
begin
    commandResult = redis.decr("sitename")

    print("Command: decr sitename | Result: ", commandResult, "\n")
rescue => e
    print("Command: decr sitename | Error: ", e, "\n")
end


# Max value of allowed integer for 64-bit integer is 9,223,372,036,854,775,807
# Let's set the value of key "mymaxtest" to a value more than that
# Command: set mymaxtest 9223372036854775810
# Result: OK
commandResult = redis.set("mymaxtest", "9223372036854775810")

print("Command: set mymaxtest 9223372036854775810 | Result: ", commandResult, "\n")

# Let's decreament the vlaue of "mymaxtest"
# We get an error
# Command: decr mymaxtest
# Result: (error) ERR value is not an integer or out of range
begin
    commandResult = redis.decr("mymaxtest")

    print("Command: decr mymaxtest | Result: ", commandResult, "\n")
rescue => e
    print("Command: decr mymaxtest | Error: ", e, "\n")
end


# Min value allowed as 64-bit int is -9,223,372,036,854,775,808
# Lets set a value close to that, -9,223,372,036,854,775,807
# Command: set mymintest  -9223372036854775807
# Result: OK
commandResult = redis.set("mymintest", "-9223372036854775807")

print("Command: set mymintest  -9223372036854775807 | Result: ", commandResult, "\n")

# Try to decr the value, it will work as it is still in range
# Command: decr mymintest
# Result: (integer) -9223372036854775808
commandResult = redis.decr("mymintest")

print("Command: decr mymintest | Result: ", commandResult, "\n")

# If we try to decrease once again we get error
# Command: decr mymintest
# Result: (error) ERR increment or decrement would overflow
begin
    commandResult = redis.decr("mymintest")

    print("Command: decr mymintest | Result: ", commandResult, "\n")
rescue => e
    print("Command: decr mymintest | Error: ", e, "\n")
end

Output:

Command: set user:23:score 85 | Result: OK

Command: decr user:23:score | Result: 84
Command: get user:23:score | Result: 84
Command: type user:23:score | Result: string

Command: get unknownkey | Result: 

Command: decr unknownkey | Result: -1
Command: get unknownkey | Result: -1

Command: set sitename bigboxcode | Result: OK

Command: decr sitename | Error: ERR value is not an integer or out of range

Command: set mymaxtest 9223372036854775810 | Result: OK

Command: decr mymaxtest | Error: ERR value is not an integer or out of range

Command: set mymintest  -9223372036854775807 | Result: OK

Command: decr mymintest | Result: -9223372036854775808
Command: decr mymintest | Error: ERR increment or decrement would overflow

Notes

  • Use method “decr” from the redis-rb.
  • Signature of the “decr” method is-
    def decr(key)

Source Code

Use the following links to get the source code used in this article-

Related Commands

CommandDetails
INCR Command Details
INCRBY Command Details
INCRBYFLOAT Command Details
DECRBY Command Details

Leave a Comment


The reCAPTCHA verification period has expired. Please reload the page.