Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Php cache penetration, cache breakdown, cache avalanche differences and solutions

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the knowledge of "php cache penetration, cache breakdown, cache avalanche differences and solutions". Many people will encounter this dilemma in the operation of actual cases, 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!

First, cache penetration

Cache penetration refers to the data that does not exist in the cache and database, and the user keeps making requests, and the user is likely to be an attacker, and the attack will lead to excessive pressure on the database.

Solution:

The interface layer adds parity, such as user authentication verification, timestamp time difference judgment, IP and user ID disable logic, etc.

If the data that cannot be obtained from the cache is not available in the database, the key-value pair can also be written as key-null, and the cache validity period can be set to a short point, such as 30 seconds (if the setting is too long, it will not be available under normal conditions). This prevents the attacking user from repeatedly using the same id to violently attack.

Second, cache breakdown

Cache breakdown means that there is no data in the cache but some data in the database (usually the cache time expires). At this time, because there are so many concurrent users, the read cache does not read the data, and at the same time, it goes to the database to get the data, which causes the pressure on the database to increase instantly and cause too much pressure.

Solution:

Set hotspot data is never out of date.

With mutex, the reference code of mutex is as follows: (logic code, not runnable)

Protected function getData () {customers $ret = $this- > getRedis ($key); if ($ret = = null) {customers try (lock.get) {customers try = $this- > getMysql ($key); customers customers if ($ret! = null) $this- > setRedis ($key, $ret) Caching avalanches: catch {lock.unlock; avalanches} return $ret;}

Cache avalanche refers to a large number of data in the cache to the expiration time, and the huge amount of query data, resulting in excessive pressure on the database and even downmachine. The difference between cache breakdown and cache breakdown is that cache breakdown refers to and check the same data, cache avalanche is that different data are expired, a lot of data can not be found in order to check the database.

Solution:

The expiration time of cached data is set randomly to prevent a large number of data expiration at the same time.

If the cache database is distributed, distribute the hot spot data evenly in different cache databases.

Set hotspot data is never out of date.

That's all for "php cache penetration, cache breakdown, cache avalanche differences and solutions". Thank you for 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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report