Decrement a resource counter
Track remaining inventory, credits, or quotas by subtracting consumed units from an integer counter.
Redis · Module
DECRBY subtracts an integer amount from a key's stored integer value and returns the updated value. Missing keys start at zero, while non-integer values and out-of-range results cause errors.
DECRBY subtracts the specified integer from the value stored at a key and returns the updated value. A missing key starts at zero; the command fails when the stored value is non-integer or the result cannot fit in a signed 64-bit integer.
DECRBY key decrement| Name | Type | Description |
|---|---|---|
key | key | The name of the key. |
decrement | integer | The integer amount to subtract from the value. |
An integer reply containing the value of the key after decrementing it.
SET inventory 10
DECRBY inventory 3
GET inventoryStores an integer string, subtracts three atomically, then reads the resulting value.
Track remaining inventory, credits, or quotas by subtracting consumed units from an integer counter.
Maintain a retry or allowance counter that decreases whenever an operation is consumed.
Update a shared numeric counter with a single Redis command.
DECRBY subtracts the supplied decrement from the integer value at a key and returns the value after subtraction. If the key is absent, Redis first treats it as zero.
Follow-up: What happens with a non-integer value? Redis returns an error rather than coercing the value.
The command accepts a key and an integer decrement amount. Its integer reply is the key's value after decrementing.
Follow-up: What numeric boundary applies? The operation is limited to signed 64-bit integers.
DECRBY requires the stored value to be an integer-compatible value. Redis returns an error if the value has the wrong type or cannot be represented as an integer.
Follow-up: How would you initialize a counter safely? Store an integer string, or rely on the absent-key zero behavior before applying the decrement.
DECRINCRBYINCRGETSET