Redis slow query log helps development and operation personnel locate the slow operation of the system. Slow query log is that the system calculates the execution time of each command before and after the command is executed. When the preset threshold value is exceeded, the relevant information of the command (slow query ID, occurrence time stamp, time consumption, and command details) is recorded.
A command of redis client is divided into the following four parts:
It should be noted that the slow query log only counts the time taken by step 3) to execute the command, so slow query does not mean that the client does not have a timeout problem. It should be noted that the slow query log only counts the time taken by step 3) to execute the command, so slow query does not mean that the client does not have a timeout problem.
1、 Configuration parameters for slow queries：
- Slow log slow than
The slowlog log slow than parameter is the default threshold, in microseconds. The default value is 10000. If a command takes more than 10000 microseconds to execute, it will be recorded in the slow query log.
If the value of slowlog log slow than is 0, all commands are logged.
If the value of slowlog log slow than is less than 0, no commands are logged.
- Length of slow query log slowlog Max len
Slowlog Max len only shows how many slow query logs can be stored at most. Redis uses a list to store slow query logs. Showlog Max len is the maximum length of the list. When the slow query log has reached the maximum length of the list, and there are slow query logs to enter the list, the log that was first inserted into the list will be removed from the list, and the new log will be inserted at the end of the list.
2、 Composition of slow query log
The slow query log consists of the following four attributes:
Identification ID, occurrence time stamp, command time consumption, execution command and parameters
3、 Access and management of slow query log
Get slow query log slowlog get [n]
Command: slowlog get [n]
Type selection: n, optional, represents the number of logs obtained
For example: showlog get 5
4、 Slow query log best practices
- Suggestions on setting slowlog Max len
In the online environment, it is recommended to increase the list of slow query logs. When logging slow query logs, redis will truncate long commands, which will not occupy a lot of memory. Increasing the list of slow queries can slow down the possibility of slow queries being excluded from the list. For example, online can be set to more than 1000.
- Suggestions on setting slowlog log lower than
This value needs to be adjusted according to the concurrency of redis. Since redis uses single thread response fame and wealth, for high traffic scenarios, if the execution time of commands is more than 1 millisecond, redis can support OPS (operations per second) less than 1000 at most. Therefore, it is recommended to set redis to 1 ms for high OPS scenarios.
- Slow query only records command execution time, not including command queuing time and network transmission time.
Therefore, the execution time of client command is longer than that of redis server. Because of the extreme queuing of command execution, slow query will lead to command cascading blocking. Therefore, when the client requests timeout, it is necessary to check whether there is corresponding slow query at this time point, so as to analyze whether the command cascade blocking is caused by slow query
- The slow query log is a first in, first out queue
When there are many slow queries, some of the slow query commands may be lost. You can execute the slow get command periodically to persist the slow query log to other storage. Then make visual interface query.