0
0
Redisquery~20 mins

SCAN for safe key iteration in Redis - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Redis SCAN Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2: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?

SCAN 0 MATCH user:*

Assume the database is static during the scan.
Redis
SCAN 0 MATCH user:*
AA cursor and a list containing keys starting with 'user:' such as ['user:1', 'user:2']
BAll keys in the database regardless of pattern
CAn error because SCAN does not accept MATCH
DOnly the key 'user:*' if it exists
Attempts:
2 left
💡 Hint
Remember SCAN returns a cursor and a list of keys matching the pattern.
🧠 Conceptual
intermediate
2: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?
ASCAN deletes keys as it scans, freeing memory
BSCAN returns all keys at once, making it faster than KEYS
CSCAN uses a cursor and returns keys incrementally, avoiding blocking the server
DSCAN only works on sorted sets, making it more efficient
Attempts:
2 left
💡 Hint
Think about server load and blocking behavior.
📝 Syntax
advanced
2: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.
ASCAN 0 MATCH cache:* COUNT 50
BSCAN MATCH cache:* COUNT 50 0
CSCAN 0 COUNT 50 MATCH cache:*
DSCAN 0 MATCH cache:* LIMIT 50
Attempts:
2 left
💡 Hint
The order of MATCH and COUNT matters.
optimization
advanced
2: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?
AUse SCAN without MATCH to get all keys faster
BUse KEYS command instead for faster results
CUse SCAN with a very large COUNT value to get all keys quickly
DUse SCAN with a small COUNT value and pause between calls
Attempts:
2 left
💡 Hint
Think about balancing speed and server load.
🔧 Debug
expert
3:00remaining
Why does this SCAN loop miss some keys?
A developer uses this loop to scan all keys matching 'data:*':

cursor = 0 keys = [] do { result = redis.scan(cursor, match='data:*') cursor = result[0] keys.extend(result[1]) } while cursor != 0

Some 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
AThe MATCH pattern is incorrect and filters out keys
BKeys were added or deleted during the scan causing inconsistent results
CThe loop should stop when cursor equals 1, not 0
DSCAN does not return all keys by design, so missing keys are normal
Attempts:
2 left
💡 Hint
Think about what happens if keys change during iteration.