Learnitweb

Incrementing and Decrementing Values in Redis

In this lesson, we will learn how to increment (increase) and decrement (decrease) values in Redis. These commands are extremely useful for scenarios where you need to maintain counters, track visits, or update numerical data without manually reading and writing the values every time.


Setting Up a Key

Before incrementing or decrementing a value, you first need to store it in Redis:

SET visits 1

Here, we have created a key visits with an initial value of 1.


Incrementing Values with INCR

Redis provides the INCR command to increase the value of a key by 1. This is especially convenient because you don’t have to retrieve the value, increment it in your application, and then update it manually.

INCR visits

Output:

(integer) 2

Each time you run INCR visits, the value will increase by 1 automatically. If the key does not exist, Redis will create it and set its value to 1.

Example: Incrementing Multiple Times

INCR visits  # 3
INCR visits  # 4
INCR visits  # 5

Decrementing Values with DECR

Similarly, Redis provides the DECR command to decrease the value of a key by 1.

DECR visits

Output:

(integer) 4

You can use DECR repeatedly to reduce the value as needed.


Incrementing by a Specific Amount with INCRBY

Sometimes you want to increase a value by more than 1. Redis allows you to do this using the INCRBY command:

INCRBY visits 20

Output:

(integer) 24

The same applies for decrementing by a specific value using DECRBY:

DECRBY visits 5

Output:

(integer) 19

Working with Floating-Point Values

By default, Redis can only increment or decrement integer values. If you try to use INCR on a key holding a floating-point value, you will get an error.

For float values, use INCRBYFLOAT:

SET price 1.0
INCRBYFLOAT price 2.3

Output:

"3.3"

You can also decrease a float value:

INCRBYFLOAT price -0.5

Output:

"2.8"

This allows precise numerical adjustments for scenarios like product pricing, account balances, or statistics tracking.


Common Use Cases

  1. Counting Page Visits or Events

You can track the number of times a product page is viewed:

INCR product:100:views  # Each page view increments the counter

Then you can retrieve the total number of views:

GET product:100:views
  1. Maintaining Rankings or Scores

For a leaderboard system, increment scores whenever a user completes an action:

INCRBY user:1:score 50
  1. Working with Floating Values for Prices or Ratings
INCRBYFLOAT product:100:price 1.5  # Increase price
INCRBYFLOAT product:100:price -0.3 # Decrease price

Summary

  • INCR key increases a key’s value by 1.
  • DECR key decreases a key’s value by 1.
  • INCRBY key amount increases a key’s value by a specified integer.
  • DECRBY key amount decreases a key’s value by a specified integer.
  • INCRBYFLOAT key float_amount increases or decreases a key’s value with floating-point precision.
  • If a key does not exist, Redis will create it automatically for integer increments.

These commands are atomic, meaning multiple clients can safely increment or decrement the same key concurrently without conflicts.


Example Table: Increment and Decrement Operations

CommandDescriptionExample Output
INCR visitsIncrease by 12
DECR visitsDecrease by 11
INCRBY visits 20Increase by 2021
DECRBY visits 5Decrease by 516
INCRBYFLOAT price 2.3Increase float value by 2.33.3
INCRBYFLOAT price -0.5Decrease float value by 0.52.8