Scan

Scan #

8 commands in two families with two different cursor contracts. Getting them mixed up is the most common integration bug with this engine.

CmdRWSyntaxReturnsExample → reply
SCANRSCAN cursor [MATCH glob] [COUNT n] [TYPE type]array [cursor, keys]; cursor 0 ends the walkSCAN 0 MATCH user:* COUNT 100 → [cursor, [user:1, user:2]]
HSCANRHSCAN KEY cursor [MATCH glob] [COUNT n]array [cursor, [field, value, ...]]HSCAN user:1 0 COUNT 10 → [0, [name, ada]]
SSCANRSSCAN KEY cursor [MATCH glob] [COUNT n]array [cursor, [member, ...]]SSCAN tags 0 → [0, [go]]
ZSCANRZSCAN KEY cursor [MATCH glob] [COUNT n]array [cursor, [member, score, ...]]ZSCAN board 0 → [0, [ada, 105]]
XSCANRXSCAN type cursor [MATCH regex] [COUNT n] [ASC|DESC]array [cursor, keys]; empty cursor ends the walkXSCAN KV "" COUNT 5 → [key:5, [...]]
XHSCANRXHSCAN KEY cursor [MATCH regex] [COUNT n] [ASC|DESC]array [cursor, [field, value, ...]]XHSCAN user:1 "" COUNT 5 → [name, [name, ada]]
XSSCANRXSSCAN KEY cursor [MATCH regex] [COUNT n] [ASC|DESC]array [cursor, [member, ...]]XSSCAN tags "" → [go, [go]]
XZSCANRXZSCAN 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 is 0.
  • 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 / ZSCAN answer the last field or member name as the cursor. Consequences:
    • A field literally named 0 collides 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.
  • MATCH takes a Redis glob (*, ?, [abc], \\ escape), translated to a regexp and applied by the handler over a bounded page.
  • SCAN supports TYPE string|hash|list|set|zset; an unknown type answers an empty result.
  • Default COUNT is 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.
  • MATCH is a regular expression, not a glob, and is passed to the storage layer.
  • ASC / DESC choose the direction (DESC walks with RevScan).
  • XSCAN’s first argument is the type name: KV, HASH, LIST, SET, ZSET (case-insensitive; anything else is an invalid key type error).

Rules that apply to both #

  • COUNT is 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