Node Redis: Get All Keys (Detailed Guide w/ Code Examples)
Use Case(s)
In Node.js applications, you may need to fetch all keys from a Redis database. This is often used for debugging purposes or when you need to analyze or manipulate all data.
## Code Examples
- Using
KEYS *
command:
CODE_BLOCK_PLACEHOLDER_0
In this example,KEYS *
command is used to fetch all the keys from Redis. The returned keys are then logged to the console. - Using
SCAN
command for large databases:
CODE_BLOCK_PLACEHOLDER_1
For larger databases, it's better to useSCAN
asKEYS *
may end up blocking the server while it fetches all keys. In this example,SCAN
is used in a loop until all keys are fetched and logged.
Best Practices
- Avoid using
KEYS *
command in a production environment as it might affect performance. Instead, useSCAN
as it's more efficient and doesn't block the server.
Common Mistakes
- Using
KEYS *
on large databases can lead to performance issues. It's best to useSCAN
for such cases.
FAQs
- Q: Can I filter keys when fetching them from Redis?
A: Yes, bothKEYS
andSCAN
commands accept a pattern that can be used to filter keys.
Was this content helpful?
Help us improve by giving us your feedback.
Similar Code Examples
- Node Redis: Get Replica
- Node Redis Get Key
- Node Redis: Get First 10 Keys
- Node Redis: Get All Keys and Values
- Node Redis: Get Length of List
- Get All Hash Keys with Redis in Node.js
- Node Redis: Get Hash Values at Key
- Node Redis: Get Current Memory Usage
- Node Redis: Get All Keys Matching Pattern
- Node Redis: Get Keys by TTL
- Node Redis: Getting All Databases
- Getting Master Status in Node Redis
White Paper
Free System Design on AWS E-Book
Download this early release of O'Reilly's latest cloud infrastructure e-book: System Design on AWS.

Switch & save up to 80%
Dragonfly is fully compatible with the Redis ecosystem and requires no code changes to implement. Instantly experience up to a 25X boost in performance and 80% reduction in cost