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
- 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
- Maintaining Rankings or Scores
For a leaderboard system, increment scores whenever a user completes an action:
INCRBY user:1:score 50
- 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 keyincreases a key’s value by 1.DECR keydecreases a key’s value by 1.INCRBY key amountincreases a key’s value by a specified integer.DECRBY key amountdecreases a key’s value by a specified integer.INCRBYFLOAT key float_amountincreases 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
| Command | Description | Example Output |
|---|---|---|
INCR visits | Increase by 1 | 2 |
DECR visits | Decrease by 1 | 1 |
INCRBY visits 20 | Increase by 20 | 21 |
DECRBY visits 5 | Decrease by 5 | 16 |
INCRBYFLOAT price 2.3 | Increase float value by 2.3 | 3.3 |
INCRBYFLOAT price -0.5 | Decrease float value by 0.5 | 2.8 |
