Need to know about Redis Slow Log? We can help you.
Recently we had a customer who experienced high latency and high CPU usage with Redis operations. In such a case, Redis Slow Log is one of the best tools to debug and trace the Redis database.
Since Redis base on a single-threaded architecture, Redis Slow Log is much more useful than MySQL Slow Query Log.
As part of our Server Management Services, we assist our customers with several Redis queries.
Today, let us discuss Redis Slow Log.
What is Redis slow log?
It is a system to log queries that exceeded a specified execution time. This does not include I/O operations like talk with the client, reply, and so forth, but the time to actually execute the command.
SLOWLOG subcommand [argument]
This command is available since 2.2.12 and we use it in order to read and reset the Redis slow queries log.
How to setup slow log parameters?
We can configure the slow log with two parameters:
This tells Redis what is the execution time, in microseconds, to exceed in order for the command to log.
It is the length of the slow log. The minimum value is zero. When a new command logs and the slow log is already at its maximum length, it removes the oldest one from the queue of logged commands in order to make space.
We can edit, redis.conf to do this configuration, or use the CONFIG GET and CONFIG SET commands while the server run.
In addition, it is highly effective at showing the actual processing time of each command.
How to read Slow Log?
The slow log accumulates in memory. This makes it remarkably fast that we can enable the logging of all the commands with a minor performance hit.
Generally, we use the SLOWLOG GET command to read the slow log which returns every entry in the slow log.
It is possible to return only the N most recent entries passing an additional argument to the command (for instance SLOWLOG GET 10).
In addition, our Support Techs suggest having a recent version of redis-cli in order to read the slow log output.
redis 127.0.0.1:6379> slowlog get 2 1) 1) (integer) 14 2) (integer) 1309448221 3) (integer) 15 4) 1) “ping” 2) 1) (integer) 13 2) (integer) 1309448128 3) (integer) 30 4) 1) “slowlog” 2) “get” 3) “100”
There are also optional fields emitted only by Redis 4.0 or greater:
5) “127.0.0.1:58217” 6) “worker-123”
Every entry composes of four (or six) fields:
- A unique progressive identifier for every slow log entry.
- The unix timestamp at which the logged command process.
- The amount of time for the execution, in microseconds.
- The array composing the arguments of the command.
- Client IP address and port (4.0 only).
- Client name if set via the CLIENT SETNAME command (4.0 only).
We can use the entry’s unique ID in order to avoid processing slow log entries multiple times.
The ID is never reset in the course of the Redis server execution. However, a server restart can reset it.
In addition, it is possible to get the length of the slow log using the command SLOWLOG LEN.
We can reset the slow log using the SLOWLOG RESET command. Once we delete the information it is lost forever.
[Need help with the Slow Log? We are available 24*7]
In short, Redis Slow Log is one of the best tools to debug and trace the Redis database. Today, we saw how our Support Techs use it to perform the task.