Challenge - 5 Problems
Redis SCAN Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
What does the following Redis SCAN command return?
Consider a Redis database with keys: user:1, user:2, session:1, session:2. What will be the output of this command?
Assume the database is static during the scan.
SCAN 0 MATCH user:*Assume the database is static during the scan.
Redis
SCAN 0 MATCH user:*Attempts:
2 left
💡 Hint
Remember SCAN returns a cursor and a list of keys matching the pattern.
✗ Incorrect
The SCAN command returns a cursor and a list of keys matching the pattern. Here, it returns keys starting with 'user:'.
🧠 Conceptual
intermediate2:00remaining
Why is SCAN preferred over KEYS for iterating keys in Redis?
Which reason best explains why SCAN is safer to use than KEYS in a production Redis environment?
Attempts:
2 left
💡 Hint
Think about server load and blocking behavior.
✗ Incorrect
SCAN returns keys incrementally using a cursor, so it does not block the server like KEYS, which returns all keys at once.
📝 Syntax
advanced2:00remaining
Which SCAN command syntax correctly scans keys with a pattern and count?
Select the correct Redis SCAN command to scan keys matching 'cache:*' and return approximately 50 keys per iteration.
Attempts:
2 left
💡 Hint
The order of MATCH and COUNT matters.
✗ Incorrect
The correct syntax is SCAN MATCH COUNT . Option A follows this order.
❓ optimization
advanced2:00remaining
How to optimize SCAN usage to reduce server load?
You want to scan all keys matching 'temp:*' without causing high CPU usage. Which approach is best?
Attempts:
2 left
💡 Hint
Think about balancing speed and server load.
✗ Incorrect
Using a small COUNT and pausing between calls reduces CPU spikes and server load.
🔧 Debug
expert3:00remaining
Why does this SCAN loop miss some keys?
A developer uses this loop to scan all keys matching 'data:*':
Some keys are missing after the loop finishes. What is the most likely cause?
cursor = 0
keys = []
do {
result = redis.scan(cursor, match='data:*')
cursor = result[0]
keys.extend(result[1])
} while cursor != 0Some keys are missing after the loop finishes. What is the most likely cause?
Redis
cursor = 0 keys = [] do { result = redis.scan(cursor, match='data:*') cursor = result[0] keys.extend(result[1]) } while cursor != 0
Attempts:
2 left
💡 Hint
Think about what happens if keys change during iteration.
✗ Incorrect
If keys are added or deleted during SCAN, some keys may be missed or returned multiple times because SCAN is not atomic.