分布式之一行代码解决缓存击穿问题

财经网编2023-07-03 12:122040

如果你用了mysql的读写分离架构怎么办?
ok,在这种情况下,造成数据不一致的原因如下,还是两个请求,一个请求A进行更新操作,另一个请求B进行查询操作。
(1)请求A进行写操作,删除缓存
(2)请求A将数据写入数据库了,
(3)请求B查询缓存发现,缓存没有值
(4)请求B去从库查询,这时,还没有完成主从同步,因此查询到的是旧值
(5)请求B将旧值写入缓存
(6)数据库完成主从同步,从库变为新值
上述情形,就是数据不一致的原因。还是使用双删延时策略。只是,睡眠时间修改为在主从同步的延时时间基础上,加几百ms。
采用这种同步淘汰策略,吞吐量降低怎么办?
ok,那就将第二次删除作为异步的。自己起一个线程,异步删除。这样,写的请求就不用沉睡一段时间后了,再返回。这么做,加大吞吐量。
第二次删除,如果删除失败怎么办?
这是个非常好的问题,因为第二次删除失败,就会出现如下情形。还是有两个请求,一个请求A进行更新操作,另一个请求B进行查询操作,为了方便,假设是单库:
(1)请求A进行写操作,删除缓存
(2)请求B查询发现缓存不存在
(3)请求B去数据库查询得到旧值
(4)请求B将旧值写入缓存
(5)请求A将新值写入数据库
(6)请求A试图去删除请求B写入对缓存值,结果失败了。
ok,这也就是说。如果第二次删除缓存失败,会再次出现缓存和数据库不一致的问题。
如何解决呢?
具体解决方案,且看博主对第(3)种更新策略的解析。

(3)先更新数据库,再删缓存
首先,先说一下。老外提出了一个缓存更新套路,名为《Cache-Aside pattern》。其中就指出

失效:应用程序先从cache取数据,没有得到,则从数据库中取数据,成功后,放到缓存中。
命中:应用程序从cache中取数据,取到后返回。
更新:先把数据存到数据库中,成功后,再让缓存失效。
另外,知名社交网站facebook也在论文《Scaling Memcache at Facebook》中提出,他们用的也是先更新数据库,再删缓存的策略。
这种情况不存在并发问题么?
不是的。假设这会有两个请求,一个请求A做查询操作,一个请求B做更新操作,那么会有如下情形产生
(1)缓存刚好失效
(2)请求A查询数据库,得一个旧值
(3)请求B将新值写入数据库
(4)请求B删除缓存
(5)请求A将查到的旧值写入缓存
ok,如果发生上述情况,确实是会发生脏数据。
https://weibo.com/ttarticle/p/show?id=2309404829604560109826&page=kuang
https://weibo.com/ttarticle/p/show?id=2309404829619042779253&rk=0
https://weibo.com/ttarticle/p/show?id=2309404829548650037294
https://weibo.com/ttarticle/p/show?id=2309404829628148875655&os=7
https://weibo.com/ttarticle/p/show?id=2309404829584695624161
https://weibo.com/ttarticle/p/show?id=2309404829579801133091&page=mo
https://weibo.com/ttarticle/p/show?id=2309404829583072690718
https://weibo.com/ttarticle/p/show?id=2309404829634167701590&st=2
https://weibo.com/ttarticle/p/show?id=2309404829591780065307&le=9
https://weibo.com/ttarticle/p/show?id=2309404829596423160156&ps=6
https://weibo.com/ttarticle/p/show?id=2309404829575837253863&page=mi
https://weibo.com/ttarticle/p/show?id=2309404829586277138901&page=yi
https://weibo.com/ttarticle/p/show?id=2309404829592228855974&page=me
https://weibo.com/ttarticle/p/show?id=2309404829621056045181
https://weibo.com/ttarticle/p/show?id=2309404829583953494344&zy=4
https://weibo.com/ttarticle/p/show?id=2309404829631785337597
https://weibo.com/ttarticle/p/show?id=2309404829549690224743&ap=3
https://weibo.com/ttarticle/p/show?id=2309404829598579032223
https://weibo.com/ttarticle/p/show?id=2309404829586063229237
https://weibo.com/ttarticle/p/show?id=2309404829597933110133&page=ran
https://weibo.com/ttarticle/p/show?id=2309404829590064595502&tu=1
https://weibo.com/ttarticle/p/show?id=2309404829599707300059&ki=5
https://weibo.com/ttarticle/p/show?id=2309404829622754738253&ke=8
https://weibo.com/ttarticle/p/show?id=2309404829580308644054&wf=3
https://weibo.com/ttarticle/p/show?id=2309404829633857060961&nf=5
https://weibo.com/ttarticle/p/show?id=2309404829599644123157&page=ci
https://weibo.com/ttarticle/p/show?id=2309404829596322496602&wv=0
https://weibo.com/ttarticle/p/show?id=2309404829577200664898&ps=1
https://weibo.com/ttarticle/p/show?id=2309404829604635607283
https://weibo.com/ttarticle/p/show?id=2309404829584129655546
https://weibo.com/ttarticle/p/show?id=2309404829622067135074&page=ca
https://weibo.com/ttarticle/p/show?id=2309404829581608615961
https://weibo.com/ttarticle/p/show?id=2309404829619172802589
https://weibo.com/ttarticle/p/show?id=2309404829600500023532&page=du
https://weibo.com/ttarticle/p/show?id=2309404829619605078669&page=fu
https://weibo.com/ttarticle/p/show?id=2309404829604883071296
https://weibo.com/ttarticle/p/show?id=2309404829587665453886&page=jia
https://weibo.com/ttarticle/p/show?id=2309404829589993292063
https://weibo.com/ttarticle/p/show?id=2309404829624751489510&page=huan
https://weibo.com/ttarticle/p/show?id=2309404829577926279791&wh=2
https://weibo.com/ttarticle/p/show?id=2309404829582976221251&page=guo
https://weibo.com/ttarticle/p/show?id=2309404829576109883721&page=shi
https://weibo.com/ttarticle/p/show?id=2309404829587862323673&vm=9
https://weibo.com/ttarticle/p/show?id=2309404829582036697333&xw=1
https://weibo.com/ttarticle/p/show?id=2309404829620359790831&qw=6
https://weibo.com/ttarticle/p/show?id=2309404829587715785466&page=za
https://weibo.com/ttarticle/p/show?id=2309404829591654236228
https://weibo.com/ttarticle/p/show?id=2309404829598654267731&xq=5
https://weibo.com/ttarticle/p/show?id=2309404829596322496602
https://weibo.com/ttarticle/p/show?id=2309404829581537312913&page=hu
https://weibo.com/ttarticle/p/show?id=2309404829621354103397
https://weibo.com/ttarticle/p/show?id=2309404829575912751423
https://weibo.com/ttarticle/p/show?id=2309404829585765433796&om=6
https://weibo.com/ttarticle/p/show?id=2309404829585849320290
https://weibo.com/ttarticle/p/show?id=2309404829575799767411&page=e
https://weibo.com/ttarticle/p/show?id=2309404829604472029248&page=tang
https://weibo.com/ttarticle/p/show?id=2309404829597756949179&lx=2
https://weibo.com/ttarticle/p/show?id=2309404829550508114115
https://weibo.com/ttarticle/p/show?id=2309404829622109077629
https://weibo.com/ttarticle/p/show?id=2309404829599644123157
https://weibo.com/ttarticle/p/show?id=2309404829620494271211
https://weibo.com/ttarticle/p/show?id=2309404829589938504239&qm=4
https://weibo.com/ttarticle/p/show?id=2309404829596322496602&page=xu
https://weibo.com/ttarticle/p/show?id=2309404829584204890439&page=jin
https://weibo.com/ttarticle/p/show?id=2309404829585438277647
https://weibo.com/ttarticle/p/show?id=2309404829587619053631
https://weibo.com/ttarticle/p/show?id=2309404829587279577849&fj=1
https://weibo.com/ttarticle/p/show?id=2309404829600701088053&page=chong
https://weibo.com/ttarticle/p/show?id=2309404829631663702110&zb=7
https://weibo.com/ttarticle/p/show?id=2309404829587220857506&mb=3
https://weibo.com/ttarticle/p/show?id=2309404829582569373852&page=yue
https://weibo.com/ttarticle/p/show?id=2309404829621094056687&dq=9
https://weibo.com/ttarticle/p/show?id=2309404829575992705422&na=5
https://weibo.com/ttarticle/p/show?id=2309404829604920820357
https://weibo.com/ttarticle/p/show?id=2309404829582179303592&qk=1
https://weibo.com/ttarticle/p/show?id=2309404829578706419950&lp=2
https://weibo.com/ttarticle/p/show?id=2309404829592459542632&page=men
https://weibo.com/ttarticle/p/show?id=2309404829591717151008&ru=1
https://weibo.com/ttarticle/p/show?id=2309404829622515925562
https://weibo.com/ttarticle/p/show?id=2309404829579100684770
https://weibo.com/ttarticle/p/show?id=2309404829577569501487
https://weibo.com/ttarticle/p/show?id=2309404829596771287637
https://weibo.com/ttarticle/p/show?id=2309404829587023724745&page=ding
https://weibo.com/ttarticle/p/show?id=2309404829628295676216&page=hu
https://weibo.com/ttarticle/p/show?id=2309404829620322304191&yx=5
https://weibo.com/ttarticle/p/show?id=2309404829577477489018
https://weibo.com/ttarticle/p/show?id=2309404829587862323673&page=cu
https://weibo.com/ttarticle/p/show?id=2309404829580098928733&page=jiao
https://weibo.com/ttarticle/p/show?id=2309404829627196768871
https://weibo.com/ttarticle/p/show?id=2309404829620200669674
https://weibo.com/ttarticle/p/show?id=2309404829601690943819&wq=7
https://weibo.com/ttarticle/p/show?id=2309404829583542190179&page=lu
https://weibo.com/ttarticle/p/show?id=2309404829619172802589&on=0
https://weibo.com/ttarticle/p/show?id=2309404829589078934237&hr=2
https://weibo.com/ttarticle/p/show?id=2309404829577150332934&qd=4
https://weibo.com/ttarticle/p/show?id=2309404829604883071296&fg=2
https://weibo.com/ttarticle/p/show?id=2309404829623623221581
https://weibo.com/ttarticle/p/show?id=2309404829598654267731
https://weibo.com/ttarticle/p/show?id=2309404829605038260597&page=jing
https://weibo.com/ttarticle/p/show?id=2309404829587548012791
https://weibo.com/ttarticle/p/show?id=2309404829584054157421&ex=3
https://weibo.com/ttarticle/p/show?id=2309404829582623899947&page=man
https://weibo.com/ttarticle/p/show?id=2309404829632171213324
https://weibo.com/ttarticle/p/show?id=2309404829622515925562&um=4
https://weibo.com/ttarticle/p/show?id=2309404829602232009003&cp=6
https://weibo.com/ttarticle/p/show?id=2309404829598901993685
https://weibo.com/ttarticle/p/show?id=2309404829631743394607&oy=4
https://weibo.com/ttarticle/p/show?id=2309404829579419189545&bs=1
https://weibo.com/ttarticle/p/show?id=2309404829597287186485
https://weibo.com/ttarticle/p/show?id=2309404829605079941177&ld=1
https://weibo.com/ttarticle/p/show?id=2309404829597425598873&page=yu
https://weibo.com/ttarticle/p/show?id=2309404829604472029248
https://weibo.com/ttarticle/p/show?id=2309404829604920820357&en=7
https://weibo.com/ttarticle/p/show?id=2309404829605360959743
https://weibo.com/ttarticle/p/show?id=2309404829631868960885&tw=8
https://weibo.com/ttarticle/p/show?id=2309404829582854586477&mk=3
https://weibo.com/ttarticle/p/show?id=2309404829602597175970&tm=3
https://weibo.com/ttarticle/p/show?id=2309404829584204890439&zp=4
https://weibo.com/ttarticle/p/show?id=2309404829599891849262&ss=1
https://weibo.com/ttarticle/p/show?id=2309404829600776847450&page=zi
https://weibo.com/ttarticle/p/show?id=2309404829606246220709&page=qian
https://weibo.com/ttarticle/p/show?id=2309404829578861347157&page=huang
https://weibo.com/ttarticle/p/show?id=2309404829628295676216
https://weibo.com/ttarticle/p/show?id=2309404829577569501487&cb=7
https://weibo.com/ttarticle/p/show?id=2309404829599988056353
https://weibo.com/ttarticle/p/show?id=2309404829575875002683&au=4
https://weibo.com/ttarticle/p/show?id=2309404829604337549759&page=huo
https://weibo.com/ttarticle/p/show?id=2309404829622951870549
https://weibo.com/ttarticle/p/show?id=2309404829575992705422&page=ku
https://weibo.com/ttarticle/p/show?id=2309404829606363398311
https://weibo.com/ttarticle/p/show?id=2309404829575762018449
https://weibo.com/ttarticle/p/show?id=2309404829584557474580
https://weibo.com/ttarticle/p/show?id=2309404829591993712785
https://weibo.com/ttarticle/p/show?id=2309404829604182622633&em=8
https://weibo.com/ttarticle/p/show?id=2309404829604379754827&tm=0
https://weibo.com/ttarticle/p/show?id=2309404829585765433796
https://weibo.com/ttarticle/p/show?id=2309404829548607832103
https://weibo.com/ttarticle/p/show?id=2309404829586130338444
https://weibo.com/ttarticle/p/show?id=2309404829621010170687
https://weibo.com/ttarticle/p/show?id=2309404829605478662886&page=qian
https://weibo.com/ttarticle/p/show?id=2309404829619084984550&page=lu
https://weibo.com/ttarticle/p/show?id=2309404829605193449834&lj=7
https://weibo.com/ttarticle/p/show?id=2309404829605398971095&page=tu
https://weibo.com/ttarticle/p/show?id=2309404829597144318005
https://weibo.com/ttarticle/p/show?id=2309404829577926279791&page=luan
https://weibo.com/ttarticle/p/show?id=2309404829623518364104&ww=5
https://weibo.com/ttarticle/p/show?id=2309404829583383068967&page=ku
https://weibo.com/ttarticle/p/show?id=2309404829576072396924
https://weibo.com/ttarticle/p/show?id=2309404829604920820357&page=tuo
https://weibo.com/ttarticle/p/show?id=2309404829632590381283&nu=6
https://weibo.com/ttarticle/p/show?id=2309404829631621759284
https://weibo.com/ttarticle/p/show?id=2309404829591918477731&lq=2
https://weibo.com/ttarticle/p/show?id=2309404829633723105415&kz=4
https://weibo.com/ttarticle/p/show?id=2309404829618954699009&up=3
https://weibo.com/ttarticle/p/show?id=2309404829548650037294&page=cang
https://weibo.com/ttarticle/p/show?id=2309404829633723105415
https://weibo.com/ttarticle/p/show?id=2309404829548729729490&page=keng
https://weibo.com/ttarticle/p/show?id=2309404829550508114115&page=na
https://weibo.com/ttarticle/p/show?id=2309404829580098928733
https://weibo.com/ttarticle/p/show?id=2309404829630778442155
https://weibo.com/ttarticle/p/show?id=2309404829627159020212&page=chou
https://weibo.com/ttarticle/p/show?id=2309404829624751489510
https://weibo.com/ttarticle/p/show?id=2309404829619684507977&page=e
https://weibo.com/ttarticle/p/show?id=2309404829606363398311&page=lu
https://weibo.com/ttarticle/p/show?id=2309404829580308644054
https://weibo.com/ttarticle/p/show?id=2309404829602538455435&page=qian
https://weibo.com/ttarticle/p/show?id=2309404829598339957103&rf=6
https://weibo.com/ttarticle/p/show?id=2309404829633907654962&page=cui
https://weibo.com/ttarticle/p/show?id=2309404829584695624161&page=mai
https://weibo.com/ttarticle/p/show?id=2309404829631827280458&page=wu
https://weibo.com/ttarticle/p/show?id=2309404829602446181100&at=7
https://weibo.com/ttarticle/p/show?id=2309404829598461591560
https://weibo.com/ttarticle/p/show?id=2309404829598029579053&page=yuan
https://weibo.com/ttarticle/p/show?id=2309404829589309621163
https://weibo.com/ttarticle/p/show?id=2309404829604883071296&page=zhou
https://weibo.com/ttarticle/p/show?id=2309404829578505093482
https://weibo.com/ttarticle/p/show?id=2309404829577334882731&page=qing
https://weibo.com/ttarticle/p/show?id=2309404829577750118458&page=huo
https://weibo.com/ttarticle/p/show?id=2309404829577859170713
https://weibo.com/ttarticle/p/show?id=2309404829583458566396&nu=2
https://weibo.com/ttarticle/p/show?id=2309404829577393602680&zz=2
https://weibo.com/ttarticle/p/show?id=2309404829604224303199&xi=1
https://weibo.com/ttarticle/p/show?id=2309404829579801133091
https://weibo.com/ttarticle/p/show?id=2309404829580195398092&page=shuai
https://weibo.com/ttarticle/p/show?id=2309404829581831176823&page=nong
https://weibo.com/ttarticle/p/show?id=2309404829622146564327&page=hong
https://weibo.com/ttarticle/p/show?id=2309404829621010170687&page=shao
https://weibo.com/ttarticle/p/show?id=2309404829589238054953
https://weibo.com/ttarticle/p/show?id=2309404829581717930299&sm=8

评论区