Bird
Raised Fist0
Redisquery~5 mins

SCAN for safe key iteration in Redis - Cheat Sheet & Quick Revision

Choose your learning style10 modes available
Recall & Review
beginner
What is the purpose of the SCAN command in Redis?
The SCAN command is used to safely iterate over keys in a Redis database without blocking the server or causing performance issues.
Click to reveal answer
beginner
How does SCAN differ from the KEYS command in Redis?
Unlike KEYS, which returns all matching keys at once and can block Redis, SCAN returns a small subset of keys per call, allowing safe, incremental iteration.
Click to reveal answer
intermediate
What does the cursor returned by SCAN represent?
The cursor is a position marker used by SCAN to keep track of the iteration state. You pass it back to SCAN to continue scanning from where you left off.
Click to reveal answer
intermediate
Why is SCAN considered 'safe' for key iteration in Redis?
Because SCAN returns keys in small batches and does not block the server, it allows other operations to continue smoothly, avoiding performance hits.
Click to reveal answer
intermediate
What is a common pattern to use SCAN to iterate all keys matching a pattern?
Start with cursor 0, call SCAN with MATCH pattern and COUNT options repeatedly, updating the cursor each time until it returns 0, collecting keys along the way.
Click to reveal answer
What does the SCAN command return in Redis?
AThe value of a key
BAll keys matching a pattern at once
COnly the total count of keys
DA cursor and a list of keys
Why should you prefer SCAN over KEYS for large Redis databases?
ASCAN blocks the server less and is incremental
BKEYS is faster for large databases
CSCAN returns all keys at once
DKEYS does not support patterns
What does a cursor value of 0 mean when returned by SCAN?
AThe scan failed
BThe iteration is complete
CThere are no keys in the database
DYou should restart scanning
Which option can you use with SCAN to filter keys by pattern?
AFILTER
BWHERE
CMATCH
DLIKE
What is the effect of the COUNT option in SCAN?
AIt suggests how many keys to return per call
BIt limits the total keys returned
CIt sorts the keys
DIt deletes keys after scanning
Explain how to safely iterate over all keys in a Redis database using SCAN.
Think about how SCAN returns partial results and a cursor to continue.
You got /5 concepts.
    Why is SCAN preferred over KEYS for production Redis environments?
    Consider server performance and responsiveness.
    You got /4 concepts.

      Practice

      (1/5)
      1. What is the main advantage of using SCAN over KEYS in Redis for iterating keys?
      easy
      A. SCAN returns all keys at once for faster access.
      B. SCAN only works with sorted sets.
      C. SCAN deletes keys while iterating.
      D. SCAN does not block Redis and returns keys in batches.

      Solution

      1. Step 1: Understand KEYS behavior

        KEYS returns all matching keys at once and blocks Redis during execution.
      2. Step 2: Understand SCAN behavior

        SCAN returns keys in small batches without blocking Redis, allowing safe iteration.
      3. Final Answer:

        SCAN does not block Redis and returns keys in batches. -> Option D
      4. Quick Check:

        Safe iteration = SCAN non-blocking [OK]
      Hint: Remember: SCAN is non-blocking, KEYS blocks [OK]
      Common Mistakes:
      • Thinking SCAN returns all keys at once
      • Confusing SCAN with key deletion
      • Assuming SCAN only works on sorted sets
      2. Which of the following is the correct syntax to start a SCAN iteration from the beginning in Redis CLI?
      easy
      A. SCAN 1
      B. SCAN 0
      C. SCAN *
      D. SCAN start

      Solution

      1. Step 1: Identify the starting cursor for SCAN

        The SCAN command requires a cursor argument; starting cursor is always 0.
      2. Step 2: Check options for correct cursor

        SCAN 0 starts iteration from the beginning; other options are invalid.
      3. Final Answer:

        SCAN 0 -> Option B
      4. Quick Check:

        Start cursor = 0 [OK]
      Hint: Always start SCAN with cursor 0 [OK]
      Common Mistakes:
      • Starting with cursor 1 instead of 0
      • Using wildcard * as cursor
      • Using invalid strings like 'start'
      3. Given the following Redis commands executed sequentially, what will be the output of the last SCAN command?
      SET user:1 Alice
      SET user:2 Bob
      SET admin:1 Carol
      SCAN 0 MATCH user:*
      medium
      A. [cursor, ["user:1", "user:2"]]
      B. [0, ["user:1", "user:2"]]
      C. [cursor, ["user:1", "user:2", "admin:1"]]
      D. [0, ["admin:1"]]

      Solution

      1. Step 1: Understand SCAN with MATCH

        The MATCH user:* filters keys starting with 'user:'. So keys 'user:1' and 'user:2' match; 'admin:1' does not.
      2. Step 2: Understand SCAN output format

        SCAN returns a two-element array: a cursor (string) and a list of keys matching the pattern in this batch. Cursor is not necessarily 0 unless iteration is complete.
      3. Final Answer:

        [cursor, ["user:1", "user:2"]] -> Option A
      4. Quick Check:

        Filtered keys = user keys only [OK]
      Hint: MATCH filters keys; cursor is a string, not always 0 [OK]
      Common Mistakes:
      • Expecting cursor 0 on first SCAN call
      • Including keys not matching pattern
      • Confusing cursor with count
      4. You wrote this Redis command to iterate keys safely:
      SCAN 0 MATCH user:*
      But you notice it returns only a partial list of keys matching the pattern. What is the likely mistake?
      medium
      A. Not repeating SCAN with the returned cursor until it returns 0.
      B. Using MATCH with an incorrect pattern.
      C. Starting SCAN with cursor 1 instead of 0.
      D. Using COUNT option with a value too high.

      Solution

      1. Step 1: Understand SCAN iteration process

        SCAN returns a cursor and a batch of keys; to get all keys, you must repeat SCAN with the new cursor until it returns 0.
      2. Step 2: Identify the mistake

        Running SCAN 0 MATCH user:* only once returns partial keys; not looping causes incomplete results.
      3. Final Answer:

        Not repeating SCAN with the returned cursor until it returns 0. -> Option A
      4. Quick Check:

        Repeat SCAN until cursor 0 [OK]
      Hint: Always loop SCAN calls until cursor returns 0 [OK]
      Common Mistakes:
      • Expecting all keys in one SCAN call
      • Misusing MATCH pattern syntax
      • Starting with wrong cursor
      5. You want to safely iterate all keys starting with session: in Redis but avoid blocking the server. You also want to limit each batch to about 10 keys. Which command sequence correctly achieves this?
      hard
      A. Use SCAN 0 COUNT 10 without MATCH.
      B. Run KEYS session:* once to get all keys.
      C. Repeat SCAN cursor MATCH session:* COUNT 10 until cursor returns 0.
      D. Use SCAN 0 MATCH session:* once without COUNT.

      Solution

      1. Step 1: Use SCAN with MATCH and COUNT

        To filter keys starting with 'session:' and limit batch size, use MATCH session:* and COUNT 10.
      2. Step 2: Repeat SCAN calls until cursor is 0

        Iterate by passing the returned cursor each time until it returns 0 to get all keys safely without blocking.
      3. Final Answer:

        Repeat SCAN cursor MATCH session:* COUNT 10 until cursor returns 0. -> Option C
      4. Quick Check:

        Safe iteration + filter + batch size = Repeat SCAN cursor MATCH session:* COUNT 10 until cursor returns 0. [OK]
      Hint: Combine MATCH and COUNT, loop until cursor 0 [OK]
      Common Mistakes:
      • Using KEYS which blocks Redis
      • Not looping SCAN calls
      • Omitting MATCH or COUNT when needed
      Recommended

      Ready to prove it?

      Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

      10 questions~5 mineasy hard