Questions about cache avalanche and cache penetration

Source: Internet
Author: User
Tags lock queue

The cache Avalanche Cache Avalanche is due to the original cache invalidation (expired) and the new cache is not in the period. All requests to query the database, and the database CPU and memory caused great pressure, serious will cause database downtime. Thus forming a series of chain reaction, causing the whole system collapse.

(1) In this case, the general concurrency is not particularly high time, the most used solution is lock queue.

[Java]  View Plain  copy   public object getproductlistnew ()             {                const int cachetime = 30;                const string cacheKey =  "Product_list";                const string lockKey =  cachekey;                              var cacheValue =  Cachehelper.get (CacheKey);                if  (cachevalue != null)                 {                   return  cachevalue;               }               else                {                    lock  (lockkey)                     {                        cachevalue  = cachehelper.get (CacheKey);                        if  (cachevalue != null)       &Nbsp;                 {                             return cacheValue;                        }                        else                        {                            cachevalue =  getproductlistfromdb ();  //here is generally  sql query data.                                           cachehelper.add (cachekey, cachevalue,  CacheTime);                        }                                         }                    return cacheValue;                }           }   


(2) Lock queue is only to alleviate the pressure of the database, and does not improve the system throughput. Assuming that the key is locked during the cache rebuild during high concurrency, this is the 1000 requests that are 999 blocks in a block. This also causes the user to wait for a timeout, which is a stopgap method.
Another solution is to add the appropriate cache tag for each cached data, record whether the cache is invalidated, and update the data cache if the cache flag is invalidated.

[Java]  View Plain  copy   public object getproductlistnew ()             {                const int cachetime = 30;                const string cacheKey =  "Product_list";                //cache tags.                const string  cachesign = cachekey +  "_sign";                                var sign = cachehelper.get (cachesign);                //Get cache values                var cacheValue =  Cachehelper.get (CacheKey);                if  (sign != null)                 {               &NB

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.