汉英对照:
Chinese-English Translation:
缓存穿透是指缓存和数据库中都没有的数据,而用户不断发起请求,如发起的数据特别大而不存在的数据。缓存击穿是指缓存中没有但数据库中有的数据,由于并发用户特别多,同时读缓存没读到数据,同时数据库取数据引起数据库压力瞬间增大,造成过大压力。
Cache penetration refers to the data that does not exist in the cache and database, and the user continuously requests, such as the data that is large and does not exist. Cache breakdown refers to the data that is not in the cache but in the database. Because there are many concurrent users, reading the cache at the same time does not read the data. At the same time, the database fetching data causes the database pressure to increase instantaneously, resulting in excessive pressure.
缓存处理流程是由前台请求,后台先从缓存中取数据,取到直接返回结果,取不到时从数据库中取,数据库取到更新缓存,并返回结果,数据库也没取到,那直接返回空结果。
The cache processing flow is a front-end request. The back-end takes the data from the cache first and returns the result directly. If the data cannot be retrieved, it takes the update cache from the database and returns the result. If the database does not get the result, it directly returns the empty result.
缓存穿透
Cache penetration
描述:
Description:
缓存穿透是指缓存和数据库中都没有的数据,而用户不断发起请求,如发起为 id 为“-1”的数据或 id 为特别大不存在的数据。这时的用户很可能是攻击者,攻击会导致数据库压力过大。
Cache penetration refers to the data that does not exist in the cache or database, and the user continuously requests, such as the data with ID “- 1” or the data with ID being extremely large and nonexistent. At this time, the user is likely to be an attacker, and the attack will lead to excessive database pressure.
解决方案:
Solution:
接口层增加校验,如用户鉴权校验,id 做基础校验,id<=0 的直接拦截;
The interface layer adds verification, such as user authentication verification, basic verification for ID, and direct interception for ID & lt; = 0;
从缓存取不到的数据,在数据库中也没有取到,这时也可以将 key-value 对写为 key-null,缓存有效时间可以设置短点,如 30 秒(设置太长会导致正常情况也没法使用)。这样可以防止攻击用户反复用同一个 id 暴力攻击。
The data that cannot be retrieved from the cache is not retrieved from the database. In this case, the key value pair can also be written as key null, and the cache effective time can be set to a shorter point, such as 30 seconds (if the setting is too long, it will lead to the normal situation that it cannot be used). This can prevent the attacker from repeatedly using the same ID to attack violently.
缓存击穿
Cache breakdown
描述:
Description:
缓存击穿是指缓存中没有但数据库中有的数据(一般是缓存时间到期),这时由于并发用户特别多,同时读缓存没读到数据,又同时去数据库去取数据,引起数据库压力瞬间增大,造成过大压力
Cache breakdown refers to the fact that there is no data in the cache but there is data in the database (generally, the cache time expires). At this time, because there are many concurrent users, reading the data in the cache at the same time, and going to the database to retrieve the data at the same time, the database pressure increases instantaneously, resulting in excessive pressure
解决方案:
Solution:
设置热点数据永远不过期。
Set the hotspot data to never expire.
加互斥锁。
Add mutually exclusive lock.
缓存雪崩
Cache avalanche
描述:
Description:
缓存雪崩是指缓存中数据大批量到过期时间,而查询数据量巨大,引起数据库压力过大甚至 down 机。和缓存击穿不同的是, 缓存击穿指并发查同一条数据,缓存雪崩是不同数据都过期了,很多数据都查不到从而查数据库。
Cache avalanche refers to the large amount of data in the cache to the expiration time, and the huge amount of query data causes the database pressure to be too large or even down machine. Different from cache breakdown, cache breakdown refers to concurrent query of the same data. Cache avalanche means that different data have expired, and many data can not be found to query the database.
解决方案:
Solution:
缓存数据的过期时间设置随机,防止同一时间大量数据过期现象发生。
The expiration time of cache data is set randomly to prevent a large number of data expiration at the same time.
如果缓存数据库是分布式部署,将热点数据均匀分布在不同搞得缓存数据库中。
If the cache database is distributed, the hot data will be evenly distributed in different cache databases.
设置热点数据永远不过期。
Set the hotspot data to never expire.