Redis / Intermediate: application correctness and coordination / Lesson 14
Iterate a keyspace with SCAN
Use cursor-based iteration while handling duplicates, empty batches, and changing keys.
On this page
Lesson goals
- Explain a complete SCAN iteration and cursor handling.
- Describe duplicate and concurrent-change behavior.
- Design bounded, idempotent processing for a maintenance task.
Define the iteration boundary
A maintenance task may need to inspect keys matching a namespace without requesting the entire keyspace in one response. SCAN uses a cursor to continue an iteration, but it is not a transactional snapshot. Record the target deployment, node or database scope, permitted key prefix, and operation before starting. In Cluster, account for node-specific keyspaces and topology changes using the client and deployment documentation.
Create synthetic lab keys
The commands below create three disposable string keys in an isolated Redis lab. They are illustrative and do not connect to a configured service. Use a unique lab prefix and appropriate access permissions. Production maintenance must be designed separately, including ownership, rate limits, failure recovery, and the effect of concurrent writers. Never infer that a naming prefix itself authorizes access.
SET lab:scan:item:1 "one"
SET lab:scan:item:2 "two"
SET lab:scan:item:3 "three"
The fixture names three keys under lab:scan:item:. Their values are synthetic. The example does not claim an execution result or require access to any application keyspace.
Continue until the returned cursor is zero
Begin with cursor zero and pass the returned cursor to the next SCAN call with the intended filter. A call may return no matching keys while the cursor is still nonzero, so an empty batch is not completion. COUNT is a work hint rather than a guaranteed batch size. The actual cursor and key order depend on the server and keyspace.
SCAN 0 MATCH lab:scan:item:* COUNT 10
The command starts a filtered iteration over the synthetic prefix. Continue with the cursor returned by the server until it returns zero. No particular cursor, order, or number of returned keys is asserted.
Handle duplicates and concurrent changes
A full iteration can return an element more than once. Keys present throughout a complete iteration have the documented inclusion guarantee, while keys added or removed during it may or may not appear. Make processing safe when repeated, and recheck each key’s current type and state if the action depends on them. Do not treat an iteration over a changing collection as a consistent inventory snapshot.
Test the operational workflow
In the isolated lab, process batches with a bounded rate and simulate an empty batch, duplicate key, interruption, and a concurrent key change. Record how progress and recovery are handled, then verify that repeating an action is safe. A cursor is iteration state, not a durable business checkpoint guaranteeing a snapshot. Monitor server load and stop the maintenance task if its agreed operational limits are exceeded.
Common errors and fixes
The scan stops after an empty batch.
- Why it happens
- The client treats zero returned keys as completion even when the cursor is nonzero.
- How to fix it
- Continue with the returned cursor until it is zero.
- Prevent it next time
- Test empty batches in the iterator.
A key is processed twice.
- Why it happens
- SCAN can return duplicates during a full iteration.
- How to fix it
- Make the operation idempotent or explicitly deduplicate where appropriate.
- Prevent it next time
- Test repeated key delivery.
A changing keyspace inventory is treated as an exact snapshot.
- Why it happens
- The iteration’s concurrency limits were ignored.
- How to fix it
- Use a separately designed consistent inventory method if exactness is required.
- Prevent it next time
- Document concurrent-change semantics and test them.
2 practice exercises
1. Should a client stop when a SCAN response has no keys but a nonzero cursor?
Show worked solution
No. Continue with the returned cursor; a filtered or intermediate batch may be empty before the full iteration finishes.
2. Why must a maintenance action tolerate the same key twice?
Show worked solution
A full SCAN iteration may return a key more than once. An idempotent action or appropriate deduplication prevents duplicate processing from corrupting the result.
Key takeaways
- Continue with returned cursors until zero, even after an empty batch.
- COUNT is a hint and duplicate keys are possible.
- SCAN over changing keys is not a consistent snapshot.