In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
This article introduces the relevant knowledge of "Redis encounters concurrency and how to solve avalanche problems". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
1. Solutions to 5 difficult problems such as Redis avalanche, penetration and concurrency
Cache avalanche
The data is not loaded into the cache, or the cache expires in a large area at the same time, resulting in all requests to check the database, resulting in excessive CPU and memory load of the database, or even downtime.
For example, the simple process of an avalanche:
1. Large area failure of redis cluster
2. Cache is invalid, but there are still a large number of requests to access the cache service redis
3. After a large number of redis failures, a large number of requests are transferred to the mysql database
4. The amount of mysql call has soared, and it will soon be unbearable, or even go down directly.
5. As a large number of application services rely on mysql and redis services, this time will soon become an avalanche of server clusters, and finally the website will collapse completely.
How to prevent cache avalanches:
1. High availability of caches
The cache layer is designed to be highly available to prevent large area cache failures. Even if individual nodes, machines, or even computer rooms are down, services can still be provided. For example, Redis Sentinel and Redis Cluster have achieved high availability.
two。 Cache degradation
You can take advantage of local caches such as ehcache (temporarily supported), but mainly limit the access to the source service, isolate resources (circuit breakers), downgrade, and so on.
When traffic soars and there are problems with the service, you still need to ensure that the service is still available. The system can be degraded automatically according to some key data, or the switch can be configured to achieve manual degradation, which will involve the cooperation of operation and maintenance.
The ultimate goal of the downgrade is to ensure that core services are available, even if they are damaging.
For example, recommendation services, many are personalized requirements, if personalized needs can not provide services, can be downgraded to supplement hot data, so as not to cause the front-end page is a big blank.
The system should be sorted out before downgrading, such as which businesses are the core (must be guaranteed), which businesses are allowed not to provide services temporarily (using static page replacement), and in line with the core indicators of the server, set the overall plan, such as:
(1) General: for example, some services can be degraded automatically if they time out occasionally because of network jitter or when the service is online.
(2) warning: if the success rate of some services fluctuates over a period of time (for example, between 95% and 100%), they can be downgraded automatically or manually, and send an alarm.
(3) error: for example, if the availability rate is less than 90%, or the database connection pool is burst, or the number of visits suddenly soars to the maximum threshold that the system can bear, it can be downgraded automatically or manually according to the situation.
(4) serious errors: for example, if the data is wrong for special reasons, an emergency manual downgrade is required.
3.Redis backup and quick warm-up
1) Redis data backup and recovery
2) Fast cache warm-up
4. Drill ahead of time
Finally, it is recommended to rehearse the load and possible problems of the application and backend after the cache layer goes down before the project is launched, and rehearse the high availability in advance to find the problem in advance.
Cache penetration
Cache traversal refers to querying data that does not exist. For example, if you miss the redis from the cache, you need to query from the mysql database, and if you cannot find the data, you will not write to the cache. This will cause this non-existent data to be queried to the database every time, resulting in cache penetration.
The solution is as follows:
If the query database is also empty, directly set a default value to be stored in the cache, so that the second time to get the value in the buffer, instead of continuing to access the database. Set an expiration time or replace the value in the cache when there is a value.
You can set some formatting rules for key, and then filter out Key that do not conform to the rules before querying.
Cache concurrency
Concurrency here refers to concurrency problems caused by simultaneous set key of multiple client of redis. In fact, redis itself is a single-threaded operation, multiple client concurrent operations, according to the principle of first-come, first-served execution, the rest of the blocking. Of course, another solution is to serialize redis.set operations in a queue, which must be executed one by one.
Cache warm-up
Cache preheating means that after the system is online, the relevant cache data is loaded directly into the cache system.
In this way, you can avoid the problem of querying the database and then caching the data when the user requests it. Users directly query pre-warmed cache data!
The solution is as follows:
1. Write a cache to refresh the page directly, and do it manually when you launch.
2. The amount of data is small and can be loaded automatically when the project starts.
The goal is to load the data into the cache before the system goes online.
Second, the three main reasons why Redis is single thread and high concurrency are explained in detail.
High concurrency and Rapid causes of Redis
1.redis is based on memory, and the read and write speed of memory is very fast
2.redis is single-threaded, saving a lot of time for context switching threads.
3.redis uses multiplexing technology to handle concurrent connections. The internal implementation of non-blocking IO adopts epoll and adopts a simple event framework implemented by epoll+ itself. Read, write, close, and connect in epoll are all converted into events, and then take advantage of the multiplexing feature of epoll to never waste any time on io.
The following focuses on the reasons for the speed of single-threaded design and IO multiplexing core design.
Why is Redis single-threaded?
1. Official answer
Because Redis is a memory-based operation, CPU is not the bottleneck of Redis, and the bottleneck of Redis is most likely to be the size of machine memory or network bandwidth. Since single-threading is easy to implement and CPU will not be a bottleneck, it makes sense to adopt a single-threaded solution.
two。 Performance index
With regard to the performance of redis, it is also available on the official website, and ordinary laptops can easily handle hundreds of thousands of requests per second.
3. Detailed reasons
1) No performance consumption of various locks is required
The data structures of Redis are not all simple Key-Value, but also complex structures such as list,hash. These structures may perform very fine-grained operations, such as adding an element after a long list and adding or deleting from the hash.
An object. These operations may require a lot of locks, resulting in a significant increase in synchronization overhead.
In short, in the case of a single thread, there is no need to consider all kinds of locks, there is no lock release operation, and there is no performance consumption caused by possible deadlocks.
2) single-thread multi-process cluster scheme
The power of single thread is actually very powerful, and the efficiency of each core is also very high, so multi-thread can naturally have a higher performance limit than single-thread, but in today's computing environment, even the upper limit of single-machine multi-thread can not meet the needs. What needs to be further explored is the scheme of multi-server clustering, and the technology of multi-thread in these schemes is still useless.
Therefore, single-threaded, multi-process cluster is a fashionable solution.
3) CPU consumption
The use of single thread avoids unnecessary context switching and competition conditions, and there is no CPU consumption caused by multi-process or multi-thread switching.
But what if CPU becomes a Redis bottleneck, or you don't want to leave the other CUP cores of the server idle?
You can consider several Redis processes. Redis is a key-value database, not a relational database, and there are no constraints between the data. As long as the client can tell which key is on which Redis process.
Advantages and disadvantages of Redis single thread
Advantage of single process and single thread
The code is clearer, the processing logic is simpler, there is no need to consider all kinds of locks, there is no lock release operation, there is no performance consumption caused by possible deadlocks, and there is no CPU consumption caused by multi-process or multi-thread switching.
Malpractice of single process and single thread
Multicore CPU performance cannot be achieved, but it can be improved by opening multiple Redis instances on a single machine.
IO Multiplexing Technology
Redis uses network IO multiplexing technology to ensure the high throughput of the system when there are multiple connections.
Multiplexing-refers to multiple socket connections, reuse-refers to the reuse of a thread. There are three main techniques for multiplexing: select,poll,epoll. Epoll is the latest and best multiplexing technology at present.
Here, "multiplex" refers to multiple network connections, and "multiplexing" refers to the reuse of the same thread. Using multi-channel IO multiplexing technology allows a single thread to process multiple connection requests efficiently (minimizing the time consumption of network Redis), and Redis operates data very fast in memory (the operation in memory will not become the performance bottleneck here). The above two points make Redis have high throughput.
Quick summary of high concurrency of Redis
1. Redis is a pure memory database, which is generally a simple access operation. Threads take up a lot of time, and the time spent is mainly concentrated on IO, so the reading speed is fast.
two。 Again, IO,Redis uses non-blocking IO,IO multiplexing, uses a single thread to poll descriptors, converts database opening, closing, reading and writing into events, and reduces context switching and competition during thread switching.
3. Redis adopts a single-threaded model, which ensures the atomicity of each operation and reduces the context switching and competition of threads.
4. In addition, the data structure also helps a lot. Redis uses hash structure throughout the process to read fast, and there are some special data structures that optimize the data storage, such as compressing tables, compressing and storing short data, and skipping tables, using ordered data structures to speed up reading.
5. In addition, Redis uses its own event separator, which is relatively efficient, uses non-blocking execution mode internally, and has relatively large throughput capacity.
III. Detailed explanation of Redis cache and MySQL data consistency scheme
Demand cause
In high concurrency business scenarios, the database is the weakest link for users to access concurrently in most cases. Therefore, you need to use redis to do a buffering operation so that the request accesses the redis first, rather than directly accessing a database such as MySQL.
In this business scenario, the read data is cached from Redis, and the business operation is generally carried out according to the process shown in the figure below.
There is generally no problem with reading Redis steps, but when it comes to data updates: database and cache updates, it is easy to have data consistency problems between cache (cache) and database (cache).
Whether you write the MySQL database first, then delete the Redis cache, or delete the cache first, and then write to the library, data inconsistencies may occur. To give an example:
1. If the cache Redis is deleted, another thread reads it before writing to the library MySQL, and finds that the cache is empty, then read data from the database and write to the cache. At this time, the cache is dirty.
two。 If you write the library first, the thread writing to the library goes down before deleting the cache, and the cache is not deleted, then data inconsistencies will also occur.
Because write and read are concurrent and the order cannot be guaranteed, there will be data inconsistencies between the cache and the database.
The Tathagata? Here are two solutions, easy before difficult, combined with business and technical costs to choose to use.
Cache and database consistency solution
1. The first scheme: using delayed double deletion strategy
Redis.del (key) operation is performed before and after writing the library, and a reasonable timeout is set.
The pseudo code is as follows:
Public void write (String key,Object data) {redis.delKey (key); db.updateData (data); Thread.sleep (500); redis.delKey (key);}
The specific steps are:
Delete the cache first; then write to the database; sleep for 500 milliseconds; delete the cache again.
So, how do you determine this 500 milliseconds, and how long should it be dormant?
You need to evaluate the time-consuming business logic of reading data for your project. The purpose of this is to ensure that the read request ends, and the write request removes the cache dirty data caused by the read request.
Of course, this strategy also takes into account the time consuming of redis and database master-slave synchronization. The final dormancy time for writing data: add a few hundred ms on the basis of reading the data business logic. For example: dormant for 1 second.
Set cache expiration time
In theory, setting an expiration time for the cache is the solution to ensure ultimate consistency. All write operations are based on the database, and as long as the cache expiration time is reached, subsequent read requests will naturally read the new value from the database and backfill the cache.
The disadvantages of the scheme
Combined with the double delete policy + cache timeout setting, the worst-case scenario is that the data is inconsistent during the timeout and increases the time-consuming of the write request.
2. The second scheme: update cache asynchronously (synchronization mechanism based on subscription binlog)
The overall idea of technology:
MySQL binlog incremental subscription consumption + message queuing + incremental data update to redis
Read Redis: basically write hot data in Redis MySQL: add, delete and modify MySQL update Redis data: MySQ data operation binlog, to update to Redis
Redis update
1) data operations are mainly divided into two parts:
One is full (all data is written to redis at once) and the other is increment (real-time update)
Here we are talking about increments, which refers to mysql's update, insert, and delate change data.
2) analyze after reading binlog, and use message queue to push and update the redis cache data of each station.
In this way, once there are new write, update, delete and other operations in MySQL, the messages related to binlog can be pushed to Redis,Redis, and then the Redis can be updated according to the records in binlog.
In fact, this mechanism is very similar to the master-slave backup mechanism of MySQL, because the master and slave of MySQL also achieve data consistency through binlog.
Here you can use canal (an open source framework of Ali), through which you can subscribe to MySQL's binlog, and canal imitates the backup request of mysql's slave database, which makes the data update of Redis achieve the same effect.
Of course, the message push tool here you can also use other third parties: kafka, rabbitMQ, etc., to implement the push update Redis.
This is the end of the content of "how to solve the problem of concurrency and avalanche in Redis". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.