0
0
Redisquery~10 mins

SCAN for safe key iteration in Redis - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - SCAN for safe key iteration
Start with cursor = 0
Call SCAN cursor MATCH pattern COUNT n
Receive new cursor and keys batch
Process keys batch
Is cursor == 0?
NoRepeat SCAN with new cursor
Yes
End iteration
SCAN starts with cursor 0, returns a batch of keys and a new cursor. Repeat until cursor returns to 0, safely iterating keys without blocking.
Execution Sample
Redis
SCAN 0 MATCH user:* COUNT 2
SCAN 3 MATCH user:* COUNT 2
SCAN 0 MATCH user:* COUNT 2
This example shows scanning keys matching 'user:*' in batches of 2, iterating safely until cursor returns to 0.
Execution Table
StepInput CursorCommandReturned CursorKeys ReturnedAction
10SCAN 0 MATCH user:* COUNT 23[user:1, user:2]Process keys user:1, user:2
23SCAN 3 MATCH user:* COUNT 20[user:3]Process key user:3
30End--Cursor 0 means iteration complete
💡 Cursor returned 0 at step 2, so iteration ends safely without blocking.
Variable Tracker
VariableStartAfter Step 1After Step 2Final
cursor0300
keys_batch[][user:1, user:2][user:3][]
Key Moments - 3 Insights
Why do we start SCAN with cursor 0?
SCAN always starts with cursor 0 to begin iteration from the start. See execution_table step 1 where input cursor is 0.
What does it mean when SCAN returns cursor 0?
Cursor 0 means the iteration is complete and no more keys remain. See execution_table step 2 where returned cursor is 0, ending iteration.
Why is SCAN safer than KEYS for large datasets?
SCAN returns keys in small batches without blocking Redis, unlike KEYS which blocks. This is shown by multiple steps returning partial keys in execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the cursor value after step 1?
A2
B3
C0
D1
💡 Hint
Check the 'Returned Cursor' column in execution_table row for step 1.
At which step does the SCAN command indicate the iteration is complete?
AStep 3
BStep 1
CStep 2
DStep 4
💡 Hint
Look for the step where 'Returned Cursor' is 0 in execution_table.
If COUNT was increased to 5, how would the keys returned per step change?
AMore keys per batch, fewer steps
BFewer keys per batch, more steps
CNo change in keys per batch
DSCAN would return all keys at once
💡 Hint
COUNT controls batch size, see how keys returned relate to COUNT in execution_table.
Concept Snapshot
SCAN command iterates keys safely without blocking.
Start with cursor 0.
Each SCAN returns a new cursor and a batch of keys.
Repeat SCAN with new cursor until cursor returns 0.
Use MATCH to filter keys and COUNT to control batch size.
Full Transcript
The SCAN command in Redis allows safe iteration over keys without blocking the server. It starts with cursor 0 and returns a batch of keys plus a new cursor. You process the keys returned, then call SCAN again with the new cursor. When SCAN returns cursor 0, the iteration is complete. This method is safer than using KEYS because it does not block Redis and handles large datasets efficiently. You can use MATCH to filter keys and COUNT to control how many keys are returned per batch.