0
0
Redisquery~5 mins

INCR and DECR for counters in Redis - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the Redis command INCR do?
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
Which command decreases the value of a Redis key by 1?
AINCR
BDECR
CINCRBY
DDECRBY
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
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)
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
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.