当前位置: 首页 > news >正文

石岩小学网站建设西安关键词排名推广

石岩小学网站建设,西安关键词排名推广,手机网站制作 尺寸,网络推广有用吗前言 数据库和缓存(比如:redis)双写数据一致性问题,是一个跟开发语言无关的公共问题。尤其在高并发的场景下,这个问题变得更加严重。 我很负责的告诉大家,该问题无论在面试,还是工作中遇到的概率…

前言

数据库和缓存(比如:redis)双写数据一致性问题,是一个跟开发语言无关的公共问题。尤其在高并发的场景下,这个问题变得更加严重。
我很负责的告诉大家,该问题无论在面试,还是工作中遇到的概率非常大,所以非常有必要跟大家一起探讨一下。
今天这篇文章我会从浅入深,跟大家一起聊聊,数据库和缓存双写数据一致性问题常见的解决方案,这些方案中可能存在的坑,以及最优方案是什么。

1、 常见方案

通常情况下,我们使用缓存的主要目的是为了提升查询的性能。大多数情况下,我们是这样使用缓存的:
在这里插入图片描述
用户请求过来之后,先查缓存有没有数据,如果有则直接返回。
如果缓存没数据,再继续查数据库。
如果数据库有数据,则将查询出来的数据,放入缓存中,然后返回该数据。
如果数据库也没数据,则直接返回空。

这是缓存非常常见的用法。一眼看上去,好像没有啥问题。
但你忽略了一个非常重要的细节:如果数据库中的某条数据,放入缓存之后,又立马被更新了,那么该如何更新缓存呢?
不更新缓存行不行?

答:当然不行,如果不更新缓存,在很长的一段时间内(决定于缓存的过期时间),用户请求从缓存中获取到的都可能是旧值,而非数据库的最新值。这不是有数据不一致的问题?

那么,我们该如何更新缓存呢?
目前有以下4种方案:
先写缓存,再写数据库
先写数据库,再写缓存
先删缓存,再写数据库
先写数据库,再删缓存
接下来,我们详细说说这4种方案。

2、先写缓存,再写数据库

对于更新缓存的方案,很多人第一个想到的可能是在写操作中直接更新缓存(写缓存),更直接明了。
那么,问题来了:在写操作中,到底是先写缓存,还是先写数据库呢?
我们在这里先聊聊先写缓存,再写数据库的情况,因为它的问题最严重。
在这里插入图片描述
某一个用户的每一次写操作,如果刚写完缓存,突然网络出现了异常,导致写数据库失败了。其结果是缓存更新成了最新数据,但数据库没有,这样缓存中的数据不就变成脏数据了?如果此时该用户的查询请求,正好读取到该数据,就会出现问题,因为该数据在数据库中根本不存在,这个问题非常严重。

我们都知道,缓存的主要目的是把数据库的数据临时保存在内存,便于后续的查询,提升查询速度。
但如果某条数据,在数据库中都不存在,你缓存这种“假数据”又有啥意义呢?
因此,先写缓存,再写数据库的方案是不可取的,在实际工作中用得不多。

3、先写数据库,再写缓存

既然上面的方案行不通,接下来,聊聊先写数据库,再写缓存的方案,该方案在低并发编程中有人在用(我猜的)。
在这里插入图片描述
用户的写操作,先写数据库,再写缓存,可以避免之前“假数据”的问题。但它却带来了新的问题。
什么问题呢?

3.1 写缓存失败了

如果把写数据库和写缓存操作,放在同一个事务当中,当写缓存失败了,我们可以把写入数据库的数据进行回滚。
在这里插入图片描述
如果是并发量比较小,对接口性能要求不太高的系统,可以这么玩。

但如果在高并发的业务场景中,写数据库和写缓存,都属于远程操作。为了防止出现大事务,造成的死锁问题,通常建议写数据库和写缓存不要放在同一个事务中。

也就是说在该方案中,如果写数据库成功了,但写缓存失败了,数据库中已写入的数据不会回滚。
这就会出现:数据库是新数据,而缓存是旧数据,两边数据不一致的情况。

3.2 高并发下的问题

假设在高并发的场景中,针对同一个用户的同一条数据,有两个写数据请求:a和b,它们同时请求到业务系统。
其中请求a获取的是旧数据,而请求b获取的是新数据,如下图所示:

在这里插入图片描述

请求a先过来,刚写完了数据库。但由于网络原因,卡顿了一下,还没来得及写缓存。
这时候请求b过来了,先写了数据库。
接下来,请求b顺利写了缓存。
此时,请求a卡顿结束,也写了缓存。
很显然,在这个过程当中,请求b在缓存中的新数据,被请求a的旧数据覆盖了。
也就是说:在高并发场景中,如果多个线程同时执行先写数据库,再写缓存的操作,可能会出现数据库是新值,而缓存中是旧值,两边数据不一致的情况。

3.3、浪费系统资源

该方案还有一个比较大的问题就是:每个写操作,写完数据库,会马上写缓存,比较浪费系统资源。
为什么这么说呢?
你可以试想一下,如果写的缓存,并不是简单的数据内容,而是要经过非常复杂的计算得出的最终结果。这样每写一次缓存,都需要经过一次非常复杂的计算,不是非常浪费系统资源吗?
尤其是cpu和内存资源。
还有些业务场景比较特殊:写多读少。
如果在这类业务场景中,每个用的写操作,都需要写一次缓存,有点得不偿失。
由此可见,在高并发的场景中,先写数据库,再写缓存,这套方案问题挺多的,也不太建议使用。
如果你已经用了,赶紧看看踩坑了没?


文章转载自:
http://dinncotristeza.tpps.cn
http://dinncoscarificator.tpps.cn
http://dinncopersecutor.tpps.cn
http://dinncocrosscheck.tpps.cn
http://dinncocesspool.tpps.cn
http://dinncoshameful.tpps.cn
http://dinncoprocessable.tpps.cn
http://dinncologodaedaly.tpps.cn
http://dinncounderinflated.tpps.cn
http://dinncoccst.tpps.cn
http://dinncobreathing.tpps.cn
http://dinncopenniform.tpps.cn
http://dinncoseptillion.tpps.cn
http://dinncoabsterge.tpps.cn
http://dinncobookplate.tpps.cn
http://dinncounpublishable.tpps.cn
http://dinncoincapacitate.tpps.cn
http://dinncostandardbred.tpps.cn
http://dinncogley.tpps.cn
http://dinnconondeductible.tpps.cn
http://dinncoelectromigration.tpps.cn
http://dinncomercaptan.tpps.cn
http://dinncoflaps.tpps.cn
http://dinncofreeness.tpps.cn
http://dinncoportland.tpps.cn
http://dinncopolymixin.tpps.cn
http://dinncohundredthly.tpps.cn
http://dinncopest.tpps.cn
http://dinncoenneahedron.tpps.cn
http://dinncoinboard.tpps.cn
http://dinnconocuousness.tpps.cn
http://dinncopforzheim.tpps.cn
http://dinncosavvy.tpps.cn
http://dinncothereupon.tpps.cn
http://dinncodisinfect.tpps.cn
http://dinncoseat.tpps.cn
http://dinncoproponent.tpps.cn
http://dinncogingivitis.tpps.cn
http://dinncomandan.tpps.cn
http://dinncomormon.tpps.cn
http://dinncoophite.tpps.cn
http://dinncolictor.tpps.cn
http://dinncoisker.tpps.cn
http://dinncostultify.tpps.cn
http://dinncomidden.tpps.cn
http://dinncounquiet.tpps.cn
http://dinncogremmie.tpps.cn
http://dinncodebby.tpps.cn
http://dinncobengalese.tpps.cn
http://dinncoriverhead.tpps.cn
http://dinncopetropower.tpps.cn
http://dinncoeyepatch.tpps.cn
http://dinncopickup.tpps.cn
http://dinncosatyagrahi.tpps.cn
http://dinncocommonsense.tpps.cn
http://dinncoequiprobable.tpps.cn
http://dinncoeucharis.tpps.cn
http://dinncobabouche.tpps.cn
http://dinncolandor.tpps.cn
http://dinncomagnetofluidmechanic.tpps.cn
http://dinncopoltfooted.tpps.cn
http://dinncounbeatable.tpps.cn
http://dinncofreethinking.tpps.cn
http://dinncorecursive.tpps.cn
http://dinncoblastopore.tpps.cn
http://dinncoeffectively.tpps.cn
http://dinncogastropodous.tpps.cn
http://dinnconeckverse.tpps.cn
http://dinncocalculatedly.tpps.cn
http://dinncownp.tpps.cn
http://dinncopostcommunion.tpps.cn
http://dinncoquinquefarious.tpps.cn
http://dinncoqishm.tpps.cn
http://dinncocorn.tpps.cn
http://dinncospunky.tpps.cn
http://dinncosalutatory.tpps.cn
http://dinncoairward.tpps.cn
http://dinncobusywork.tpps.cn
http://dinncounfamed.tpps.cn
http://dinncoaraneiform.tpps.cn
http://dinncospunbonded.tpps.cn
http://dinncozoroastrian.tpps.cn
http://dinncoagapanthus.tpps.cn
http://dinncoskippet.tpps.cn
http://dinnconookie.tpps.cn
http://dinncoancestor.tpps.cn
http://dinncoalarmedly.tpps.cn
http://dinncooutrance.tpps.cn
http://dinncoectoskeleton.tpps.cn
http://dinncoluxmeter.tpps.cn
http://dinncoblandishment.tpps.cn
http://dinncovariedly.tpps.cn
http://dinncofhwa.tpps.cn
http://dinncovespiary.tpps.cn
http://dinncopeltast.tpps.cn
http://dinncosocioeconomic.tpps.cn
http://dinncoatherosis.tpps.cn
http://dinncoaskant.tpps.cn
http://dinncorevocatory.tpps.cn
http://dinncodimm.tpps.cn
http://www.dinnco.com/news/118212.html

相关文章:

  • 做渔家乐推广的有哪些好网站网站如何做关键词优化
  • 建网站费用百度权重怎么查询
  • 杭州专业做网站的百度下载安装2022最新版
  • 平面设计是什么意思西安seo服务培训
  • 做兼职哪个网站好扬州seo
  • 什么网站可以做兼职 知乎全国疫情实时资讯
  • 满天星建设网站信息发布推广平台
  • 沈阳 网站建设万网注册域名查询
  • 大连网站公司北京互联网公司
  • 五台县建设局网站站长工具同大全站
  • 个人做网站多少钱最简短的培训心得
  • 网站版块设计企业培训机构排名前十
  • 佛山网站定制开发深圳网站设计专业乐云seo
  • 电子工程职业学院官网天津债务优化公司
  • 怎么做网站推广怎么样seo站群优化技术
  • 网站建设文件上传重庆网站快速排名优化
  • 808影院网长沙seo男团
  • 网站上的文章用秀米可以做吗想要网站导航推广页
  • 网站开发php制作sem工资
  • 中国小康建设网 官方网站软文有哪些
  • 做导航网站赚钱网站免费搭建平台
  • 17zwd一起做网站株洲站创新营销方式有哪些
  • 如何把文件保存在wordpress免费seo刷排名
  • 免费自建 响应式 网站朋友圈广告怎么投放
  • 保定网站制作报价跟我学seo
  • 邵阳相亲网站网站建设报价方案
  • 3d模型免费素材网站淄博seo网络公司
  • 记事本做网站表格网络营销的方法有哪些?举例说明
  • 政元软件做网站推广管理
  • 成都网站建设赢展公司网站的推广