Scan #
8 commands in two families with two different cursor contracts. Getting them mixed up is the most common integration bug with this engine.
| Cmd | RW | Syntax | Returns | Example → reply |
|---|---|---|---|---|
SCAN | R | SCAN cursor [MATCH glob] [COUNT n] [TYPE type] | array [cursor, keys]; cursor 0 ends the walk | SCAN 0 MATCH user:* COUNT 100 → [cursor, [user:1, user:2]] |
HSCAN | R | HSCAN KEY cursor [MATCH glob] [COUNT n] | array [cursor, [field, value, ...]] | HSCAN user:1 0 COUNT 10 → [0, [name, ada]] |
SSCAN | R | SSCAN KEY cursor [MATCH glob] [COUNT n] | array [cursor, [member, ...]] | SSCAN tags 0 → [0, [go]] |
ZSCAN | R | ZSCAN KEY cursor [MATCH glob] [COUNT n] | array [cursor, [member, score, ...]] | ZSCAN board 0 → [0, [ada, 105]] |
XSCAN | R | XSCAN type cursor [MATCH regex] [COUNT n] [ASC|DESC] | array [cursor, keys]; empty cursor ends the walk | XSCAN KV "" COUNT 5 → [key:5, [...]] |
XHSCAN | R | XHSCAN KEY cursor [MATCH regex] [COUNT n] [ASC|DESC] | array [cursor, [field, value, ...]] | XHSCAN user:1 "" COUNT 5 → [name, [name, ada]] |
XSSCAN | R | XSSCAN KEY cursor [MATCH regex] [COUNT n] [ASC|DESC] | array [cursor, [member, ...]] | XSSCAN tags "" → [go, [go]] |
XZSCAN | R | XZSCAN KEY cursor [MATCH regex] [COUNT n] [ASC|DESC] | array [cursor, [member, score, ...]] | XZSCAN board "" → [ada, [ada, 105]] |
The Redis family #
SCAN / HSCAN / SSCAN / ZSCAN follow Redis’ shape but not Redis’ cursor encoding.
- Start at cursor
0; the walk is over when the reply’s cursor is0. SCAN’s cursor is an opaque base64 string that carries five sub-cursors, one per keyspace (string,hash,list,set,zset). It is not a number and not a key offset.HSCAN/SSCAN/ZSCANanswer the last field or member name as the cursor. Consequences:- A field literally named
0collides with the end marker. - Client helpers that parse the cursor as an integer (go-redis’ typed
HScan, for example) reject it. Use the generic command API:redis-cli,Do("hscan", …),execute_command.
- A field literally named
MATCHtakes a Redis glob (*,?,[abc],\\escape), translated to a regexp and applied by the handler over a bounded page.SCANsupportsTYPE string|hash|list|set|zset; an unknown type answers an empty result.- Default
COUNTis 10 when omitted.
The X family #
XSCAN / XHSCAN / XSSCAN / XZSCAN are the engine’s native scans:
- Start at an empty cursor
""; the walk is over when an empty cursor comes back. MATCHis a regular expression, not a glob, and is passed to the storage layer.ASC/DESCchoose the direction (DESCwalks withRevScan).XSCAN’s first argument is the type name:KV,HASH,LIST,SET,ZSET(case-insensitive; anything else is aninvalid key typeerror).
Rules that apply to both #
COUNTis a hint, capped at 10000. It bounds allocation, not the number of returned items.- A page can come back empty while the cursor still advances. Never stop on a short or empty page;
loop until the cursor is
0(Redis family) or""(X family). - Scans do not take a snapshot: keys added or removed during a walk may or may not appear. That is the same guarantee Redis gives.
- Both families are read commands, so on a follower they need
--openreads(or a redirect to the leader).
Loop patterns #
# Redis family — go-redis's typed HScan does not work; use the generic API
cursor = 0
while True:
cursor, keys = r.execute_command("SCAN", cursor, "MATCH", "user:*", "COUNT", 500)
for k in keys:
...
if cursor == b"0" or cursor == "0":
break
# X family — empty cursor terminates
cursor = ""
while True:
cursor, items = r.execute_command("XSCAN", "HASH", cursor, "MATCH", "^user:", "COUNT", 500)
for k in items:
...
if not cursor:
break
# redis-cli drives the cursor for you on SCAN
redis-cli -p 11001 --scan --pattern 'user:*' | head