Challenge - 5 Problems
Redis Counter Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate1:30remaining
Incrementing a counter in Redis
What will be the value of the key
1.
2.
3.
page_views after running these commands in order?1.
SET page_views 102.
INCR page_views3.
INCRBY page_views 5Redis
SET page_views 10 INCR page_views INCRBY page_views 5
Attempts:
2 left
💡 Hint
Remember that INCR increases by 1 and INCRBY increases by the specified number.
✗ Incorrect
The initial value is 10. INCR adds 1 making it 11. INCRBY adds 5 making it 16.
📝 Syntax
intermediate1:00remaining
Correct syntax to initialize a counter
Which Redis command correctly initializes a counter named
likes to zero?Attempts:
2 left
💡 Hint
To set a key to a value, use SET with key and value.
✗ Incorrect
SET likes 0 sets the key 'likes' to zero. INCR and INCRBY require the key to exist or treat missing as zero but cannot set directly to zero with arguments.
❓ optimization
advanced2:00remaining
Efficiently increment multiple counters
You want to increment counters
counter1, counter2, and counter3 by 1 each in Redis. Which approach is the most efficient?Attempts:
2 left
💡 Hint
Reducing round trips to Redis improves performance.
✗ Incorrect
A Lua script runs atomically and reduces network round trips by incrementing all counters in one call. MULTI/EXEC groups commands but still sends multiple commands. Separate INCR commands cause multiple round trips. INCRBY does not accept multiple keys.
🔧 Debug
advanced1:30remaining
Why does INCR fail on this key?
You run
SET user_count "ten" and then INCR user_count. What error will Redis return?Attempts:
2 left
💡 Hint
INCR expects the key to hold an integer value or not exist.
✗ Incorrect
Since the value is the string "ten", which is not an integer, INCR cannot increment it and returns an error.
🧠 Conceptual
expert2:00remaining
Atomicity of counter increments in Redis
Which statement best describes the atomicity of the INCR command in Redis when multiple clients increment the same counter simultaneously?
Attempts:
2 left
💡 Hint
Redis commands are atomic by design.
✗ Incorrect
Redis processes commands sequentially in a single thread, so INCR commands are atomic and safe from race conditions even with concurrent clients.