Safely Accessing Massive Redis Data Without Causing Service Outage
This article explains why using the KEYS command on large Redis keyspaces can crash the service and demonstrates how the SCAN command provides a non‑blocking, incremental way to iterate over massive data safely.
The author shares how to access massive data in Redis while avoiding incidents.
Incident: Using the KEYS user_token* command to count online users caused Redis to become unresponsive because KEYS scans the entire keyspace with O(N) complexity on a single‑threaded server.
Analysis: With millions of keys, KEYS blocks the server; Redis processes commands sequentially, so other operations must wait for KEYS to finish.
Solution: Use the SCAN command, which iterates incrementally using a cursor, does not block the thread, supports pattern matching and a COUNT hint, may return duplicate entries, and requires client‑side deduplication; the iteration ends when the returned cursor is zero.
SCAN command format: SCAN cursor [MATCH pattern] [COUNT count]
Explanation: SCAN is incremental, returning a small subset of elements each call; the cursor starts at 0 and the iteration finishes when the cursor returns to 0, preventing Redis from hanging.
Example: Starting from cursor 0 returns cursor 6 and some keys; subsequent SCAN calls continue from cursor 6, and so on, until the cursor reaches 0.
Conclusion: SCAN is a common interview topic and essential for handling large keyspaces in production; using KEYS on large datasets can severely degrade performance.
Architect's Tech Stack
Java backend, microservices, distributed systems, containerized programming, and more.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.