Book Image

Instant Redis Optimization How-to

By : Arun Chinnachamy
Book Image

Instant Redis Optimization How-to

By: Arun Chinnachamy

Overview of this book

The database is the backbone of any application and Redis is a next generation NoSQL database which can provide ultra-fast performance if tuned and calibrated correctly. Instant Redis Optimization How-to will show you how to leverage Redis in your application for greater performance and scalability. Instant Redis Optimization How-to will show you how to make the most of Redis. Using real-world examples of Redis as a caching and queuing service, you will learn how to install and calibrate Redis to optimize memory usage, read and write speed, as well as bulk writes and transactions. If you want to use Redis for its blazing fast capabilities, then this book is for you.Instant Redis Optimization How-to shows you how to optimize and scale Redis with practical recipes on installation and calibration for performance and memory optimization as well as advanced features like PUB/SUB. This book starts by providing clear instructions on how to install and fine-tune Redis to work efficiently in your application stack. You will also learn how to maintain persistence, how to optimize Redis to handle different data types, as well as memory usage optimization. You will then learn how to use bulk writes and transactions, as well as publish/subscribe features to get the most out of Redis. Offering best practices and troubleshooting tips, this book will also show you how to manage and maintain performance. This book finishes by recommending the best client libraries for all major programming languages. By the end of this book you will know how to create blazing fast applications using Redis.
Table of Contents (7 chapters)

Troubleshooting and monitoring (Intermediate)


The computing world is not ideal and it is common to face issues and performance problems in Redis, which require troubleshooting techniques. In this section, let us discuss about commands that help in debugging and also about how to troubleshoot memory issues in Redis.

Redis is a simple piece of software that does not require complex debugging tools to identify the issues. Most of the time the issues in Redis are either due to wrong configuration or bad application design.

We have a couple of tools inbuilt into Redis that can help us identify what is really going on inside Redis. Let us see how to use the monitor command.

Tip

Warning

The monitor command adds a lot of overhead to the server and should be used strictly for debugging, and as rarely as possible. The command can reduce the performance of the server significantly.

How to do it...

  1. The monitor command, like any other command, issues through redis-cli or Telnet. So open redis-cli or Telnet to connect to Redis.

  2. This command streams every command issued to the Redis server back to the client, which helps us to see what our server is going through.

  3. Issue the following command:

    redis-cli monitor
    
  4. After you issue this command, open another redis-cli and issue the following command:

    SET Google Android
    
  5. In our monitor's command-line interface, we can see this command as follows:

    1356217734.180621 [0 127.0.0.1:62544] "set" "Google" "Android"
    
  6. To stop the monitor command in redis-cli, you need to use Ctrl + C (SIGINT). The monitor command can be used by clients connected through Telnet too, in which case the QUIT command needs to be used to stop the command.

There's more...

A software without bugs does not exist. There might be situations when Redis does not behave as it should. Redis has got a solution to provide as much information as possible to the developers to fix the issues.

Slow log

The Redis Slow log is a system to log queries. The slowlog command is used to view the commands that exceeded the configured time in our configuration file. This command points out the queries that are running slowly, which can help us design the system better. Using this command, we can read the slow queries and also reset the queue. The Slow log is stored strictly in the memory, and provides quite rapid logging with little to no performance hit at the cost of extra memory to store the log.

To get the current length of the Slow log, use slowlog len. To read the Slow log, the slowlog get command should be used. This command returns the complete contents of the log. If you want only the latest N number of entries, pass the number to the command as follows:

slowlog get 2

The output format for the log will be:

1)1) (integer) 7
     2) (integer) 1356248799
     3) 15
     4)  1) "SUNIONSTORE"
          2) "Set1"
          3) "set2"
          4) "set3"
2) 1) (integer) 6
     2) (integer) 1356248815
     3) 12
     4)  1) "SINTER"
          2) "Set1"
          3) "set2"
          4) "set3"

The preceding log output has two entries. Every entry consists of four fields, as follows:

  • A unique auto-incrementing identifier for every log. The identifier will be reset on server restart.

  • The Unix timestamp at which the logged command was executed.

  • The amount of time needed for its execution, in microseconds.

  • The arguments of the command in the form of an array.

To reset the Slow log, use the slowlog reset command. Once the command is executed, the log information is lost forever.

Redis software watchdog

In the latest version (v2.6.13) of Redis, a new debugging tool for developers has been introduced, watchdog. It is designed to track latency problems that could escape analysis using normal tools. This feature is still experimental and should be used in production with caution as a last resort when all other means of debugging fail. It is not advisable to keep the watchdog running for an extended period of time. The output is logged into a logfile that contains low-level reports about the server component. The log can be sent to the Redis community to figure out what caused the block in the server and fix the problem, if any, in the server.

Tip

Caution

Be sure to switch off the watchdog by using the command CONFIG SET watchdog-period 0.

To enable the watchdog, you need to use the CONFIG SET command as follows:

CONFIG SET watchdog-period 700

The preceding command will log any latency issues if the delay is more than 700 milliseconds. The minimum time that can be mentioned is 200 milliseconds.