What if you could count things perfectly without lifting a finger?
Why INCR and DECR for counters in Redis? - Purpose & Use Cases
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.
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.
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.
count = count + 1 # manually increase counter
INCR visits # Redis command to increase counterYou can track live counts easily and reliably without mistakes or delays.
Counting how many times a button is clicked on a website in real time.
Manual counting is slow and error-prone.
INCR and DECR automate safe counting in Redis.
This makes live tracking simple and reliable.
Practice
INCR mycounter do if mycounter does not exist yet?Solution
Step 1: Understand INCR behavior on missing keys
WhenINCRis called on a key that does not exist, Redis treats it as 0 before incrementing.Step 2: Apply increment to initial value
Incrementing 0 by 1 results in 1, so Redis setsmycounterto 1.Final Answer:
It createsmycounterwith value 1. -> Option AQuick Check:
INCR on missing key = 1 [OK]
- Thinking INCR returns error if key missing
- Assuming key starts at 0 after INCR
- Confusing INCR with setting key manually
pageviews by 1?Solution
Step 1: Recall Redis command for decrement by 1
The correct Redis command to decrease a key by 1 isDECR key.Step 2: Check options for correct syntax
DECR pageviews usesDECR 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.Final Answer:
DECR pageviews -> Option BQuick Check:
Use DECR key to subtract 1 [OK]
- Using DECREMENT instead of DECR
- Trying INCR with negative number
- Confusing DECR and DECRBY syntax
INCR visits
INCR visits
DECR visits
INCR visits
What is the final value of the key
visits?Solution
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 -> 2Step 2: Recalculate carefully
Re-check: Actually, after DECR visits from 2, visits = 1.
Then INCR visits from 1, visits = 2.Final Answer:
2 -> Option DQuick Check:
INCR twice, DECR once, INCR once = 2 [OK]
- Adding all INCRs without subtracting DECR
- Assuming visits starts at 1
- Mixing order of commands
DECR mycount on a Redis key mycount that currently holds the string value "hello". What will happen?Solution
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.Step 2: Apply to given value
Sincemycountholds "hello", which is not an integer, Redis returns an error.Final Answer:
Redis will return an error because the value is not an integer. -> Option CQuick Check:
DECR on non-integer string = error [OK]
- Assuming Redis converts strings to 0 automatically
- Expecting string concatenation
- Thinking DECR silently fails
Solution
Step 1: Understand counting active users
Increment on login is simple withINCR. Decrement on logout must avoid negative counts.Step 2: Prevent negative counts on logout
Before callingDECR, check ifactive_users> 0 to avoid going below zero.Final Answer:
Use INCR on login and check value > 0 before DECR on logout. -> Option AQuick Check:
Check before DECR to avoid negatives [OK]
- Not checking before DECR causes negative counts
- Deleting key on logout resets count incorrectly
- Assuming DECRBY and DECR behave differently here
Ready to prove it?
Pass the test and this pattern moves unknown
