Introduction
SCAN helps you look through all keys in Redis without freezing the database. It is safer than getting all keys at once.
SCAN cursor [MATCH pattern] [COUNT count]
SCAN 0SCAN 0 MATCH user:*SCAN 0 COUNT 100
SCAN 0 MATCH session:* COUNT 10
SCAN over KEYS in Redis for iterating keys?KEYS behaviorKEYS returns all matching keys at once and blocks Redis during execution.SCAN behaviorSCAN returns keys in small batches without blocking Redis, allowing safe iteration.SCAN does not block Redis and returns keys in batches. -> Option DSCAN non-blocking [OK]SCAN is non-blocking, KEYS blocks [OK]SCAN returns all keys at onceSCAN with key deletionSCAN only works on sorted setsSCAN iteration from the beginning in Redis CLI?SCAN command requires a cursor argument; starting cursor is always 0.SCAN 0 starts iteration from the beginning; other options are invalid.SCAN with cursor 0 [OK]SCAN command?
SET user:1 Alice SET user:2 Bob SET admin:1 Carol SCAN 0 MATCH user:*
SCAN with MATCHMATCH user:* filters keys starting with 'user:'. So keys 'user:1' and 'user:2' match; 'admin:1' does not.SCAN output formatSCAN 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.SCAN 0 MATCH user:*But you notice it returns only a partial list of keys matching the pattern. What is the likely mistake?
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.SCAN 0 MATCH user:* only once returns partial keys; not looping causes incomplete results.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?MATCH session:* and COUNT 10.SCAN cursor MATCH session:* COUNT 10 until cursor returns 0. -> Option CSCAN cursor MATCH session:* COUNT 10 until cursor returns 0. [OK]Pass the test and this pattern moves unknown