INCR increases the integer value of a key by 1. If the key does not exist, it is set to 0 before incrementing.
Click to reveal answer
beginner
What happens if you use DECR on a key that does not exist in Redis?
The key is set to 0 first, then decreased by 1, resulting in a value of -1.
Click to reveal answer
intermediate
Can INCR and DECR be used on keys holding non-integer values?
No. INCR and DECR only work on keys holding integer values. Using them on non-integer values causes an error.
Click to reveal answer
intermediate
How would you atomically increase a counter by 5 in Redis?
Use the INCRBY command with the key and 5 as arguments, e.g., INCRBY key 5.
Click to reveal answer
beginner
Why are INCR and DECR commands useful for counters in Redis?
They provide a simple, fast, and atomic way to increase or decrease counters without race conditions.
Click to reveal answer
What is the result of INCR on a key that does not exist?
ADoes nothing
BReturns an error
CSets the key to 0
DSets the key to 1
✗ Incorrect
INCR sets the key to 0 first, then increments it by 1, so the final value is 1.
Which command decreases the value of a Redis key by 1?
AINCR
BDECR
CINCRBY
DDECRBY
✗ Incorrect
DECR decreases the integer value of a key by 1.
What happens if you run INCR on a key holding the string 'hello'?
AAn error is returned
BThe value becomes 1
CThe value becomes 'hello1'
DThe key is deleted
✗ Incorrect
INCR only works on integer values; using it on a non-integer string causes an error.
How can you increase a Redis counter by 10 in one command?
AINCR key 10
BDECRBY key 10
CINCRBY key 10
DINCR key; INCR key; ... (10 times)
✗ Incorrect
INCRBY key 10 increases the key's value by 10 atomically.
Why is using INCR and DECR commands better than getting and setting values manually?
AThey are atomic and avoid race conditions
BThey are slower but safer
CThey use less memory
DThey allow floating point increments
✗ Incorrect
INCR and DECR are atomic operations, so they prevent race conditions in concurrent environments.
Explain how INCR and DECR commands work in Redis and why they are useful for counters.
Think about how counters change and why atomicity matters.
You got /5 concepts.
Describe what happens if you use INCR or DECR on a key with a non-integer value in Redis.
Consider data types and command restrictions.
You got /4 concepts.
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
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.
Step 2: Apply increment to initial value
Incrementing 0 by 1 results in 1, so Redis sets mycounter to 1.
Final Answer:
It creates mycounter with value 1. -> Option A
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
Step 1: Recall Redis command for decrement by 1
The correct Redis command to decrease a key by 1 is DECR key.
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.
Final Answer:
DECR pageviews -> Option B
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
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
Step 2: Recalculate carefully
Re-check: Actually, after DECR visits from 2, visits = 1. Then INCR visits from 1, visits = 2.
Final Answer:
2 -> Option D
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
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
Since mycount holds "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 C
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
Step 1: Understand counting active users
Increment on login is simple with INCR. Decrement on logout must avoid negative counts.
Step 2: Prevent negative counts on logout
Before calling DECR, check if active_users > 0 to avoid going below zero.
Final Answer:
Use INCR on login and check value > 0 before DECR on logout. -> Option A
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 unknownstrongwhile earning +1 Grip toward your certificate.