Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Redis · Module

DECRBY

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.

How it works

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.

Syntax

DECRBY key decrement

Parameters

NameTypeDescription
keykeyThe name of the key.
decrementintegerThe integer amount to subtract from the value.

Returns

An integer reply containing the value of the key after decrementing it.

Key points

  • It subtracts the requested integer amount from the value associated with the key.
  • The decrement argument is an integer subtraction amount.
  • The result is the updated integer value, not the amount subtracted.
  • For example, decrementing a value of 10 by 3 produces 7.
  • Use it for counters whose values fit within signed 64-bit integer limits.

Common mistakes

  • A missing key is treated as zero before the decrement.
  • The value must be an integer-compatible string; otherwise the command errors.
  • Values and results are constrained to signed 64-bit integer range.

Example: Decrement an integer value

SET inventory 10
DECRBY inventory 3
GET inventory

Stores an integer string, subtracts three atomically, then reads the resulting value.

When to use it

Decrement a resource counter

Track remaining inventory, credits, or quotas by subtracting consumed units from an integer counter.

Record consumption

Maintain a retry or allowance counter that decreases whenever an operation is consumed.

Apply atomic counter changes

Update a shared numeric counter with a single Redis command.

Interview questions

QWhat does DECRBY do when the key does not exist?

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.

QWhat are DECRBY's arguments and return 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.

QWhen does DECRBY return an error?

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.

  • DECR
  • INCRBY
  • INCR
  • GET
  • SET

Source: DECRBY | Docs - Redis. Checked 2026-10-16.