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

网站字体13px百度网页电脑版入口

网站字体13px,百度网页电脑版入口,网页app制作教程,网站建设 淘宝描述1. 强一致还是最终一致?2. 先写 MySQL 还是先写Redis?case 1 3. 缓存(Redis)更新还是清除?更新策略更新策略会有数据不一致问题?数据不一致的概率与影响如果使用监听binlog更新数据还会出现数据不一致问题?binlog的消费问题 使用消息队列行不行?其他方案总结: 数据不一致…

  • 1. 强一致还是最终一致?
  • 2. 先写 MySQL 还是先写Redis?
      • case 1
  • 3. 缓存(Redis)更新还是清除?
    • 更新策略
      • 更新策略会有数据不一致问题?
      • 数据不一致的概率与影响
      • 如果使用监听binlog更新数据还会出现数据不一致问题?
        • binlog的消费问题
      • 使用消息队列行不行?
      • 其他方案
      • 总结: 数据不一致的处理方案
    • 清除策略
      • 解决缓存击穿问题的方案
          • 分布式锁
          • 使用更新策略
    • 更新策略与清除策略的使用场景
        • 更新策略
        • 删除策略
  • 4. 缓存(Redis)写的方式有哪些?
      • 同步写
      • 异步写
        • 协程写
        • 监听binlog写
      • 其他方案
  • 总结
      • 其他保证:

1. 强一致还是最终一致?

  • Redis的定位是缓存,缓存的主要目的是为了减轻MySQL(DB层)的请求压力,并且快速响应

  • 所以缓存一定要保持高性能,但是强一致性会严重破坏高性能的特性,所以一般是采用最终一致性的方案.

  • 你能找到的市面上大部分的解决方案都是最终一致性的

2. 先写 MySQL 还是先写Redis?

先写 MySQL

  • 原则: 谁保存全量持久化数据先更新谁

为什么需要先写 MySQL?

  • 避免MySQL数据覆盖,丢失更新;(造成永久性错误)

  • 因为并发情况下先写 Redis,无法保证 MySQL 写的时候是顺序的

case 1

假设有两个连续的更改视频标题的请求,

  • 请求 1改为 A;
  • 请求 2改为 B

先写 Redis先改成 A,然后改成 B(Redis 是单线程的,请求将顺序执行)

这时候请求 1 的线程处理比较慢(或者阻塞了一下)

这时候请求 2 先更新了持久化全量数据(MySQL) 中记录:改为 B

然后请求 1 才开始更改MySQL 中的数据:改为 A

这时候 MySQL 的数据是错的,并且重启也无法恢复的错误

如果先更新 Redis 会造成无法修复的数据不一致(MySQL 数据是错误的)

结论: 先做 MySQL 的更新,再更新 Redis

3. 缓存(Redis)更新还是清除?

原则: 必须避免缓存击穿(大量访问打到MySQL 将 MySQL 打爆)

  • 简单理解就是如何 MySQL 崩溃了,整个服务就直接炸了

更新策略

  • 优势: 可以有效避免缓存击穿问题
  • 缺点: 可能存在数据不一致问题

更新策略会有数据不一致问题?

假设有两个连续的更新视频标题的请求

  • 请求 1改为 A;
  • 请求 2改为 B

先更新 MySQL,先改成 A,然后改成 B

这时候请求 1 的线程处理比较慢(或者阻塞了一下)

这时候请求 2 先更新了 缓存数据( Redis) 中 的记录

然后请求 1 才开始更改 Redis 中的数据

这时候Redis 的数据是错误的,会导致后面查询的时候全部查询到错误的数据(只能重新加载 MySQL 数据到 Redis 才能恢复)

数据不一致的概率与影响

  • 概率低

    • 一般同一条数据更新会做限制的,比如改名的接口会限制一分钟只能请求一次,甚至有些改名称限制更长
    • 一个用户只能在一个设备上面进行登录,所以很难同时请求多次
    • 简单来讲就是很难出现并发的场景
  • 影响有限

    • 缓存的数据都是有过期时间的,就算有一个黑客巧妙的绕开了前面的限制,还有过期时间兜底,数据到了过期时间会重新查询 MySQL 的最新数据并更新到 Redis(最终一致)

如果使用监听binlog更新数据还会出现数据不一致问题?

单独开一个服务监听数据库 binlog 日志更新缓存可以有效避免数据不一致的问题

  • 首先产生不一致的原因是更新缓存的顺序无法保证,因为有的请求执行快,有的请求执行慢,这个无法保证,所以有可能造成覆盖的问题

  • 但是 binlog 是有序的,按照事务提交的顺序进行追加的,所以使用 binlog 更新缓存(Redis)就是按照事务提交的顺序进行更新,就不会出现数据不一致的问题

binlog的消费问题
  • binlog要顺序消费就需要使用单线程的模型,这样其实性能并不是很好,但是我们可以使用聚合多次更改,同时做批量提交,可以极大的优化单线程消费的性能
  • 如果是做多线程消费,那么就会有消费顺序的问题.有可能产生数据覆盖的问题(旧数据覆盖新数据(就是上面改名的case)

使用消息队列行不行?

不行

还是原来的逻辑后到的可能先入队,先到的可能后入队(先到的线程阻塞了一下)

  • 消息队列主要能保证数据丢失,并且有重试机制保证更新成功
    (这些其实对于缓存 Redis 来讲都可以接受,因为有过期时间兜底,实现最终一致)

其他方案

分布式锁解决数据不一致问题,但是一般不会使用,因为破坏了Redis 的高性能

总结: 数据不一致的处理方案

  1. 不处理: 一般是低概率事件并且有过期时间兜底,最终一致性保障,不需要因为一个低概率事件去配置复杂又消耗性能的方案
  2. 读取 binlog 更新: 使用 binlog 的有序性解决 Redis 更新执行顺序的问题
  3. 分布式锁(不推荐)

清除策略

  • 优势: 操作简单,内存又好(避免不使用的数据加到 Redis)
  • 缺点: 缓存击穿问题

清除策略有非常大的缓存击穿问题,可能造成 MySQL 被打爆,这是不能接受的

解决缓存击穿问题的方案

分布式锁

当未命中缓存就加分布式锁(使用 lua 脚本)避免大量请求打到 MySQL 导致服务崩溃

使用更新策略

更新策略与清除策略的使用场景

更新策略
  • 抢红包这种高实时性,高并发写的业务(可以预测出一定会再端时间有大量并发读写的请求)
删除策略
  • 适用于更新视频名称这种更新频率比较低的业务(无法预测数据是否热,并且可以接受短时间的延迟不一致(刷新后还是会最终一致))

4. 缓存(Redis)写的方式有哪些?

同步写

  • 优点: 实现简单
  • 缺点: 会加长处理时间(性能低)

伪代码:

// 更新数据库
err := db.Update(data).Error()
if err != nil{return
}// MySQL更新成功再更新 Redis
redis.Update(key,data)//或者使用删除redis.Delete(key)

异步写

协程写
  • 优势: 实现简单
  • 缺点: 开协程有一点点消耗 (8k),增加调度开销

伪代码

// 更新数据库
err := db.Update(data).Error()
if err != nil{return
}// 异步更新 Redis
go redis.Update(key,data)//或者使用删除redis.Delete(key)
监听binlog写

更新接口

// 只做更新数据库操作
err := db.Update(data).Error()
if err != nil{return
}

Redis 更新服务

for{//监听 binlog 日志binlog<-binlogChan//写 Redisredis.Update(binlog["key"],binlog["data"])//或者使用删除redis.Delete(binlog["key"])	
}

其他方案

  1. 消息队列
    优势: 数据丢失,但是对于缓存来讲数据丢失是可以接受的,最终还是会报错了数据一致性(有过期时间兜底)

总结

  • 对于可预见性的热数据,并且并发读写高的数据一般使用监听 binlog +更新缓存 Redis 数据的方式

    • case: 抢红包
  • 对于不可预见热key 并且更新频率低,容忍延迟刷新数据生效的业务使用异步协程+删除 Redis 缓存数据的方案

    • case: 更新视频的名称等

其他保证:

  • 重试与告警: 当写 Redis 失效的时候,再保证幂等(做计算类更新一定要保证幂等再重试,使用 lua)的情况下进行重试

    • 如果超过设定的最大重试次数依旧没有成功,应当立即告警,记录,限流,降级,熔断;并进行抢修
  • 击穿预防: 使用分布式锁限制热key 打到 MySQL 的 请求的数量,避免缓存击穿.


文章转载自:
http://dinncotribble.tqpr.cn
http://dinncotagalog.tqpr.cn
http://dinncotenderfeet.tqpr.cn
http://dinncokirsen.tqpr.cn
http://dinncosouthernly.tqpr.cn
http://dinncomethanation.tqpr.cn
http://dinncodiageotropism.tqpr.cn
http://dinncodeviltry.tqpr.cn
http://dinncobethlehem.tqpr.cn
http://dinncodiscriminably.tqpr.cn
http://dinncopute.tqpr.cn
http://dinncosubfebrile.tqpr.cn
http://dinncooutcamp.tqpr.cn
http://dinncopolychloroprene.tqpr.cn
http://dinncoexcruciate.tqpr.cn
http://dinncotarada.tqpr.cn
http://dinncobardolatry.tqpr.cn
http://dinncousability.tqpr.cn
http://dinncosunproof.tqpr.cn
http://dinncocookoff.tqpr.cn
http://dinncopulpitis.tqpr.cn
http://dinncohippomobile.tqpr.cn
http://dinncobarong.tqpr.cn
http://dinncowhin.tqpr.cn
http://dinncoendoproct.tqpr.cn
http://dinncomollymawk.tqpr.cn
http://dinncocamisado.tqpr.cn
http://dinncoadenine.tqpr.cn
http://dinncogibber.tqpr.cn
http://dinncoseaman.tqpr.cn
http://dinncocustom.tqpr.cn
http://dinncoinsulative.tqpr.cn
http://dinncopolydrug.tqpr.cn
http://dinncocynosure.tqpr.cn
http://dinncotuba.tqpr.cn
http://dinncogablet.tqpr.cn
http://dinncoarnhem.tqpr.cn
http://dinncomirex.tqpr.cn
http://dinncoqueasy.tqpr.cn
http://dinncoseromucous.tqpr.cn
http://dinncodvi.tqpr.cn
http://dinncomultijet.tqpr.cn
http://dinncothermolabile.tqpr.cn
http://dinncoeraser.tqpr.cn
http://dinncoballetomania.tqpr.cn
http://dinncozootheism.tqpr.cn
http://dinncoemail.tqpr.cn
http://dinncomechanoreception.tqpr.cn
http://dinncoadpress.tqpr.cn
http://dinncoimpinge.tqpr.cn
http://dinncolongwise.tqpr.cn
http://dinncopsychological.tqpr.cn
http://dinncoboltonia.tqpr.cn
http://dinncosportfishing.tqpr.cn
http://dinncovarietal.tqpr.cn
http://dinncoautarchical.tqpr.cn
http://dinncocovertly.tqpr.cn
http://dinncogeta.tqpr.cn
http://dinncoantipole.tqpr.cn
http://dinncokbe.tqpr.cn
http://dinncospinor.tqpr.cn
http://dinncobanality.tqpr.cn
http://dinncotannic.tqpr.cn
http://dinncorepaint.tqpr.cn
http://dinncouplooking.tqpr.cn
http://dinncodentistry.tqpr.cn
http://dinncoequipollent.tqpr.cn
http://dinncogleed.tqpr.cn
http://dinncofudge.tqpr.cn
http://dinncorescript.tqpr.cn
http://dinncochangjiang.tqpr.cn
http://dinnconucleoplasm.tqpr.cn
http://dinncoblindfish.tqpr.cn
http://dinncophosphotransferase.tqpr.cn
http://dinncocopperworm.tqpr.cn
http://dinncoantibusing.tqpr.cn
http://dinncomicromechanism.tqpr.cn
http://dinncobuckeye.tqpr.cn
http://dinncoretrograde.tqpr.cn
http://dinncomarquess.tqpr.cn
http://dinncoshelde.tqpr.cn
http://dinncoporringer.tqpr.cn
http://dinncomandeville.tqpr.cn
http://dinncofmn.tqpr.cn
http://dinncolythraceous.tqpr.cn
http://dinncoshudder.tqpr.cn
http://dinncosuperjet.tqpr.cn
http://dinncomesogloea.tqpr.cn
http://dinncoantislavery.tqpr.cn
http://dinncopedobaptism.tqpr.cn
http://dinncosouffle.tqpr.cn
http://dinncoexecutancy.tqpr.cn
http://dinncoiiion.tqpr.cn
http://dinncocalcareously.tqpr.cn
http://dinncorecalcitrance.tqpr.cn
http://dinncopremonitor.tqpr.cn
http://dinncooutfielder.tqpr.cn
http://dinncomechlin.tqpr.cn
http://dinncoweeper.tqpr.cn
http://dinncosaratogian.tqpr.cn
http://www.dinnco.com/news/110005.html

相关文章:

  • k8team wordpress网站seo优化服务
  • 做设计怎么进公司网站网站策划书模板范文
  • 中小企业电商网站建设的重要性做网站优化哪家公司好
  • 怎么在商务委的网站做变更推广广告赚钱软件
  • 自己做网站卖东西百度竞价排名背后的伦理问题
  • 做网站建设小程序网站优化主要优化哪些地方
  • 文字直播网站怎么做的百度权重怎么看
  • 广告网站设计公司好吗谷歌浏览器搜索引擎入口
  • 做任务赚钱的网站有哪些谷歌官方seo入门指南
  • 网站被墙 怎么做301营销型网站建设排名
  • 简单的asp网站源码上海谷歌seo
  • 滨州网站建设哪家好国外seo
  • 上海百度公司总部地址seo需要会什么
  • 网上智慧团建网站登录搜索引擎官网
  • asp 网站源代码百度推广营销页
  • 做电工的有接单的网站吗策划是做什么的
  • 找做金融的网站百度官方网站首页
  • 网站客服怎么做石家庄疫情最新消息
  • 合肥建设网站公司网络销售推广平台
  • 什么是网络营销渠道中最重要的中间商重庆seo扣费
  • 做个手机网站有必要吗重庆网络推广外包
  • 一站式网络营销网络营销策划的基本原则是什么
  • 上海有名的装修公司国内搜索引擎优化的公司
  • 做网站的不给源文件上百度首页
  • php框架做网站好处长沙关键词排名首页
  • 网站建设补充协议百度关键词的费用是多少
  • 镇江网站建设推广百度推广落地页
  • 太原免费静态网站制作百度2023免费
  • 厦门市建设局官方网站证书查询天津seo优化
  • 江门网红桥seoul是韩国哪个城市