Bird
Raised Fist0
Redisquery~3 mins

Why INCR and DECR for counters in Redis? - Purpose & Use Cases

Choose your learning style10 modes available
The Big Idea

What if you could count things perfectly without lifting a finger?

The Scenario

Imagine you are tracking how many people visit your website by writing down each visit on a piece of paper.

Every time someone visits, you add one mark manually.

The Problem

This manual counting is slow and easy to mess up.

You might lose track, count twice, or forget to add a mark.

It becomes a big headache when many people visit quickly.

The Solution

Using INCR and DECR commands in Redis lets you automatically add or subtract from a counter.

This happens instantly and safely, even if many users visit at the same time.

Before vs After
✗ Before
count = count + 1  # manually increase counter
✓ After
INCR visits  # Redis command to increase counter
What It Enables

You can track live counts easily and reliably without mistakes or delays.

Real Life Example

Counting how many times a button is clicked on a website in real time.

Key Takeaways

Manual counting is slow and error-prone.

INCR and DECR automate safe counting in Redis.

This makes live tracking simple and reliable.

Practice

(1/5)
1. What does the Redis command INCR mycounter do if mycounter does not exist yet?
easy
A. It creates mycounter with value 1.
B. It returns an error because the key does not exist.
C. It creates mycounter with value 0.
D. It sets mycounter to -1.

Solution

  1. Step 1: Understand INCR behavior on missing keys

    When INCR is called on a key that does not exist, Redis treats it as 0 before incrementing.
  2. Step 2: Apply increment to initial value

    Incrementing 0 by 1 results in 1, so Redis sets mycounter to 1.
  3. Final Answer:

    It creates mycounter with value 1. -> Option A
  4. Quick Check:

    INCR on missing key = 1 [OK]
Hint: INCR on missing key starts at 1 automatically [OK]
Common Mistakes:
  • Thinking INCR returns error if key missing
  • Assuming key starts at 0 after INCR
  • Confusing INCR with setting key manually
2. Which of the following is the correct syntax to decrease a Redis key named pageviews by 1?
easy
A. DECREMENT pageviews
B. DECR pageviews
C. INCR pageviews -1
D. DECR pageviews 1

Solution

  1. Step 1: Recall Redis command for decrement by 1

    The correct Redis command to decrease a key by 1 is DECR key.
  2. Step 2: Check options for correct syntax

    DECR pageviews uses DECR pageviews, which is valid. DECREMENT pageviews is invalid command, C is incorrect syntax, and D is invalid because DECR does not accept an amount argument.
  3. Final Answer:

    DECR pageviews -> Option B
  4. Quick Check:

    Use DECR key to subtract 1 [OK]
Hint: Use DECR key to subtract 1 exactly [OK]
Common Mistakes:
  • Using DECREMENT instead of DECR
  • Trying INCR with negative number
  • Confusing DECR and DECRBY syntax
3. Given the following Redis commands executed in order:
INCR visits
INCR visits
DECR visits
INCR visits

What is the final value of the key visits?
medium
A. 4
B. 3
C. 1
D. 2

Solution

  1. Step 1: Track each command's effect on visits

    Start: visits does not exist, treated as 0.
    INCR visits -> 1
    INCR visits -> 2
    DECR visits -> 1
    INCR visits -> 2
  2. Step 2: Recalculate carefully

    Re-check: Actually, after DECR visits from 2, visits = 1.
    Then INCR visits from 1, visits = 2.
  3. Final Answer:

    2 -> Option D
  4. Quick Check:

    INCR twice, DECR once, INCR once = 2 [OK]
Hint: Count increments and decrements stepwise [OK]
Common Mistakes:
  • Adding all INCRs without subtracting DECR
  • Assuming visits starts at 1
  • Mixing order of commands
4. You run the command DECR mycount on a Redis key mycount that currently holds the string value "hello". What will happen?
medium
A. Redis will convert "hello" to 0 and then decrement to -1.
B. The value will become "hello-1".
C. Redis will return an error because the value is not an integer.
D. The value will remain "hello" unchanged.

Solution

  1. Step 1: Understand DECR command requirements

    DECR expects the key's value to be an integer or missing (treated as 0). A non-integer string causes an error.
  2. Step 2: Apply to given value

    Since mycount holds "hello", which is not an integer, Redis returns an error.
  3. Final Answer:

    Redis will return an error because the value is not an integer. -> Option C
  4. Quick Check:

    DECR on non-integer string = error [OK]
Hint: DECR only works on integer values [OK]
Common Mistakes:
  • Assuming Redis converts strings to 0 automatically
  • Expecting string concatenation
  • Thinking DECR silently fails
5. You want to track the number of active users on a website using Redis. Which sequence of commands correctly increases the count when a user logs in and decreases it when a user logs out, ensuring the count never goes below zero?
hard
A. Use INCR active_users on login and before DECR active_users on logout, check if value > 0.
B. Use INCRBY active_users 1 on login and DECRBY active_users 1 on logout without checks.
C. Use INCR active_users on login and DECR active_users on logout without checks.
D. Use INCR active_users on login and DEL active_users on logout.

Solution

  1. Step 1: Understand counting active users

    Increment on login is simple with INCR. Decrement on logout must avoid negative counts.
  2. Step 2: Prevent negative counts on logout

    Before calling DECR, check if active_users > 0 to avoid going below zero.
  3. Final Answer:

    Use INCR on login and check value > 0 before DECR on logout. -> Option A
  4. Quick Check:

    Check before DECR to avoid negatives [OK]
Hint: Check count > 0 before DECR to prevent negatives [OK]
Common Mistakes:
  • Not checking before DECR causes negative counts
  • Deleting key on logout resets count incorrectly
  • Assuming DECRBY and DECR behave differently here
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard