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

Redis · Module

DECR

Decrements the integer value stored at a key by one and returns the updated value. If the key is missing, Redis starts from zero.

Since
1.0.0

How it works

DECR subtracts one from the integer represented by a key's string value and returns the resulting integer. A missing key starts at zero, while a wrong type, non-integer string, or value outside the signed 64-bit range causes an error.

Syntax

DECR key

Parameters

NameTypeDescription
keykeyName of the key whose integer value is decremented.

Returns

Integer reply: the value of the key after decrementing it.

Key points

  • DECR subtracts exactly one from the integer value associated with the supplied key.
  • A missing key is initialized to zero, so the first DECR produces negative one.
  • The required argument identifies which Redis key will be modified.
  • The response is the updated integer, allowing the caller to use the new value immediately.
  • DECR has been available since Redis Open Source 1.0.0.
  • The operation has constant time complexity.

Common mistakes

  • The command fails when the key stores a non-string value or another incompatible Redis type.
  • Only signed 64-bit integer values are supported; larger numeric strings are rejected.

Example: Decrement an integer value

import { createClient } from "redis";

const client = createClient();
await client.connect();

await client.set("stock", "10");
const remaining = await client.decr("stock");
console.log(remaining); // 9

await client.quit();

Connects to Redis, initializes a string integer, decrements it once, and prints the resulting value.

When to use it

Inventory counters

Maintain a remaining quantity, such as available inventory, where each operation consumes one unit.

Capacity tracking

Track a number of available permits or slots and atomically reserve one.

Work and quota counters

Reduce a retry, quota, or pending-work counter by one per processed item.

Interview questions

QWhat does DECR do when the key does not exist?

DECR decrements the integer value stored at a key by one and returns the new value. If the key is absent, Redis treats its initial value as zero.

Follow-up: What happens with a non-numeric string? Redis returns an error because the stored string cannot be represented as an integer.

QWhat is the time complexity of DECR?

The command runs in O(1) time. It performs a single integer update and returns the resulting value.

Follow-up: How would you decrement by an arbitrary amount? Use DECRBY with the key and the desired decrement.

QWhat constraints apply to the value stored at the key?

DECR accepts one required argument, the key name. The key's value must represent a signed 64-bit integer, otherwise Redis returns an error.

Follow-up: What reply should a client expect? An integer reply containing the value after decrementing.

  • DECRBY
  • INCR
  • INCRBY
  • SET
  • GET

Source: DECR. Checked 2026-10-16.