(Reprint) redis cache penetration, avalanche breakdown scenarios and their solutions

First, caching process flow

      Reception request to take back existing cache data, to take a direct result of the return, whichever is less than when taken from the database, the database taken to update the cache, and returns the result, the database did not get to, that directly returns an empty result.

      

 

Second, the cache penetration

       description:

       Cache and data cache penetration means are not in the database, and users continue to initiate a request, such as to initiate data id is "-1" data id is particularly large or nonexistent. This time, the user is likely to be the attacker, the attacker can cause excessive pressure on the database.

      solution:

  1. Checking the interface layer increases, as the user authentication check, id foundation check, id <= 0 direct interception;
  2. The reach of the cache data, in the database does not get to this time may be written as key-value pairs key-null, the cache valid time point can be set short as 30 seconds (normally set too long leads to We are unable to use). This prevents attacks repeatedly use the same user id violent attack

 

Third, the cache breakdown

      description:

      Cache breakdown refers to the cache database but no existing data (typically cache time expires), then due to the particularly large number of concurrent users, while the read cache data is not read, but at the same time to get the data to the database, the database caused by pressure increases instantaneously, resulting in excessive pressure

      solution:

  1. Set hot data never expires.
  2. Plus mutex, mutex reference code as follows:

         

 

          Description:

          1) buffer the data, go directly after the above code, line 13 returns the result of

         2) there is no data in the cache, the first one into the thread acquires the lock and fetch data from the database, not before the lock is released into the other parallel threads will wait 100ms, then re-cache to fetch data. This prevents duplicate database to fetch data are repeated to update the data cache occurs.

          3) Of course, this is a simplified process, in theory, if the lock key according to value the better, thread A is taken from the database data does not interfere with data key1 taken key2 of thread B, the above code obviously can not do this.

 

Fourth, the cache avalanche

      description:

      缓存雪崩是指缓存中数据大批量到过期时间,而查询数据量巨大,引起数据库压力过大甚至down机。和缓存击穿不同的是,        缓存击穿指并发查同一条数据,缓存雪崩是不同数据都过期了,很多数据都查不到从而查数据库。

     解决方案

  1. 缓存数据的过期时间设置随机,防止同一时间大量数据过期现象发生。
  2. 如果缓存数据库是分布式部署,将热点数据均匀分布在不同搞得缓存数据库中。
  3. 设置热点数据永远不过期。
                        <li class="tool-item tool-active is-like "><a href="javascript:;"><svg class="icon" aria-hidden="true">
                            <use xlink:href="#csdnc-thumbsup"></use>
                        </svg><span class="name">点赞</span>
                        <span class="count">59</span>
                        </a></li>
                        <li class="tool-item tool-active is-collection "><a href="javascript:;" data-report-click="{&quot;mod&quot;:&quot;popu_824&quot;}"><svg class="icon" aria-hidden="true">
                            <use xlink:href="#icon-csdnc-Collection-G"></use>
                        </svg><span class="name">收藏</span></a></li>
                        <li class="tool-item tool-active is-share"><a href="javascript:;"><svg class="icon" aria-hidden="true">
                            <use xlink:href="#icon-csdnc-fenxiang"></use>
                        </svg>分享</a></li>
                        <!--打赏开始-->
                                                <!--打赏结束-->
                                                <li class="tool-item tool-more">
                            <a>
                            <svg t="1575545411852" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5717" xmlns:xlink="http://www.w3.org/1999/xlink" width="200" height="200"><defs><style type="text/css"></style></defs><path d="M179.176 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5718"></path><path d="M509.684 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5719"></path><path d="M846.175 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5720"></path></svg>
                            </a>
                            <ul class="more-box">
                                <li class="item"><a class="article-report">文章举报</a></li>
                            </ul>
                        </li>
                                            </ul>
                </div>
                            </div>
            <div class="person-messagebox">
                <div class="left-message"><a href="https://blog.csdn.net/kongtiao5">
                    <img src="https://profile.csdnimg.cn/C/6/F/3_kongtiao5" class="avatar_pic" username="kongtiao5">
                                            <img src="https://g.csdnimg.cn/static/user-reg-year/2x/12.png" class="user-years">
                                    </a></div>
                <div class="middle-message">
                                        <div class="title"><span class="tit"><a href="https://blog.csdn.net/kongtiao5" data-report-click="{&quot;mod&quot;:&quot;popu_379&quot;}" target="_blank">每天进步一点点yes</a></span>
                                            </div>
                    <div class="text"><span>发布了20 篇原创文章</span> · <span>获赞 65</span> · <span>访问量 7万+</span></div>
                </div>
                                <div class="right-message">
                                            <a href="https://im.csdn.net/im/main.html?userName=kongtiao5" target="_blank" class="btn btn-sm btn-red-hollow bt-button personal-letter">私信
                        </a>
                                                            <a class="btn btn-sm  bt-button personal-watch" data-report-click="{&quot;mod&quot;:&quot;popu_379&quot;}">关注</a>
                                    </div>
                            </div>
                    </div>
    
发布了15 篇原创文章 · 获赞 3 · 访问量 4931

一、缓存处理流程

Guess you like

Origin blog.csdn.net/kingsley520_a/article/details/104287675