w3resource

Redis String: DECR

Redis DECR Command

Redis DECR command is used to decrement the integer value of a key by one. If the key does not exist, it is set to 0 before performing the operation. If the key contains a value of the wrong type or contains a string that can not be represented as integer an error is returned.

Here are some common uses and scenarios for the "DECR" command:

  • Counters: Implement decreasing counters (e.g., inventory management, countdowns).
  • Batch Processing: Decrease values of multiple keys efficiently.
  • Atomic Operations: Ensure atomicity when decrementing values in a multi-client environment.
  • Session Management: Use for generating unique session IDs or sequence numbers in reverse order.
  • Data Adjustment: Adjust integer values downward as needed.

Syntax:

DECR KEY_NAME

Available since

1.0.0.

Return Value

Integer reply, the value of key after the increment

Return Value Type

Integer

Example: Redis DECR

redis 127.0.0.1:6379> SET visitors 1000
OK
redis 127.0.0.1:6379> DECR visitors
(integer) 999
redis 127.0.0.1:6379> SET visitors "13131312312312312312312rgergerg"
Ok
redis 127.0.0.1:6379> DECR visitors
ERR value is not an integer or out of range

Previous: INCRBYFLOAT
Next: DECRBY



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/redis/redis-decr-key.php