How to Handle Redis Slow Queries: Logs, Commands, and Best Practices
This article explains how to identify and resolve Redis slow queries by using the slow query log, understanding command complexity, replacing inefficient commands with more efficient alternatives, and configuring slowlog parameters for optimal performance.
Redis slow queries can degrade performance, and the first step is to locate them using the slow query log.
Slow Query Log
Retrieve Slow Query Log
showlog get [N]The log contains four fields: log ID, timestamp, command duration, and the executed command with its arguments.
Get Current Length of Slow Query Log List
showlog lenClear Slow Query Log
showlog resetCommand Complexity of Slow Queries
Slow operations are related to command complexity. You can check command complexities at https://redis.io/commands . For example, the keys command has O(N) complexity and should be avoided in production.
Replace with Efficient Commands
If many slow commands are identified, replace them with more efficient alternatives:
Use SSCAN instead of SMEMBERS to iterate over set members and avoid blocking.
Perform sorting, intersection, and union operations on the client side rather than using SORT, SUNION, or SINTER commands.
The KEYS command scans all keys and is generally discouraged in production environments.
Best Practices
Two main configuration options control the slow query log: slowlog-max-len and slowlog-log-slower-than.
slowlog-max-len
Purpose: Maximum number of entries stored in the slow query log.
Default: 128 entries (source code confirms this default).
Recommendation: Increase the length in production (e.g., >1000) to avoid truncating long commands and to limit memory usage.
slowlog-log-slower-than
Purpose: Threshold (in microseconds) for logging a command as slow.
Default: 10 ms.
Recommendation: Adjust based on traffic; for high‑load scenarios, set to 1 ms.
Persisting the Log
Persist the slow query log to a database to prevent loss and enable visual query interfaces for operations troubleshooting.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Wukong Talks Architecture
Explaining distributed systems and architecture through stories. Author of the "JVM Performance Tuning in Practice" column, open-source author of "Spring Cloud in Practice PassJava", and independently developed a PMP practice quiz mini-program.
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.
