redis和mysql保证数据一致性

--

redis面试经典问题,如何保证redis和mysql的数据一致性

前提:在高并发的业务场景下,为了缓解关系型数据库的压力,我们通常将一部分数据缓存到redis总,让访问请求先查询redis,而并不是直接访问我们的关系型数据库,这样就可以极大的缓解数据库的压力。伴随着高并发业务场景,两个库必然带来了一个老问题,就是如何保证数据一致性。或者根据CAP理论,这样说更合理,如何尽可能的保证数据一致性。
一说来说redis和mysql保证数据一致性有这么几种做法

  1. 先更新缓存在更新数据库
  2. 先更新数据库在更新缓存
  3. 延时双删(同步&异步)
  4. 基于中间件对数据库日志进行串行化处理

先更新缓存在更新数据库

redis和mysql保证数据一致性插图

正常我们的理想的业务流程应该是

  1. 线程A修改缓存数据,再修改mysql数据
  2. 线程B修改缓存数据,再修改mysql数据
    然而在现实的分布式的高并发环境下,很可能会发生一些极端的情况
  3. 线程A执行修改缓存数据,成功后,线程A在执行后续业务的时候出现了卡顿,在此时
  4. 线程B执行了修噶缓存数据,成功后,并迅速修改了mysql的数据
  5. 线程B执行完毕后,线程A缓过来了。并成功修改mysql的数据,
  6. 此时造成的后果就是缓存存储的是线程B的数据,mysql存储的是线程A执行的数据

先更新数据库在更新缓存

redis和mysql保证数据一致性插图1
这里的逻辑和先更新缓存在更新数据库大致,不在赘述

延时双删(同步&异步)

普通的双删,这里不做介绍了。直接看延时双删了。
延时双删的逻辑大致如下,在程序执行数据库更新的前后都进行删除缓存操作

public void update(String key,Object data){
    redis.delKey(key);
    db.updateData(data);
    Thread.sleep(x秒);
    redis.delKey(key);
}

它主要想既觉得问题是 更新数据库后,未更新缓存时候,其他线程会从缓存拿到旧的数据
他主要想解决下面这种业务场景

  1. 线程A执行del缓存操作
  2. 线程B查询缓存,没有,只能查询数据库
  3. 线程B查询结果后,更新缓存
  4. 线程A执行update数据库操作
  5. 线程A休眠
  6. 线程A执行del缓存操作,下次重新查询的时候,再重新缓存一边

看上去是没啥问题。但是再想一下,如果在5,6两步之间再来一个线程C 查询缓存,直接可以查到线程B缓存的缓存了。而此时的mysql数据已经被线程A给修改了
有人说那既然这样,我不要第4不,直接第4步到底6不不就好了。这样又带来一个问题,就是如果我们的第3步,线程B缓存延迟了一点。拖到底6步后面,不就是线程B缓存了一个老的数据了吗。
这里的第5步延时,主要就是为了方式这个情况,当然只能说是尽量避免这个情况。因为这个值不能设的过大,也不能过小,

异步双删其实就是把,5,6两步放在一个单独的线程去执行,这样在update方法里面就不会有睡眠这个操作,毕竟我们对系统的响应时间是有要求的,不能随便一个方法就睡眠一段时间。
主要思路就是在第4步之后,发出一个删除缓存指令,可以放在mq或者那个缓存容器中,保证另一个执行删除的线程能够串行化执行删除操作。当然这样的写法也是有缺陷的,就是对业务代码造成了很大的侵入性。

如此看来延时双删也并不能很好的解决数据一致性问题。

基于canal的串行化处理

这个方法是基于中间件,项目更新数据库操作前后不做缓存的任何操作,而是通过中间件监听mysql的binlog,然后修改缓存
canal是阿里巴巴开源的一个基于mysql的binlog的消费订阅框架
主要工作原理是:

  • canal 模拟 MySQL slave 的交互协议,伪装自己为 MySQL slave ,向 MySQL master 发送dump 协议
  • MySQL master 收到 dump 请求,开始推送 binary log 给 slave (即 canal )
  • canal 解析 binary log 对象(原始为 byte 流)
    可以通过客户端去连接canal服务,执行操作,canal也可以投递消息到消息队列中去。具体可以看自己的业务需要
    关于canal的简单部署与测试我在另一篇文章里面有介绍
    canal的部署测试

当然如果高并发的情况下,产生大量的缓存数据库更新操作,用这个方法还是会有一定的延时。这也是分布式架构决定的,正如CAP无法一次性满足一样。
还是开篇的
撇开业务场景谈架构都是耍流氓,每个方式都有可取之处,也有试用场景,并不见得哪一种就是行业规范。

821 对 “redis和mysql保证数据一致性”的想法;

  1. En iyi leke kremi alarak cildinizdeki cilt tonu farklılıklarına son verebililrsiniz, Dünya çapında satışa sunulan hc care leke kremine sahip olmak
    sadece bir tık uzakta.. Yaz gelmeden cilt tonu farklılıklarına en iyi leke kremi markası ile son verebilirsiniz!

    Leke kremi ; LEke KREMi

  2. Hey there I am so happy I found your website, I really found you by mistake,
    while I was browsing on Askjeeve for something else, Anyhow I am here now and would just like
    to say thank you for a tremendous post and a all round thrilling blog (I also love the theme/design), I don’t
    have time to read it all at the moment but I have
    bookmarked it and also added your RSS feeds, so when I have
    time I will be back to read a lot more, Please do keep up
    the superb work.

  3. It’s a pity you don’t have a donate button!
    I’d definitely donate to this superb blog! I suppose for now i’ll settle
    for book-marking and adding your RSS feed to my Google account.
    I look forward to new updates and will share this website with my Facebook group.
    Talk soon!

  4. When I initially left a comment I seem to have clicked
    the -Notify me when new comments are added- checkbox
    and from now on whenever a comment is added I receive four emails with the exact same comment.
    Is there an easy method you are able to remove me from that service?
    Kudos!

  5. Hi! Quick question that’s entirely off topic. Do you know how to
    make your site mobile friendly? My blog looks weird when browsing from my iphone.
    I’m trying to find a template or plugin that might be able to fix this problem.
    If you have any recommendations, please share. Thanks!

  6. Hey! I could have sworn I’ve been to this site before but after checking through some of
    the post I realized it’s new to me. Anyhow, I’m definitely happy I found it and
    I’ll be bookmarking and checking back frequently!

  7. It’s perfect time to make a few plans for the future and it’s time to be happy.
    I’ve read this post and if I could I wish to recommend you few interesting issues
    or tips. Maybe you can write subsequent articles regarding this article.

    I want to learn even more things approximately
    it!

  8. Hi! I know this is kinda off topic but I was wondering which blog
    platform are you using for this website? I’m getting tired of WordPress
    because I’ve had problems with hackers and I’m looking at
    options for another platform. I would be awesome if you could point me in the
    direction of a good platform.

  9. Hey this is kind of of off topic but I was wanting to know if blogs use WYSIWYG editors or if you have to manually code with HTML.
    I’m starting a blog soon but have no coding skills so I wanted to get advice from someone with experience.
    Any help would be enormously appreciated!

  10. Just want to say your article is as surprising.
    The clarity for your publish is simply spectacular and i
    could think you are a professional on this subject.
    Fine with your permission allow me to seize your feed to keep
    updated with approaching post. Thanks one million and please continue the enjoyable work.

  11. Hi there, just became aware of your blog through Google, and found that it’s truly informative.

    I’m gonna watch out for brussels. I’ll be grateful if you continue this in future.
    Many people will be benefited from your writing. Cheers!

  12. I’m pretty pleased to uncover this website.
    I wanted to thank you for your time for this particularly wonderful read!!
    I definitely savored every part of it and i
    also have you book-marked to check out new things on your website.

  13. It’s a shame you don’t have a donate button! I’d most certainly donate to this superb
    blog! I guess for now i’ll settle for bookmarking and adding your RSS feed
    to my Google account. I look forward to new updates and will
    talk about this blog with my Facebook group.
    Talk soon!

  14. Do you have a spam problem on this website;
    I also am a blogger, and I was curious about your situation; many of us have developed some nice methods
    and we are looking to swap strategies with others, why
    not shoot me an email if interested.

  15. I’m impressed, I must say. Rarely do I come across a blog that’s both equally educative and engaging, and let me tell you,
    you’ve hit the nail on the head. The problem is an issue that too few folks are speaking intelligently about.
    I am very happy I stumbled across this in my search for something regarding this.

  16. Pingback: what is cialis
  17. Pingback: sildenafil 20 mg
  18. I have been browsing online more than three hours today, yet I never found any interesting article like yours.
    It is pretty worth enough for me. In my view, if all
    web owners and bloggers made good content as you did, the net will be
    much more useful than ever before.

  19. Very nice post. I just stumbled upon your weblog and wished to
    say that I’ve truly enjoyed surfing around your blog posts.
    After all I’ll be subscribing to your rss feed and I
    hope you write again soon!

  20. I’ve been browsing online more than 4 hours today,
    yet I never found any interesting article
    like yours. It’s pretty worth enough for me.

    Personally, if all website owners and bloggers made good content as you did, the net will be much
    more useful than ever before.

  21. Thanks a lot for sharing this with all of us you really realize what you’re talking approximately!

    Bookmarked. Kindly also visit my site =). We could have a
    hyperlink exchange agreement among us

  22. Its like you read my mind! You appear to know so much approximately this, such
    as you wrote the e-book in it or something. I think that you just can do with
    a few p.c. to drive the message house a bit, however other than that, that is excellent blog.
    An excellent read. I will certainly be back.

  23. I’ve been browsing on-line more than three hours as of late,
    but I never discovered any interesting article like yours.

    It is lovely worth enough for me. Personally, if all site owners and bloggers made good content material as
    you probably did, the web will be a lot more helpful than ever before.

  24. It is perfect time to make some plans for the
    future and it is time to be happy. I’ve learn this put up and if
    I may just I wish to recommend you some attention-grabbing
    things or advice. Maybe you could write subsequent articles regarding this article.
    I wish to read more things about it!

  25. It’s the best time to make some plans for the future and it’s time to be happy.
    I’ve learn this put up and if I could I want to recommend you
    some fascinating things or advice. Maybe you can write subsequent articles referring to this article.
    I wish to learn more things about it!

  26. I have figured out some considerations through your site post. One other point I would like to express is that there are lots of games available and which are designed mainly for toddler age little ones. They involve pattern acceptance, colors, creatures, and forms. These commonly focus on familiarization rather than memorization. This will keep children occupied without sensing like they are learning. Thanks

  27. I have been browsing online greater than 3 hours these days, but I never discovered any interesting article like yours. It is lovely price enough for me. In my opinion, if all site owners and bloggers made good content as you did, the internet will likely be much more useful than ever before.

  28. I have been surfing on-line greater than three
    hours lately, yet I by no means found any fascinating article like yours.
    It’s beautiful value enough for me. In my view, if all website owners and bloggers
    made just right content material as you probably did, the net
    will be a lot more useful than ever before.

  29. Howdy I am so glad I found your webpage, I really found you by accident, while I was
    looking on Bing for something else, Regardless I am here now and would just
    like to say cheers for a tremendous post and a all round thrilling
    blog (I also love the theme/design), I don’t have time to browse it all at the minute but I
    have book-marked it and also added in your RSS feeds, so when I have time I will be back
    to read more, Please do keep up the great b.

  30. Very nice post. I just stumbled upon your weblog and wished
    to say that I’ve really enjoyed browsing your blog posts.
    After all I will be subscribing to your rss feed
    and I hope you write again very soon!

  31. It is the best time to make some plans for the future and it’s time
    to be happy. I’ve read this post and if I could I desire to suggest you few interesting things or advice.

    Maybe you can write next articles referring to this article.
    I want to read even more things about it!

  32. It is appropriate time to make some plans for the longer term and it’s time to be happy.
    I have read this publish and if I could I want to
    counsel you some interesting things or advice. Perhaps you can write subsequent articles referring to
    this article. I wish to learn even more issues approximately it!

  33. Pingback: wellbutrin xl dosage
  34. Hey there would you mind stating which blog platform
    you’re working with? I’m planning to start my own blog soon but I’m having a tough time selecting between BlogEngine/Wordpress/B2evolution and Drupal.
    The reason I ask is because your layout seems different then most blogs
    and I’m looking for something unique. P.S My apologies for getting off-topic but I had to ask!

  35. Thanks for the ideas shared in your blog. Something also important I would like to talk about is that weight-loss is not all about going on a fad diet and trying to reduce as much weight as you’re able in a couple of days. The most effective way to lose weight is by consuming it gradually and obeying some basic suggestions which can allow you to make the most from a attempt to lose weight. You may know and be following some of these tips, but reinforcing expertise never damages.

  36. It’s perfect time to make some plans for the
    long run and it’s time to be happy. I have read this put up and if
    I may I wish to suggest you some fascinating
    things or suggestions. Maybe you can write next articles
    regarding this article. I want to read even more things
    approximately it!

  37. When I initially commented I clicked the “Notify me when new comments are added” checkbox and now each time a comment is added I get four emails with the same comment.
    Is there any way you can remove me from that service? Bless you!

  38. Pingback: what is metronidazole
  39. Hey I know this is off topic but I was wondering
    if you knew of any widgets I could add to
    my blog that automatically tweet my newest twitter updates.
    I’ve been looking for a plug-in like this for quite some time and was hoping maybe you would have some
    experience with something like this. Please let me know if
    you run into anything. I truly enjoy reading your blog and I look forward to your new
    updates.

  40. Hey just wanted to give you a quick heads up. The text in your content
    seem to be running off the screen in Internet explorer.
    I’m not sure if this is a formatting issue or something to do with web browser compatibility but I thought
    I’d post to let you know. The design and style look great though!

    Hope you get the issue fixed soon. Kudos

  41. Howdy exceptional website! Does running a blog such as this require a massive amount work?
    I’ve virtually no knowledge of programming but I had
    been hoping to start my own blog in the near future. Anyhow, if
    you have any ideas or techniques for new blog owners
    please share. I understand this is off subject however I just wanted
    to ask. Many thanks!

  42. Pingback: goodrx cialis
  43. Pingback: viagra generic
  44. Pingback: levitra overdose
  45. Someone necessarily help to make seriously posts I would state.
    This is the first time I frequented your website page and to this
    point? I surprised with the analysis you made to create this actual publish incredible.
    Fantastic job!

  46. I have been browsing online greater than three hours lately, yet I never discovered any interesting article like yours.

    It’s beautiful price sufficient for me. Personally, if
    all webmasters and bloggers made good content as you did, the internet will likely be a lot
    more helpful than ever before.

  47. Its such as you read my thoughts! You appear to know so
    much approximately this, like you wrote the e book in it or something.
    I believe that you can do with some % to force the message house a bit, however other than that, that is wonderful blog.
    A great read. I’ll definitely be back.

  48. You are so cool! I don’t believe I’ve truly read a single thing like that before.
    So wonderful to discover somebody with some original thoughts on this issue.
    Really.. thanks for starting this up. This website is
    something that’s needed on the internet, someone with
    a little originality!

  49. Woah! I’m really digging the template/theme of this website.

    It’s simple, yet effective. A lot of times it’s very hard to get that “perfect balance” between usability and
    visual appearance. I must say you’ve done
    a superb job with this. Additionally, the blog loads extremely fast for me on Opera.
    Superb Blog!

  50. Pingback: acyclovir 400 mg cost
  51. You actually make it seem really easy along with your presentation however I in finding this matter to be actually something which I
    feel I might by no means understand. It seems too complicated and
    extremely large for me. I am looking ahead to your subsequent
    put up, I will attempt to get the hold of it!

  52. Pingback: what is cefdinir
  53. Howdy! I could have sworn I’ve been to this website before but after reading
    through some of the post I realized it’s new to
    me. Anyways, I’m definitely delighted I found it and
    I’ll be bookmarking and checking back frequently!

  54. I’ve beren surfiong on-line greater thaqn three hourts today, yeet I
    never discovered anny interfesting article like yours.
    It’s pretty valu sufficient ffor me. In myy
    view, iff alll weeb ownners andd blogbgers madee goodd conttent material ass yyou probably did, tthe wweb will preobably bbe mhch mire elpful than ever before.

  55. Hello there! This is my 1st comment here so I just wanted to give
    a quick shout out and tell you I genuinely enjoy reading through your blog posts.
    Can you recommend any other blogs/websites/forums that cover
    the same subjects? Appreciate it!

  56. Pingback: cialis peak effect
  57. Pingback: cialis 5mg dosage
  58. Pingback: cialis 50mg
  59. It’s the est tim too makee soe pkans for thhe future
    aand it iss time tto bbe happy. I’ve reazd tbis post aand iff I ould I wiseh
    too sugggest youu somme interesring things orr advice.
    Mayybe youu caan wrige nexst afticles referring to this article.
    I wiosh to rread molre things about it!

  60. Its like you read my thoughts! You appear
    to understand so much approximately this, like you wrote the book in it or something.
    I feel that you simply can do with some % to drive the message house
    a bit, however instead of that, that is magnificent blog.
    A fantastic read. I will definitely be back.

  61. You really make it seem so easy with your presentation but I find this matter to be really something
    that I think I would never understand. It seems too complicated and extremely broad for
    me. I am looking forward for your next post,
    I’ll try to get the hang of it!

  62. Simply desire to say your article is as astounding. The clarity for your put up is
    simply spectacular and that i could think you’re an expert on this subject.

    Fine along with your permission let me to take
    hold of your RSS feed to stay up to date with forthcoming
    post. Thanks 1,000,000 and please keep up the rewarding work.

  63. Pingback: cialis com coupons
  64. Pingback: viagra canada
  65. I’d like to thank you for the efforts you’ve put in penning this
    blog. I’m hoping to see the same high-grade content by you later on as well.
    In truth, your creative writing abilities has encouraged me to get my own website now 😉

  66. Hey would you mind letting me know which hosting company
    you’re working with? I’ve loaded your blog in 3 completely different
    browsers and I must say this blog loads a lot quicker then most.
    Can you recommend a good hosting provider at a fair price?
    Thanks, I appreciate it!

  67. Pingback: best online viagra
  68. I just like the helpful information you supply on your articles.

    I’ll bookmark your blog and take a look at again right here regularly.
    I’m relatively sure I’ll be informed lots
    of new stuff right here! Best of luck for the following!

  69. Pingback: generic viagra names
  70. I have been browsing on-line greater than 3 hours
    these days, yet I by no means found any fascinating article like yours.

    It’s pretty price sufficient for me. In my opinion, if
    all website owners and bloggers made excellent content as you probably
    did, the internet will likely be much more useful than ever before.

  71. Pingback: amlodipine swelling
  72. Pingback: cialis generic canada
  73. Pingback: generic cialis cost
  74. I’ve bsen browsibg onlpine morre thaan 4 hhours today, yyet I nevver fpund anyy interestinng article
    like yours. It’s pretty wortth enougbh ffor me. In mmy
    opinion, iff all webb wners annd bloggers madxe god
    content aas yoou did, thhe intgernet will bee muhh moire usweful than evr before.

  75. Its like you read my mind! You appear to understand so much approximately this, such as you wrote the e-book in it or something.
    I feel that you just could do with a few % to power the message home a little bit, however instead of
    that, this is great blog. An excellent read. I will definitely be back.

  76. I think this is among the so much important info for me.
    And i’m satisfied studying your article. However wanna observation on some
    basic things, The website style is wonderful, the articles is
    truly nice : D. Excellent job, cheers

  77. Pingback: dapoxetine cream
  78. Pingback: clomid and cm
  79. Pingback: tamoxifen magnesium
  80. Pingback: tizanidine hcl dosage
  81. Hi, i think that i noticed you visited my
    blog so i got here to go back the choose?.I am attempting to in finding things to improve my web site!I suppose its
    good enough to make use of some of your ideas!!

  82. Having read this I thought it was really enlightening.
    I appreciate you spending some time and effort to put this short article together.
    I once again find myself personally spending way too much time
    both reading and commenting. But so what, it was still worth it!

  83. Pingback: cialis patent
  84. It’s appropriate time to make a few plans for the future and it is time to be happy.
    I’ve read this publish and if I could I want to counsel you some attention-grabbing issues
    or advice. Perhaps you can write subsequent articles relating to this article.

    I desire to read even more issues approximately it!

  85. I loved as much as you’ll receive carried out right here.
    The sketch is attractive, your authored subject matter stylish.
    nonetheless, you command get bought an nervousness over that you wish be
    delivering the following. unwell unquestionably come more formerly again since exactly the same nearly very
    often inside case you shield this increase.

  86. Pingback: cialis extraforrel
  87. Pingback: online real viagra
  88. It’s perfect time to make some plans for the future
    and it’s time to be happy. I’ve read this post and if I could I desire to suggest you few interesting things or suggestions.

    Perhaps you could write next articles referring to this article.
    I want to read more things about it!

  89. I’m amazed, I have to admit. Rarely do I encounter a blog that?s equally educative and amusing, and without
    a doubt, you’ve hit the nail on the head. The issue is something not enough men and
    women are speaking intelligently about. I’m very happy I came across this in my
    hunt for something concerning this.

    Also visit my blog post; 강남오피

  90. Hi, i read your blog from time to time and i own a similar one and i was just curious if you get a
    lot of spam comments? If so how do you reduce it, any plugin or anything you
    can suggest? I get so much lately it’s driving me
    mad so any assistance is very much appreciated.

  91. I am glad for commenting to make you be aware of of the awesome encounter my friend’s princess found browsing your blog. She realized several details, which included what it’s like to possess an awesome giving nature to have others with no trouble fully grasp a number of very confusing topics. You undoubtedly exceeded people’s desires. Thank you for providing these great, trustworthy, educational as well as easy guidance on this topic to Mary.

  92. Appreciating the time and energy you put into your website and detailed information you
    provide. It’s nice to come across a blog every once in a while that isn’t the same outdated rehashed information. Fantastic read!
    I’ve saved your site and I’m including your RSS feeds to
    my Google account.

  93. Hi there, i read your blog occasionally and i own a similar one and i was
    just wondering if you get a lot of spam feedback? If so how do you protect
    against it, any plugin or anything you can advise?
    I get so much lately it’s driving me mad so any
    support is very much appreciated.

  94. Hello There. I found your weblog the use of msn. This
    is an extremely neatly written article. I’ll be sure to bookmark it and return to learn more
    of your helpful information. Thanks for the post. I will certainly return.

  95. My developer is trying to convince me to move to .net from PHP.
    I have always disliked the idea because of the costs.
    But he’s tryiong none the less. I’ve been using WordPress on a number of websites
    for about a year and am nervous about switching to another platform.
    I have heard great things about blogengine.net. Is there
    a way I can import all my wordpress posts into it?
    Any help would be really appreciated!

  96. Great goods from you, man. I have understand your stuff previous to and you’re just extremely wonderful.
    I actually like what you’ve acquired here,
    really like what you’re saying and the way in which you say it.

    You make it enjoyable and you still care for to keep it wise.
    I cant wait to read far more from you. This is actually a terrific website.

  97. Good day! I know this is somewhat off topic but I was wondering if you knew where I could
    locate a captcha plugin for my comment form?
    I’m using the same blog platform as yours and I’m having difficulty finding one?
    Thanks a lot!

  98. Admiring the dedication you put into your site and in depth information you provide.
    It’s nice to come across a blog every once in a while that isn’t
    the same out of date rehashed information. Fantastic read!
    I’ve saved your site and I’m including your RSS feeds to my Google account.

  99. I don’t even know how I stopped up right here, however I thought this put
    up was once great. I do not understand who you might be but definitely
    you’re going to a well-known blogger should you
    aren’t already. Cheers!

  100. Pingback: sensepeanut
  101. Do you have a spam issue on this website; I
    also am a blogger, and I was curious about your situation; many
    of us have developed some nice practices and we are
    looking to trade strategies with others, why not
    shoot me an email if interested.

  102. Hey! I know this is kind of off topic but I was wondering if you
    knew where I could find a captcha plugin for my comment form?
    I’m using the same blog platform as yours and I’m having difficulty finding one?

    Thanks a lot!

  103. Howdy, i read your blog occasionally and i own a
    similar one and i was just wondering if you get a lot of
    spam comments? If so how do you prevent it, any plugin or anything
    you can recommend? I get so much lately it’s driving me crazy so any support is very much appreciated.

  104. Hello! I realize this is sort of off-topic but I
    had to ask. Does running a well-established website like yours require a lot of work?

    I’m completely new to operating a blog however I do write
    in my diary every day. I’d like to start a blog so I
    can share my personal experience and feelings online. Please let me know if
    you have any kind of suggestions or tips for brand new aspiring blog owners.

    Thankyou!