In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the solution of redis cache penetration, which is very detailed and has a certain reference value. Friends who are interested must finish it!
Caching technology can be used to reduce the pressure on the database and improve access efficiency. At present, more and more attention is paid to caching in enterprise projects. But caching does not mean casually joining the project. Integrating the cache into the project is the first step. The penetration problem caused by cache and the snow jump problem caused by cache are all problems that we urgently need to solve. This article will share the solutions of my usual project to you for reference.
I. the principle of cache penetration
The normal use of the cache is shown in the figure:
As shown in the figure, the process of using the cache:
1. Take the data from the cache first, and if you can get it, return the data directly to the user. In this way, there is no need to access the database, reducing the pressure on the database.
2. If there is no data in the cache, the database will be accessed.
There will be a BUG in it, as shown in the figure:
As shown in the figure, caching is like a firewall for a database, which puts frequently requested data into the cache, thus reducing the pressure on the database. But if someone attacks maliciously, it's easy to penetrate your cache and put all the pressure on the database. For example, in the image above, your cached key is all positive integers, but I use negative numbers as key to access your cache, which will lead to penetration of the cache and direct pressure on the database.
Second, the causes of cache penetration
The problem with cache penetration must be in the case of large concurrency. Based on this premise, we analyze the reasons for cache penetration as follows:
1. Malicious attack, guess how your key is named, and then estimate that it will be accessed using a key that will not be in your cache.
2. When the data is accessed for the first time, if there is no data in the cache, all requests will be pressed to the database in concurrent scenarios.
3. The data in the database is also empty, so even if you access the database, you cannot get the data, so there must be no corresponding data in the cache. This can also lead to penetration.
Third, solve cache penetration
Cache penetration is the reason to avoid penetration step by step, as shown in the figure:
As shown in the figure above, the steps to solve the problem are as follows:
1. When the web server is started, the data that may be accessed frequently and concurrently is written to the cache in advance. -this avoids a large number of requests and causes queue blocking in step 3.
2. Standardize the naming of key and unify the entry of query and write cache. In this way, the key specification is detected at the entrance. -in this way, malicious key is blocked.
3. Synchronized double detection mechanism. In this case, we need to use the Synchronized mechanism to query whether the corresponding key exists in the cache before synchronizing the code block, and then query again whether there is a key to be queried in the cache in the synchronized code block. The purpose of this "double checking" is to avoid meaningless database access (which is also a strict way to avoid penetration) caused by concurrency scenarios.
This step leads to queuing, but as we said in the first step, in order to avoid a large number of queues, a large number of predictable requests can be written to the cache in advance.
4. No matter whether there is data in the database or not, the corresponding key is saved in the cache. The value is empty. -this is to avoid the absence of this data in the database, resulting in a trivial penetration cache to access the database.
5. If there are too many null values in step 4, it will also cause memory exhaustion. Results in unnecessary memory consumption. To do this, clean up the null key regularly. Prevent memory from being maliciously occupied. Causes the normal function to fail to cache data.
These are all the solutions to redis cache traversal. Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
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.