首页 > 技术相关, 数据库 > Redis实现之虚拟存储系统(VM)实现(二)–数据交换(Swap)的实现

Redis实现之虚拟存储系统(VM)实现(二)–数据交换(Swap)的实现

2011年7月17日 发表评论 阅读评论

Redis的虚拟存储系统(VM subsystem)的目的是实现将Redis对象(Redis Objects)方便的在主存(Memory)和硬盘(Disk)之间交换。在Redis虚拟存储系统中,Redis仅仅会将和值(Values)关联的对象交换(swap)到硬盘上。在前一篇日志中,对VM涉及的数据结构进行简要介绍,在这篇日志中,将介绍数据交换的具体过程。

在具体讲Redis数据交换的具体实现之前,有必要讲下swap文件,swap文件是由若干页(page)组成的,每页包含着给定字节数的数据。由于不同的redis实例的最优配置不一样(取决于你实际存储数据的大小),这些参数可以通过redis.conf文件进行修改。下面是默认的大小:

 vm-page-size 32
vm-pages 134217728
 

Redis使用位向量(英文为bitmap,这是一些连续的位,每位的值为0或1)来表示在硬盘中page是否被使用。假如一个给定位是1,则表示该页被使用(有swap文件存在上面),为0,表示该页未被使用。

通过在内存中使用位向量(下面将称为页表位向量),可以在很少的内存消耗情况下取得巨大的性能提升,因为我们仅仅只需要为每页提供一位。对于上面的默认配置,总共有4G的虚存,但是只需要16M的内存给页表位向量。

为了将主存上的数据交换到交换空间,我们需要做如下步骤(假设没有使用虚存线程,仅仅是块实现)。

  1. 找出需要多少页来存储交换文件。这仅仅需要通过调用rdbSavedObjectPages函数即可返回需要的页数。
  2. 知道需要的页数后,我们需要找到交换空间中一些连续的页来存储。这是通过vmFindContiguousPages函数实现的。这个函数有可能因为内存满了而失败,也有可能因为找不到连续的页失败。当这种情况发生时,交换将被取消,数据将继续保存在内存中。
  3. 最终,我们只需要调用vmWriteObjectOnSwap函数即可以将数据交换到对应的位置。数据交换完成后,对应的主存被释放,对应的key也被标记为REDIS_VM_SWAPPED,而对应的页表位向量也被标记为使用中。
    而将交换空间的数据切回到主存中,则很简单,由于知道对象存的位置以及占用页数。只需要调用vmLoadObject即可以完成。

注:本文内容翻译自redis源码包doc目录中的相关文档。



本文作者: Sigma    在新浪微博关注SigmaSigmaWeibo    RSS订阅本博客
本文链接: http://www.sigma.me/2011/07/17/redis-vm-implement-swap-processing.html
本博客采用知识共享署名—非商业性-禁止演绎使用3.0协议进行许可,转载请保留作者和原文链接。

  1. 2011年7月24日02:21 | #1

    。。。你的防垃圾评论系统不照啊。。。

  2. 2011年7月24日06:07 | #2

    @grapeot

    是啊,都要手动删。。。

    并且也没有过滤系统,尤其是什么宁波的,一系列垃圾评论

  3. 2011年7月23日08:43 | #3

    一个人吃饭,旅行,看书,写信,留言,自己对话谈心。

  4. 2011年8月17日06:07 | #4

    vm装了N次装不起,索性不装鸟~

  5. 2019年1月9日11:57 | #5

    I precisely desired to say thanks yet again. I am not sure what I could possibly have used without those thoughts shown by you about my subject. It had become an absolute challenging crisis in my circumstances, nevertheless finding out your well-written way you dealt with it took me to cry for joy. I will be happy for your help and in addition hope that you realize what a great job you’re getting into instructing the rest via a site. Probably you haven’t met any of us.

  6. 2019年1月10日08:47 | #6

    Needed to post you this bit of remark in order to give thanks yet again with the wonderful solutions you have discussed in this case. It has been really pretty generous of you to supply unreservedly exactly what a few individuals could possibly have advertised for an ebook to help make some dough for themselves, precisely considering the fact that you could have done it in case you decided. The inspiring ideas as well worked to be the easy way to be sure that most people have similar dream really like my own to understand significantly more on the subject of this condition. Certainly there are several more enjoyable sessions up front for folks who see your blog post.

  7. 2019年1月11日00:57 | #7

    I precisely wanted to appreciate you once more. I’m not certain the things that I would’ve implemented in the absence of those information contributed by you over such problem. It had been the depressing condition for me, nevertheless witnessing the very specialised avenue you resolved it made me to leap for joy. I am just happier for the support and in addition hope that you recognize what an amazing job your are putting in teaching others through the use of your webblog. I am certain you haven’t encountered all of us.

  8. 2019年1月11日04:52 | #8

    Thanks for all of your labor on this web page. Kate take interest in participating in investigation and it’s really easy to see why. I notice all relating to the lively method you deliver effective guides via this web site and as well as attract contribution from other individuals on this issue while our favorite child is always becoming educated a great deal. Take advantage of the remaining portion of the year. You’re conducting a really great job.

  9. 2019年1月12日00:29 | #9

    Thanks for all your effort on this website. Gloria really loves conducting research and it is obvious why. I notice all relating to the compelling tactic you present very useful thoughts on the blog and welcome response from some other people on this content plus our favorite daughter is always discovering a great deal. Have fun with the rest of the year. You are always doing a fantastic job.

  10. 2019年1月12日16:43 | #10

    I precisely needed to thank you very much yet again. I am not sure the things that I would’ve used in the absence of the entire basics revealed by you on such question. It has been the depressing problem in my view, but taking a look at the very well-written style you treated the issue made me to weep for contentment. Now i’m happier for the help and then have high hopes you really know what an amazing job your are putting in teaching the others all through your website. More than likely you’ve never encountered any of us.

  11. 2019年1月13日11:43 | #11

    Thank you for your whole effort on this web site. My mum take interest in getting into internet research and it’s easy to understand why. Almost all notice all concerning the lively manner you convey very important things by means of this blog and therefore attract response from other people on that article and our own daughter is without question learning so much. Take advantage of the remaining portion of the year. You are always doing a tremendous job.

  12. 2019年1月14日07:12 | #12

    I together with my buddies happened to be digesting the best tips and tricks located on the blog and before long developed a terrible suspicion I never thanked the website owner for those tips. All of the young men are already certainly glad to learn all of them and have now simply been taking pleasure in these things. Appreciation for actually being really kind and for opting for some ideal resources millions of individuals are really wanting to learn about. Our honest regret for not expressing appreciation to sooner.

  1. 本文目前尚无任何 trackbacks 和 pingbacks.

无觅相关文章插件,快速提升流量