Redis is officially available starting from 2.8Sentinel(哨兵模式)
The method of master-slave switching technology is: when the host server goes down, one server needs to be manually switched to the master server, which requires human intervention, is time-consuming and labor-intensive, and will also cause the service to be unavailable for a period of time. This method is not recommended, so consider using it most of the time哨兵模式
Sentinel mode is a special mode. First of all, Redis provides sentinel commands. Sentinel is an independent process. As a process, it will run independently.
Principle: Sentinel monitors multiple running Redis instances by sending commands and waiting for a response from the Redis server.
Sentinels serve two purposes:
By sending commands, let the Redis server return to monitor its running status, including the master server and slave server.
When Sentinel detects that the master is down, it will automatically switch the slave to the master, and then 发布订阅模式notify other slave servers and modify the configuration file to allow them to switch hosts.
However, problems may arise when a sentinel process monitors the Redis server. For this reason, multiple sentinels can be used for monitoring.
Each sentinel will also be monitored, thus forming a multi-sentry mode.
Assume that the main server is down and Sentinel 1 detects this result first. The system will not proceed immediately failover(故障转移). It is just that Sentinel 1 subjectively believes that the main server is unavailable. This phenomenon is called 主观下线. When the subsequent sentinels also detect that the main server is unavailable, and the number reaches a certain value, a vote will be held between the sentinels, and the voting results will be initiated by one sentinel for operation failover(故障转移). After the switch is successful, each sentry will use the publish-subscribe mode to switch the slave server it monitors to the host. This process is called客观下线
15.2 Testing
The current status of the server is one master and two slaves
Configure the sentinel configuration file:sentinel.conf
[root@iz2ze0l46im3eg03queta2z bin]# redis-sentinel myredisconfig/sentinel.conf 26866:X 04 Nov 202016:47:08.573 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo26866:X 04 Nov 202016:47:08.573 # Redis version=6.0.9, bits=64, commit=00000000, modified=0, pid=26866, just started26866:X 04 Nov 202016:47:08.573 # Configuration loaded
_._
_.-``__ ''-._
_.-```.`_. ''-._ Redis 6.0.9 (00000000/0)64 bit
.-`` .-```.```\/ _.,_ ''-._
( ' , .-`|`, ) Running in sentinel mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 26379
| `-._ `._ / _.-'| PID: 26866`-._ `-._ `-./ _.-' _.-'|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | http://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'||`-._`-._ _.-'_.-'|`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'`-._ _.-'
`-.__.-'
26866:X 04 Nov 202016:47:08.574 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.26866:X 04 Nov 202016:47:08.576 # Sentinel ID is 276060e845a7365dbef0fe397a9c4907dab24e9026866:X 04 Nov 202016:47:08.576 # +monitor master myredis 127.0.0.1 6379 quorum 126866:X 04 Nov 202016:47:08.577 * +slave slave 127.0.0.1:6380 127.0.0.1 6380 @ myredis 127.0.0.1 637926866:X 04 Nov 202016:47:08.579 * +slave slave 127.0.0.1:6381 127.0.0.1 6381 @ myredis 127.0.0.1 6379
At this time, Sentinel successfully monitors host 6379. When the host is disconnected, a new host will be selected using the voting algorithm in the slave.
15.3 Advantages and Disadvantages of Sentry Mode
advantage:
Sentinel cluster, based on the master-slave replication model, has all the advantages of master-slave replication.
Master and slave can be switched, faults can be transferred, and the system has better availability.
Sentry mode is an upgrade of the master-slave mode. From manual to automatic, the system is more robust.
shortcoming:
Redis is not easy to expand online. Once the number of clusters reaches the upper limit, online expansion will be very troublesome.
Implementing the configuration of sentinel mode is actually very troublesome. There are many configuration items in it.
The use of Redis cache greatly improves the performance and efficiency of applications, especially in data query. At the same time, he also brought some problems. Among them, the most critical problem is 数据的一致性the problem (the transaction cannot guarantee atomicity during runtime). Strictly speaking, this problem has no solution.
If the consistency requirements of the data are very high, caching cannot be used
Other typical problems: cache penetration, cache avalanche, and cache breakdown. Currently, there are some popular solutions
16.2 Cache penetration
Caused by inability to find data
16.2.1 Overview
The concept of cache penetration is very simple. The user wants to query a piece of data and finds that the redis memory database does not have it. That is, the cache does not hit, so it queries the persistence layer database. No found, this query failed.
When there are many users, the cache does not hit (instant kill), so they all request the persistence layer database, which will put a lot of pressure on the persistence layer database. At this time, it is equivalent to cache penetration. If the database cannot be found, there will be no cache, and database access will continue.
16.2.2 Solution
bloom filter
Bloom filter is a data structure that stores all possible query parameters in the form of Hash to quickly determine whether this value exists. The control layer first performs interception and verification. If the verification fails, it will be returned directly, which reduces the storage cost. system pressure
Cache empty objects
When the storage layer misses, the empty object returned in time will also be cached, and an expiration time will be set. Subsequent accesses to the data will be obtained from the cache, protecting the back-end data source. That is, if a request is not found in the cache or database, an empty object will be placed in the cache to handle subsequent requests.
There are two problems with this approach:
If vacancies can be cached, it means that the cache needs more space to store more keys, because there may be many keys with null values.
Even if an expiration time is set for null values, there will still be inconsistencies between the data in the cache layer and the storage layer for a period of time, which will have an impact on businesses that need to preserve consistency.
16.3 Cache breakdown (too much volume, cache expires)
The query volume is too large and the cache expires.
16.3.1 Overview
Compared with cache penetration, cache penetration is more purposeful. For an existing key, when the cache expires, there will be a large number of requests at the same time. These requests will penetrate to the DB, resulting in a large amount of instantaneous DB requests and sudden pressure. Increase will break the cache. This is cache breakdown, which is caused by the unavailability of the cache for one of the keys, but other keys can still use cached responses.
16.3.2 Solution
设置热点数据永不过期: In this way, hotspot data will not expire, but when the Redis memory space is full, part of the data will be cleared, and this solution will take up space. Once there is more hotspot data, it will take up part of the space.
加互斥锁(分布式锁): Before accessing the key, use SETNX (set if ont exist) to set another short-term key to lock access to the current key, and then delete the short-term key after the access. Ensure that only one thread accesses it at the same time. This puts very high requirements on locks
16.4 Cache Avalanche
16.4.1 Overview
Cache avalanche means that there is an error in the cache layer and it cannot work properly. Therefore, all requests will reach the storage layer, and the number of calls to the storage layer will increase dramatically, causing the storage layer to hang up. Such a large number of keys are set to the same expiration time, but all caches will expire at the same time, causing an instant DB Heavy requests and sudden increase in pressure caused an avalanche
16.4.2 Solution
redis高可用: Since redis may go down, add a few more redis, so that after one goes down, the others can continue to work. In fact, it is a cluster.
限流降级: After the cache expires, control the number of threads reading the database write cache through locking or queuing. For example: only one thread is allowed to query data and write cache for a certain key, while other threads wait
数据预热: The meaning of data preheating is to pre-access possible data before formal deployment, so that some data that may be accessed in large quantities will be loaded into the cache. Before a large concurrent access is about to occur, manually trigger the loading of different keys in the cache and set different expiration times to make the cache invalidation time points as even as possible.