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

The method to solve the breakdown of redis cache

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

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

This article mainly introduces the method of solving redis cache breakdown, which has certain reference value, and friends who need it can refer to it. I hope you all have a lot to gain after reading this article. Let's take a look at it together.

Distributed caching is a technology often used by website servers. In business scenarios where there are more reads and less writes, caching can effectively support high concurrent access and protect data sources such as back-end databases. There are many distributed caches on the market, such as Redis, Memcached and Ali's Tair, etc. No matter which cache product we use, we will basically encounter cache breakdown, cache invalidation and hot key problems. How to effectively prevent these problems is also a difficult problem that we must solve while enjoying the dividends brought by cache.

Usually when we use cache, we first check whether there is cache, if there is cache content directly returned, if not, directly query the database and then cache query results returned, for example, as shown in the figure below,

Cache Breakdown: Description

Query a database does not exist in the data, such as product details, query a non-existent ID, every time will access the DB, if someone malicious damage, it is likely to directly cause excessive pressure on the DB.

Solution:

When querying data through a key, if the corresponding data in the database does not exist, we set the value corresponding to this key to a default value, such as "NULL", and set a cache expiration time. In this case, all accesses through this key are blocked by the cache before the cache expires. Later, if the data corresponding to this key exists in the DB, after the cache fails, the data can be accessed through this key to get the new value.

Cache invalidation: Description

In a highly concurrent environment, if the cache corresponding to the key fails, multiple processes will query the DB at the same time, and then set the cache at the same time. At this time, if this key is a hot key in the system or the number of simultaneous failures is relatively large, the DB access volume will increase instantaneously, causing excessive pressure.

Solution:

The cache invalidation time of keys in the system is staggered evenly to prevent a large number of cache invalidation corresponding to keys at a uniform time point;

Redesign the use of cache, when we query the data by key, first query the cache, if the cache is not found at this time, lock it through distributed lock, the process that obtains the lock checks DB and sets the cache, and then unlocks it; other processes wait if they find a lock, and then return to the cache data after unlocking or query DB again.

Hot Key: Description

The challenge of storing values corresponding to certain Keys in the cache (possibly for an app and a promotional item) on one machine in the cluster, causing all traffic to flock to the same machine, is that it cannot be solved by increasing machine capacity.

Solution:

Client hotspot key cache: cache the hotspot key corresponding to value locally on the client, and set an expiration time. For each read request, it will first check whether the key exists in the local cache, if it exists, it will return directly, and if there is no machine to access the distributed cache.

Distribute the hotspot key into multiple subkeys and store them on different machines in the cache cluster. The values corresponding to these subkeys are the same as the hotspot key. When querying data through a hotspot key, a subkey is randomly selected through a hash algorithm, and then the cache machine is accessed, so that the hotspot is distributed to multiple subkeys.

Thank you for reading this article carefully. I hope Xiaobian can share the method of solving redis cache breakdown to help everyone. At the same time, I hope that everyone will support you a lot. Pay attention to the industry information channel. If you encounter problems, find detailed solutions waiting for you to learn!

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

Database

Wechat

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

12
Report