Redis

Redis Slowlog

As database administrators, it is crucial to know the best tools to help you perform the task. In Redis, the Slow Log is one such utility. It is a logging mechanism but in a non-traditional sense.

This guide will discuss configuring and using the Redis slow log commands.

What is Redis Slow Log?

The Redis Slow Log or RSL is a feature that allows the Redis server to track and log all queries that exceed the allocated execution time.

If you are experiencing slow query execution or high CPU usage in your Redis server, this is the tool you want to use.

NOTE: The execution time allocated does not include Input/Output operations. It refers to the actual time after the server receives the command and its time to complete executing it.

How Slow Log Works?

The slow log feature is very simple. It works into two simple steps.

  1. Once a query is issued, the server keeps track of the time elapsed when executing the command.
  2. If the command exceeds the allocated time, it is logged using the slow log system.

NOTE: The execution time of a query is specified in the slowlog-log-slower-than directive in the Redis configuration file.

You can also specify the number of entries in the slow log file using the slowlog-max-len directive.

Setting up Log Parameters

Two entries allow you to configure slow log parameters.

  1. Slowlog-log-slower-than
  2. Slowlog-max-len

Edit the configuration file with your text editor to change or set these values. By default, these values are set to:

slowlog-log-slower-than 10000

slowlog-max-len 128

Hence, the server will log 128 entries of the queries that exceed 10000 microseconds.

If the log entry contains more than the specified entries, it removes the oldest one to make space for a new entry.

Using the config set command, you can also configure the slowlog parameters at runtime. An example is as shown:

127.0.0.1:6379> config set slowlog-log-slower-than 10000

127.0.0.1:6379> config set slowlog-max-len 128

Redis Slow Log

Before reading the entries in the slowlog, let us change the value of the slowlog to a very small value.

An example command is as shown below:

127.0.0.1:6379> config set slowlog-log-slower-than 1

OK

All queries that exceed 1 microsecond will be logged into the entries.

Let us perform sample queries as shown:

127.0.0.1:6379> HSET user_info username "user1" email "[email protected]"

(integer) 2

127.0.0.1:6379> HGETALL user_info

In the above example, we run two queries that are bound to exceed 1 micro second.

Fetching Slow Log Entries

To fetch all the entries in the Redis Slow log, run the SLOWLOG GET command:

127.0.0.1:6379> SLOWLOG GET

The command above should retrieve all the entries as shown in the output below:

Components of a Slow Log Entry

Each Slow Log entry is comprised of 6 main parts.

  1. The first is a unique identifier for the log entry.
  2. Next is a unix timestamp denoting the time at which the entry was added.
  3. The third part denotes the time that the query took in microseconds.
  4. Fourth is an array containing the arguments of the specified command.
  5. Last but not least is the client address and port that issued the command.
  6. Finally, the client name as specified by the client setname command.

Redis Reset Slow Log Entries

If you want to clean up the slow log entries, use the SLOWLOG RESET command as shown:

127.0.0.1:6379> SLOWLOG RESET

OK

The command should remove all existing entries. However, if the reset command exceeds the specified time, it will be added to the logs.

Conclusion

This article covered one of the best tools for administering and troubleshooting Redis; Redis Slow Log.

See you in the next one!

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list