Redis study notes

I recently read a book and recorded some helpful knowledge points in the book for the use of Redis:
1. Do not execute keys * on Redis with a lot of keys. The single-threaded model will cause blocking when traversing the keys in full.
2. Do not configure auto-save RDB on a Redis with a large amount of write operations, it will block during fork operations
3. There are three ways to start Redis: default configuration (run redis-server directly), running configuration (carry startup parameters when executing redis-server), configuration file startup (start by specifying a configuration file), it is recommended that the production environment use the configuration file to start
4. Redis-cli connects to the server in two ways:
                Interactive: redis-cli -h {host} -p {port};
                Imperative: redis-cli -h {host} -p {port} {command} 
5. To stop the Redis service, you need to initiate a stop command from the client: redis-cli shutdown
                Stopping the service by command can ensure that data will not be lost, because when the service is stopped, the connection with the client will be disconnected first and a persistent file will be generated.
                In addition, the Redis service can be forcibly killed by kill -9, but the persistence operation will not be performed, and the buffer will not be closed gracefully, which may cause AOF and replication to lose data.
                Cold knowledge: redis-cli shutdown can specify whether to generate a persistent file nosave| save before closing Redis. If you can't use this command directly, you can connect to the server first and enter shutdown in the interactive interface
6. Global command:
                keys * View all keys, dbsize view the total number of keys;
                When dbsize is executed, it does not traverse all keys, but directly obtains the built-in key total variable of Redis. The keys * command will traverse all keys. When Redis saves a large number of keys, the online environment is prohibited.
                exists key Check if the key exists
                del key Exclusion key
                Expire key seconds Add expiration time to key
                You can view the remaining expiration time through the ttl key
                type key View the data structure type of the key
7. Redis data structure type
                string basic type, common commands do not want to write
                hash basic type, common commands do not want to write
                List basic type, common commands do not want to write
                set is an unordered collection, and the members of the collection are unique, which means that duplicate data cannot appear in the collection
                zset sorted set, each member of zset has a score corresponding to it, and the score can be repeated
 
8. Each data structure has two or more internal encoding implementations. You can view the internal encoding through the object encoding key. Redis’s move is to improve the internal encoding and adapt to different scenarios at any time.
9. Redis is a single-threaded processing command. A command will not be executed immediately after reaching the server from the client, but will enter a queue and be executed one by one. There will be no two commands executed at the same time.
10. Redis uses io multiplexing to solve single-threaded performance problems
11. The single-threaded model has requirements for the execution time of each command. If the command execution time is too long, it will block other commands.
12. set key value option:
                ex seconds Set the second-level expiration time for the key setex key seconds value or set key value ex seconds
                px milliseconds Set the millisecond expiration time for the key set key value px milliseconds
                The nx key must not exist before it can be set successfully. It is used to add setnx key value or set key value nx
                xx is the opposite of nx, requires that the key must exist and is used to update set key value xx
                Cold knowledge: redis provides setex and setnx separately, and other options do not provide separate commands; if multiple client colleagues execute the setnx key value, according to the single-threaded model and command characteristics, only one client can be successfully set, and setnx can be set. As an implementation of distributed locks
13. Batch set value mset [key value ......], batch get value mget [key .....]; batch operation commands can improve development efficiency and execution efficiency; it should be noted that it is not one command, but multiple commands will be generated It is submitted to the server at one time, and finally executed by the server one by one, so batch operations cannot be used without restraint. Because there are too many mxxx commands, the network transmission time will be shortened, but the execution time of the server will be prolonged, causing command blocking.
14. Redis provides auto-increment incr (self-increment), decr (self-decrement), incrby (self-increment specified number), decrby (self-decrement specified number), incrbyfloat (self-increment floating-point number)
15. Description of redis data types, list can be used as a subscription publishing mode to implement blocking queues, set can be used to compare sets and take differences, zset ordered queue application scenarios are not much to discuss, string comparison basics let's not talk
16. Redis supports key migration
                move supports internal migration of redis services 
                dump+restore supports migration between different redis instances and does not guarantee atomicity
                migrate encapsulates dump+restore and is atomic
 
17. Traverse key
                1. Fully traverse keys *, support *, ? , []
                delete all keys starting with abc redis-cli keys abc* | xargs reds-cli del
                2. Progressively traverse scan, if you need to traverse all keys, you need to execute scan multiple times
                Syntax: scan cursor [pattern] [count] cursor is a required parameter and is a cursor. The first traversal starts from 0, and the value of the current cursor is returned every time the scan is completed. Knowing that the cursor value is 0, means the traversal is over. This The cursor is a bit contrary to common sense, don't mind these details; parrrn is an optional parameter, equivalent to the filtering parameter of keys; count is an optional parameter, indicating how many keys to traverse, the default value is 10, this parameter can be appropriately increased (I seem to Found a way to redis keys paging)
                In addition, redis also provides hscan(hash) sscan(set) zscan(zset)
                Scan is not perfect. If there is a key change during the scan process, scan cannot take care of it, and some keys may not be traversed.
 
18. Redis-cli can use select dbindex to switch the database, the default is 0, this one is in vain, the official is about to remove this function
19. Flushall is to clear all databases, flushdb only clears the current database, and it will also cause blocking when there is a lot of data
20. Redis supports slow query analysis and does not want to write, because slow query analysis will also occupy the command queue position, which will cause blocking if the time is too long
21, redis shell does not want to write, general commands and shell parameters are enough
22. Although pipline can reduce network transmission costs, too many command lines will still cause blocking
23. Transactions and lua, transactions are not discussed, but lua can be understood. If you are familiar with scripting languages ​​such as python and groovy, it is not difficult to learn. Lua is executed atomically, which can help you develop and maintain your own custom commands.
24, bitmaps do not intend to understand for the time being
25. HyperLogLog does not intend to understand for the time being
26. We have special mq middleware for publish and subscribe...
27. GEO can be used to realize functions such as nearby location, shaking, etc. This solr can also do it, depending on what you need to choose                
28. The Redis communication protocol is RESP, which is based on TCP              
29. jedis parameter description
                host
                port
                connectionTimeout client connection timeout
                soTimeout client read and write timeout
30, RDB endurance
        Manually trigger save and bgsave, save will block the current server until the RDB process is completed, and it is not recommended to use the online environment; bgsave will perform the fork operation, create sub-threads, and the persistence is the responsibility of the sub-process, which ends automatically after completion, blocking only occurs in In the fork phase, the save command has been deprecated. bgsave is automatically executed during debug reload and shutdown.
31. Blocking
        Unreasonable use of Api or data structures, finding slow queries, finding big objects bigkeys
        CPU saturation problem redis-cli —stat Get the current usage for troubleshooting. It is not recommended to deploy with other multi-core CPU-intensive services for operation and maintenance. In addition, you can bind CPU
        Although there is bgsave for persistence-related blocking, it still needs fork every time. When the fork time is too long, blocking will occur
32. Redis set password config set require pass 123; enter password auth 123
33. jedispool depends on Apache's common-pool, and the basic configuration items also come from common-pool
34. Check the maximum number of connections on the redis server config get maxclients; set the maximum number of connections redis-server --maxclients 100000 when redis Qidong (however, it is useless, it should be the wrong way of opening it)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326153031&siteId=291194637