To use Redis well, you must not only know how to use the API, but also know how to prevent Redis from blocking and check and analyze the blocking. Today, let’s talk to you about how to discover the blocking of Redis—finding blocked commands through slow queries.
Like Mysql, Redis also has slow query records. When the command execution time exceeds the set value, the command will be recorded in the slow query list. With slow queries, we can improve our programs. Prevent redis from blocking.
Configuration
There are two configuration parameters for slow query:
slowlog-log- slower-than
slowlog-max-len
slowlog-log-slower-than is used to set a threshold in microseconds. The default value is 10000, which is 10 milliseconds.
When the value is set to less than 0, no commands will be recorded;
When the value is equal to 0, all commands will be recorded .
slowlog-max-len indicates the maximum number of records, the default is 128, for example, set it to 10, when the 11th slow query is inserted, then the data at the head of the queue will be out of line.
These two configurations also support dynamic configuration. When the project has just started and the number of visits is not very large, the value of slow-log-slower-than can be set larger. As the number of visits increases, you may need to change its value to a smaller value. But we don't want to stop the redis service, so we can dynamically modify the configuration.
127.0.0.1:6379> config set slowlog-log-slower-than 1000 OK # 在线修改配置 127.0.0.1:6379> config rewrite OK # 将修改的配置持久化到配置文件中
Operation and maintenance suggestions: When the number of visits is large, we generally recommend setting slowlog-log-slower-than to 1000, or less. When this value is 1000, it means that redis can support up to 1000 concurrent .
Slow query view
The slow query view of redis is different from that of mysql. There are special commands for redis view.
Get slow query log
slowlog get [n], n represents the number of entries, the default is 10
127.0.0.1:6379> slowlog get 1) 1) (integer) 18004 2) (integer) 1589424642 3) (integer) 50 4) 1) "slowlog" 2) "get" 5) "127.0.0.1:58364" 6) "" 2) 1) (integer) 18003 2) (integer) 1589423805 3) (integer) 47 4) 1) "slowlog" 2) "get" 5) "127.0.0.1:58364" 6) "" ……
The first parameter is the log ID number
The second parameter is the occurrence timestamp
The third parameter is the command Execution time (microseconds)
The last one is the command and parameters.
Get the length of slow query
slowlog len
127.0.0.1:6379> slowlog len (integer) 128
Clear the slow query list
127.0.0.1:6379> slowlog reset OK
Persist the slow query log to mysql
Because the length of redis is limited , so when there are many slow queries, there will be loss. We can regularly go to redis to take out the slow query record list and persist it into mysql to prevent this from happening.
The pseudo code is as follows:
while (true) { $slowlen = $redis->slowlog('len'); $slowlogs = $redis->slowlog('get',$slowlen); $insSql = "INSERT INTO slowlog(exec_time,run_time,command) VALUES"; if (is_array($slowlogs) && count($slowlogs)) { foreach ($slowlogs as $slowlog) { $execTime = $slowlog[2]; $runTime = $slowlog[1]; $command = implode(' ', $slowlog[3]); $insSql .= "('$execTime', '$runTime','$command'),"; } $flag = mysqli_query($mysqli, substr($insSql, 0, -1)); if ($flag) { $redis->slowlog('reset'); } } unsleep(10000); }
The above code can realize the persistence of slow queries to MYSQL.
The above is the detailed content of Redis operation and maintenance slow query log. For more information, please follow other related articles on the PHP Chinese website!

RedisisclassifiedasaNoSQLdatabasebecauseitusesakey-valuedatamodelinsteadofthetraditionalrelationaldatabasemodel.Itoffersspeedandflexibility,makingitidealforreal-timeapplicationsandcaching,butitmaynotbesuitableforscenariosrequiringstrictdataintegrityo

Redis improves application performance and scalability by caching data, implementing distributed locking and data persistence. 1) Cache data: Use Redis to cache frequently accessed data to improve data access speed. 2) Distributed lock: Use Redis to implement distributed locks to ensure the security of operation in a distributed environment. 3) Data persistence: Ensure data security through RDB and AOF mechanisms to prevent data loss.

Redis's data model and structure include five main types: 1. String: used to store text or binary data, and supports atomic operations. 2. List: Ordered elements collection, suitable for queues and stacks. 3. Set: Unordered unique elements set, supporting set operation. 4. Ordered Set (SortedSet): A unique set of elements with scores, suitable for rankings. 5. Hash table (Hash): a collection of key-value pairs, suitable for storing objects.

Redis's database methods include in-memory databases and key-value storage. 1) Redis stores data in memory, and reads and writes fast. 2) It uses key-value pairs to store data, supports complex data structures such as lists, collections, hash tables and ordered collections, suitable for caches and NoSQL databases.

Redis is a powerful database solution because it provides fast performance, rich data structures, high availability and scalability, persistence capabilities, and a wide range of ecosystem support. 1) Extremely fast performance: Redis's data is stored in memory and has extremely fast read and write speeds, suitable for high concurrency and low latency applications. 2) Rich data structure: supports multiple data types, such as lists, collections, etc., which are suitable for a variety of scenarios. 3) High availability and scalability: supports master-slave replication and cluster mode to achieve high availability and horizontal scalability. 4) Persistence and data security: Data persistence is achieved through RDB and AOF to ensure data integrity and reliability. 5) Wide ecosystem and community support: with a huge ecosystem and active community,

Key features of Redis include speed, flexibility and rich data structure support. 1) Speed: Redis is an in-memory database, and read and write operations are almost instantaneous, suitable for cache and session management. 2) Flexibility: Supports multiple data structures, such as strings, lists, collections, etc., which are suitable for complex data processing. 3) Data structure support: provides strings, lists, collections, hash tables, etc., which are suitable for different business needs.

The core function of Redis is a high-performance in-memory data storage and processing system. 1) High-speed data access: Redis stores data in memory and provides microsecond-level read and write speed. 2) Rich data structure: supports strings, lists, collections, etc., and adapts to a variety of application scenarios. 3) Persistence: Persist data to disk through RDB and AOF. 4) Publish subscription: Can be used in message queues or real-time communication systems.

Redis supports a variety of data structures, including: 1. String, suitable for storing single-value data; 2. List, suitable for queues and stacks; 3. Set, used for storing non-duplicate data; 4. Ordered Set, suitable for ranking lists and priority queues; 5. Hash table, suitable for storing object or structured data.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

WebStorm Mac version
Useful JavaScript development tools

Dreamweaver CS6
Visual web development tools

Atom editor mac version download
The most popular open source editor

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.