Skip to main content

Description

Iterates the set of keys in the database using a cursor-based iterator. Unlike KEYS, SCAN doesn’t block the server and is safe to use in production.

Method Signatures

Parameters

cursor
string | number
required
The cursor position. Use "0" or 0 to start a new iteration.
opts
ScanCommandOptions
Optional configuration object:

Return Value

result
[string, string[]] | [string, { key: string; type: string }[]]
A tuple containing:
  1. Next cursor - Use this value in the next scan call. When "0", iteration is complete.
  2. Keys - Array of matching keys (or objects with key and type if withType: true)

Examples

Basic iteration

Scan with pattern matching

Scan with count hint

Filter by key type

Scan with type information

Combine options

Single-pass scan

Cursor-Based Iteration

The SCAN command uses cursor-based iteration:
  1. Start with cursor "0"
  2. Call scan(cursor) and receive [nextCursor, keys]
  3. Process the keys
  4. If nextCursor is "0", iteration is complete
  5. Otherwise, call scan(nextCursor) and repeat
The cursor is opaque - don’t parse or manipulate it. Always use the cursor returned from the previous call.

Guarantees and Characteristics

  • Full iteration guarantee: A key that exists from start to finish will be returned
  • No duplicates guarantee: A key that is never modified will be returned only once
  • Safe for production: Non-blocking, suitable for large databases
  • Variable results: The number of keys returned per call may vary
  • Pattern matching: Filtering happens after fetching, so you may get empty results

See Also

  • keys - Get all keys matching a pattern (blocking)
  • type - Get the type of a key