0
0
Redisquery~3 mins

Why INCRBY and DECRBY in Redis? - Purpose & Use Cases

Choose your learning style9 modes available
The Big Idea

What if you could update scores instantly without any chance of mistakes?

The Scenario

Imagine you have a list of scores for a game stored in a notebook. Every time a player scores, you have to find their score and add the points manually. If the player loses points, you have to subtract them by hand. This gets confusing and slow as the game goes on and the list grows.

The Problem

Manually updating scores is slow and easy to mess up. You might write the wrong number, forget to update a score, or lose track of who has what points. It's hard to keep everything accurate and up-to-date, especially when many players are involved.

The Solution

INCRBY and DECRBY commands let you add or subtract numbers directly in the database without reading or rewriting the whole value. This means the database handles the math safely and quickly, so you never lose track or make mistakes.

Before vs After
Before
score = get_score(player)
new_score = score + points
set_score(player, new_score)
After
INCRBY player points
DECRBY player points
What It Enables

It makes updating numbers fast, safe, and easy, even when many users change data at the same time.

Real Life Example

In a live online game, players earn or lose points constantly. Using INCRBY and DECRBY keeps scores accurate instantly without slowing down the game.

Key Takeaways

Manual updates are slow and error-prone.

INCRBY and DECRBY update numbers directly in the database.

This keeps data accurate and operations fast.