Redis Research (14th)-management tools, redis research tools
To do well, you must first sharpen your tools. When using Redis, if you can effectively use various Redis management tools, it will greatly facilitate development and management.
I. redis-cli
I believe you are familiar with redis-cli. As the command line client of Redis, you can find it from any server installed with Redis, therefore, Redis-cli is the simplest and most practical tool for managing redis. Redis-cli can execute most Redis commands, including the INFO command for viewing database information, the CONFIG command for changing database settings, and the SAVE command for forcing RDB snapshots, the following describes several useful commands for managing Redis.
1. Time-consuming command log
When the execution time of a command exceeds the limit, Redis adds the execution time and other information of the command to the time-consuming command log (slow log) for developers to view. You can set this limit using the slowlog-log-slower-than parameter in the configuration file. Note that the unit is microseconds (1000000 microseconds is equivalent to 1 second). The default value is 10000. The time-consuming command logs are stored in the memory. You can use the slowlog-max-len parameter in the configuration file to limit the number of records.
Use the slowlog get command to obtain the current time-consuming command log, such:
redis>SLOWLOG GET1) 1) (integer) 42) (integer) 13568064133) (integer) 584) 1) "get"2) "foo"2) 1) (integer) 32) (integer) 13568064083) (integer ) 344) 1) "set"2) "foo"3) "bar "
Each log consists of the following four parts:
(1) Unique ID of the log;
(2) UNIX time when the command is executed;
(3) Time consumed by the command, in microseconds;
(4) commands and their parameters.
To generate some time-consuming command logs for demonstration, set the slowlog-log-slower-than parameter value to O, that is, to record all commands. If it is set to a negative number, the time-consuming command log is disabled.
2. Command monitoring
Redis provides the MONITOR command to MONITOR all commands executed by Redis. redis-cli also supports this command, such as executing the MONITOR in redis-cli:
redis>MONITOROK
At this time, any commands executed by Redis will be printed out in redis-cli. If we open another redis-cli and execute the SET foo bar command, the following content will be output in the previous redis-cli:
1356806981.885237 [0 127.0.0.1:57339] "SET" "foo" "bar
The MONITOR command affects Redis performance very much. Using the MONITOR command on a client will reduce Redis's load capability by nearly half. Therefore, the MONITOR command is only suitable for debugging and error correction.
The Instagram team developed a Redis query and Analysis Program redis-faina Based on the MONITOR command. Redis-faina can analyze the most commonly used commands, the most frequently accessed keys, and other information based on the monitoring results of the MONITOR command, which is very helpful for understanding the usage of Redis.
Instagram is an image sharing community under Facebook. The redis-faina Project address is.
The input value of the redis-faina.py is the result of the MONITOR command execution for a period of time. For example:
Redis-cli MONITOR | head-n <Number of commands to be analyzed> |./redis-faina.py
Ii. phpRedisAdmin
When there are many keys in Redis, it is not very convenient to use redis-cli to manage data, just as some people like to use phpMyAdm in to manage MySQL, redis also has a Web page management tool phpRedisAdmin developed by PHP. PhpRedisAdmin allows you to view key lists, edit key values, import/export database data, view database information, and view key information in a tree structure.
1. Install phpRedisAdmin
Install phpRedisAdmin as follows:
git clone http s://github.com /ErikDubbelboer/phpRedisAdmin.gitcd phpRedisAdmin
PhpRedisAdmin depends on the PHP Redis client Predis. Therefore, you need to execute the following two commands to download the Predis:
git submodule initgit submodule update
2. Configure database connection
After downloading phpRedisAdmin, You need to configure the Redis connection information. By default, phpRedisAdmin connects to 127.0.0.1 and port 6379. If you need to change or add database information, edit the config. inc. php file in the supported des folder.
3. Use phpRedisAdmin
Install PHP and Web servers (such as Nginx), and store the phpRedisAdmin folder in the website directory for access,
PhpRedisAdmin automatically separates Redis keys with ":" and displays them in a tree structure, which is very intuitive. For example, the keys post: 1 and post: 2 are in the post tree.
Click a key to view the key information, including the key type, survival time, and key value, which can be easily edited.
4. Performance
PhpRedisAdmin uses the KEYS * command to obtain the key list, and then uses the TYPE command to obtain the data TYPE of all KEYS, therefore, the performance is not high when there are many KEYS (for a Redis database with 1 million KEYS, using the KEYS * command on a general PC takes about dozens of milliseconds ). Because Redis uses a single thread to process commands, phpRedisAdmin is not suitable for databases with large data volumes in the production environment.
Iii. Rdbtools
Rdbtools is a snapshot file parser for Redis. It can export JSON data files based on the snapshot file and analyze the space occupied by each key in Redis. Rdbtools is developed using Python. The project address is
Https://github.com/sripathikrishnan/redis-rdb-tools.
1. Install Rdbtools
Run the following command to install Rdbtools:
git clone https://github.com/sripathikrishnan/redis-rdb-toolscd redis-rdb-toolssudo python setup.py instal l
2. Generate a snapshot File
If RDB persistence is not enabled, you can use the SAVE command to manually generate a snapshot file for Redis.
3. Export snapshots in JSON format
Snapshot files are in binary format, which is not conducive to viewing. You can use Rdbtools to export them to JSON format. The command is as follows:
rdb --command json /path/to/dump.rdb > output_file name.json
/Path/to/dump. rdb is the path of the snapshot file, and output_filename.json is the path of the file to be exported.
4. Generate a space usage report
Rdbtools can export the storage status of each key recorded in the snapshot file as a CSV file. You can import the CSV file to data analysis tools such as Excel for analysis to understand the usage of Redis. The command is as follows:
rdb -c memory /path/to/dump.rdb > output_filename.csv
The fields and descriptions of exported CSV files are as follows: