Redis

Redis Monitor

Redis is an in-memory database widely used for distributed caching systems and as a high-performance data store. The data layer is extremely important in any application stack, and frequent monitoring is worthwhile. Redis logs are one of the ways that system administrators can use to debug or identify the issues occurring inside the server. It is very useful to see what client requests have been served by the Redis server all the time while it is awake. It may help database administrators to identify what went wrong or what commands have caused the database to crash.

The MONITOR command is one of the best debugging utility commands introduced by Redis. In this article, the Redis MONITOR command is focused on.

Redis MONITOR Command Explained

The MONITOR command yields every command served by the Redis server instantly. It is possible to utilize the MONITOR command through the telnet and Redis CLI. Furthermore, when the MONITOR command is executed, the Redis CLI streams all the data in real-time, and the CLI becomes non-interactive. Most importantly, all the commands executed within the server instance can be tracked and help to spot bugs easily, which is advantageous.

Syntax:

MONITOR

In addition, no optional arguments are available with the MONITOR command since it is just for server monitoring purposes.

It returns a non-standard return value that is the set of commands received from clients to the server instance infinitely.

Use Case 01 – The MONITOR Command via Redis CLI

Let’s connect to the Redis server with the Redis-CLI and specify the MONITOR command in the connection string as follows:

redis-cli monitor

Here, the CLI will open up in the monitoring mode. You can connect to the Redis CLI first and then execute the MONITOR command, as shown in the following:

Now, we can connect to the server with another CLI and execute some Redis commands. At the same time, you can see all the commands are logged into the monitoring windows.

On the left-hand side, you can see the monitoring client, and, on the other side, the normal client connection to the Redis server.

Furthermore, you can use the Ctrl+C(SIGINT) keys to exit from the monitoring mode, as shown in the following:

Use Case 02: The MONITOR Command via Telnet

Another way of executing the MONITOR command is via telnet. After connecting to the Redis server instance via telnet by giving the host name and port, you can directly issue the following MONITOR command:

telnet localhost

In addition, the QUIT and RESET commands can be issued to quit the monitoring stream.

Pros of the Monitor Command

As you have seen in the previous section, there are several advantages over the MONITOR command. Some of the major benefits are listed in the following:

  • Track down the commands executed in a given period
  • Help understand server status
  • Spot bugs based on the executed command stack
  • Spot database corruptions
  • Track down unwanted data deletion and alternations
  • Hiding the sensitive data associated with administrative commands like AUTH

Cons of the Monitor Command

The MONITOR command has many advantages when debugging the Redis server for issues. In addition, running a single monitor client affects the throughput of the Redis server, which is a considerable trade-off.

Let’s inspect the benchmark values without running the MONITOR command over the server. We can use the REDIS-BENCHMARK command as shown below:

Redis-benchmark -c 10 -n 100000 -q

As per the benchmark results, the server throughput for the SET command is around 31000 requests per second; GET is around 30000, and so on.

Next, we will check the benchmark values with the MONITOR stream running.

Note that the number of requests per command has been drastically reduced even if a single monitoring client is running. When we increase the number of monitoring clients, the throughput decreases by a huge amount, which is a drawback in the Redis MONITOR command.

Conclusion

In summary, the MONITOR is a very simple Redis command built for debugging purposes. As discussed, it can be used via Redis-CLI and telnet. The MONITOR command streams every request processed by the Redis server instance. As you have seen, the Redis CLI becomes non-interactive when the MONITOR streams back all the commands executed by the clients on the Redis server instance. For security purposes, the MONITOR command doesn’t stream Redis administrative commands. Overall, it helps spot issues in the server but reducing the throughput by a considerable amount is a trade-off.

About the author

Nimesha Jinarajadasa

Being a Full-stack Senior Software Engineer for more than five years, I love technology, as technology has the power to solve our many problems within just a minute. I try to learn more and create more opportunities for this new world.