On the cache system of three questions

A. Ubiquitous buffer
cache in a computer system is everywhere, there Cache L1-L3 at the CPU level, there is acceleration TLB virtual address and the physical address, there Redis database cache memory and the like in the application layer in Linux, there are local cache in the browser, the phone has a local file cache, and so on.
Visible, the cache in the computer system has a very important position, the main role is to improve the response speed and reduce disk reads and so on, this article focuses on caching system in highly concurrent systems. Sentence status caching system in highly concurrent systems, then, is:

If the system is highly concurrent kebabs, then the cache system is that a handful of cumin ......

II. High concurrency system cache

  • The role of caching system

Caching system in the role of highly concurrent systems is huge, there is no cache system is difficult to support C50K (Perhaps this value has been very optimistic) or more scenes.
SSD or mechanical disk-based database systems, read and write speed is much slower than RAM, so the database disk media alone can not support high concurrency, you can think of is to protect the disk cache database is a barrier disk database.

  • Access the cache system and database system


With less reading and writing scenario as an example (the actual reading and writing scene is less), to see how the request is to get a response, simple process: After the request arrives, the service thread first access the cache, if the cache hit return, if not hit It continued to request the disk database systems, and the result is written back to the cache system and increasing aging time, so in the time-out and then you can have the same cache hit when a request arrives. These are the interaction between high concurrency system cache and disk database systems, client requests, follow-up analysis of the problem, is also based on this process unfolded.

Three three major issues caching system of
three issues on the network for three articles caching problem a lot, mainly mentioned:

  1. Cache Cache Avalanche Avalanche
  2. Cache Cache Penetration penetration
  3. Cache breakdown Hotspot Invalid

For the three terms I have been above I could not tell, and do not mind a clear difference, so expect to Google to see how crooked nuts is to say, however, that the English expression above in English, and Chinese translations are basically the same , so only a strong mind.

A. cache avalanche problem

The so-called avalanche of snow and ice that had been supporting, relying on a moment to lose, and instantly flooded down. This scene reminds me of the 2011 release of Conan Movie "silent fifteen minutes," Conan Kitazawa village in order to save the reservoir avalanche village made:

Note: The picture shows Conan was buried by an avalanche, rescue golden period when Mao Lilan looking for his shot

Avalanche seen really horrible, back to high concurrency system, if the cache system failure, a large number of requests from the cache data request can not be completed, and therefore the full amount of the surging toward the disk database systems, resulting in the database were killed and the total collapse of the entire system.

Avalanche caching solutions

From the point of view the main reason is not enough cache system availability, thereby increasing the cache system stability and availability is necessary, for the purposes of use Redis as cache system requires the use of Sentinel Sentinel mechanism, clustering, persistence, etc. to improve the cache system HA.

 In addition to ensuring the other hand HA cache system other than the service itself also needs to support downgrade, you can use Netflix's Hystrix blown to implement the service, demotion, current limiting to reduce the extent of the problem when an avalanche occurs. Do not let that white is completely dead service on the line, as heavy snow closed the high-speed certainly completely impassable, and at least some of the traffic jam that is slow to take, if not clear, recall that each year you return home ticket is how to grab back from 12306 .

 

B. Cache penetrating questions

Penetrate image point is: The request came in a circle as if nothing passes through the transparent zone the same.

Cache penetration in highly concurrent systems, in fact, if such a request req need no key in the cache, then the business thread will access the disk database systems, however, do not have this key-disk database, but unfortunately can only return to the thread business null, vain deal circle.
Small probability event in highly concurrent systems almost become a necessity, that is, if a certain period of time a large number of malicious blasting requested key does not exist, then the service will have to deal with these requests do not exist, leading to a normal request can not be processed to appear problem.
For chestnut:

Ramen Museum staff and chefs are not allowed to refuse the consumer has come in, but limited business scope Ramen Museum, in which case the malicious consumer ordered a 5 pounds of lobster in Australia, through waiters and cooks are unable to respond to this demand, so it is ultimately rejected, this time in turn come 1000 this malicious consumers, the basic noodle shops to break dishes.

Cache penetrate Solutions

Highly concurrent systems, too, whether there are effective screening before deciding whether to read the key is very important, common practice are:

 a. The key does not exist to write about null, this again is equivalent to hit, in fact, significant limitations of this approach, today is 5 pounds of lobster, crab into 6 pounds tomorrow, caching systems and databases to store large amounts of useless key itself is meaningless, it is generally not recommended.
 B. Another idea is converted to identify problems, similar to the search for a key if the data is present in the mass, in consideration of spatial complexity and temporal complexity, Bloom filter is generally used to achieve.

Slow Bloom filter Profile

Bloom filter is a good thing, in 1970, proposed by Bloom, it is actually a long series of random binary vector and mapping functions.

Bloom system can be achieved include: spam detection, search spiders reptiles url to heavy, mainly by means of K hash function and an oversized bit hash array to reduce false positives caused by the conflict itself, and thus improve the recognition accuracy .
Bloom filter there are some false positives, determine if there may not necessarily exist, but if it is judged that there must not exist, so just to solve the cache penetration of key scenes look at the use, in fact, many systems are based cloth Long filter to solve the problem of penetrating the cache.

 

C. Cache breakdown issue

Cache is a breakdown situation: As the hot data caching system time has expired, if there is no expiration date on the cause of the inconsistency of main memory and cache data, so the expiration time is generally not too long, imagine a certain time Meanwhile expired batch of hot data in the cache system, then this part will request data on-disk database systems.
From the description of view a bit like a small-scale avalanche, but the pressure on the database is very small, but will affect the concurrent performance, but in multithreaded scenarios buffer breakdown is often the case, contrary cache penetration and frequency of avalanche not as good as cache breakdown, so study the breakdown of larger significance .

缓存击穿解决方案

可以采用的方案大概有几种:

a. 在设置热点数据过期时间时尽量分散,比如设置100ms的基础值,在此基础上正负浮动10ms,从而降低相同时刻出现CacheMiss的key的数量。
b. 另外一种做法是多线程加锁,其中第一个线程发现CacheMiss之后进行加锁,再从数据库获取内容之后写到缓存中,其他线程获取锁失败则阻塞数ms之后再进行缓存读取,这样可以降低访问数据数据库的线程数,需要注意在单机和集群需要使用不同的锁,集群环境使用分布式锁来实现,但是由于锁的存在也会影响并发效率。
c.还有一种办法是使用类似于Redis的SETNX命令,这种貌似存在问题,存在一个gap间隙请求处于无法从缓存获取数据也无法从主存获取数据的未决状态。
d. 最后一种方法是在业务层对使用的热点数据查看是否即将过期,如果即将过期则去数据库获取最新数据进行更新并延长该热点key在缓存系统中的时间,从而避免后面的过期CacheMiss,相当于把事情提前解决了。

缓存击穿的解决方法都有一定的权衡,实际中根据自己的需求来解决,不过缓存击穿的影响一般来说并不会太大,或许在你的服务跑了很久之后你才意识到会有缓存击穿问题。

Guess you like

Origin www.cnblogs.com/backnullptr/p/11993876.html