How to Handle Slow Queries in Redis: Logs, Commands, and Best Practices
To diagnose and resolve Redis slow queries, this guide explains how to retrieve and manage the slow query log, understand command complexities, replace inefficient commands with optimized alternatives, and configure slowlog settings such as slowlog‑max‑len and slowlog‑log‑slower‑than for production environments.
Hello, I am Wufan.
“Redis 有慢查询怎么办?”
First, locate slow queries using the slow query log.
Slow Query Log
Retrieve Slow Query Log
slowlog get [N]The log contains four fields: entry ID, timestamp, execution time, and the command with its arguments.
Get Current Length of Slow Query Log List
showlog lenClear Slow Query Log
showlog resetComplexity of Slow Commands
The slowness of a command is related to its algorithmic complexity. You can check command complexities at the official Redis command reference: https://redis.io/commands When Redis performance degrades, use the Redis log or the latency monitor tool to identify slow requests, then consult the documentation to see if the command has high complexity. For example, the keys command has O(N) complexity, as shown in the figure below.
Replace with Efficient Commands
If many slow commands are found, replace them with more efficient alternatives:
When you need all members of a set, use SSCAN iteratively instead of SMEMBERS to avoid returning a large payload at once.
For sorting, intersection, or union operations, perform them in the client rather than using SORT, SUNION, or SINTER to prevent slowing down the Redis instance.
The KEYS command scans all stored key‑value pairs, causing high latency and is generally discouraged in production.
Best Practices
Two main configurations 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 value: 128 (as seen in the source code).
Configuration recommendation: In production, increase the length to avoid truncating long commands; a value of 1000 or more is common.
slowlog-log-slower-than
Purpose: Threshold (in microseconds) for a command to be considered slow and logged.
Default value: 10 ms.
Configuration recommendation: Adjust based on traffic; high‑throughput environments may set it to 1 ms.
Persisting the Log
Persist the slow query log to a database to avoid loss and enable a visual query interface for operations troubleshooting.
References: Redis 6.0 source code, Redis development and operations articles.
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.
