首页 > 技术相关, 数据库 > Redis实现之虚拟存储系统(VM)实现(三)–阻塞式虚存(Blocking VM)交换

Redis实现之虚拟存储系统(VM)实现(三)–阻塞式虚存(Blocking VM)交换

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

个人理解,所谓阻塞式虚存(Blocking VM),是指redis发生数据交换时,进程被阻塞,不能干其他的事情(后面一篇文章将讲到Threaded VM相反,有独立VM线程)。

在Redis中,启用阻塞式虚存需要在配置文件中将server.vm_max_threads设成0(即不为Threaded VM),和VM另一个相关配置变量是sever.vm_max_memory,它表示Redis中数数据所占用的最大内存(实际上运行过程中可能大于这个值),只有数据量占用内存大于这个值时,才会出现数据交换到disk。

交换到硬盘

数据交换是通过一个计划任务函数(cron function)实现的。这个函数每隔一定的时间会检查是否超出了内存最大值(out of memory)。假如发现超出了最大值,则通过循环调用vmSwapOneObject(这个函数只有一个参数,0表示阻塞式,1表示线程式)来将数据交换到硬盘。

vmSwapOneObject实现如下:

  1. 和Cache替换一样,很关键的一点就是需要找到需要交换出去的Object,使得交换代价最小(后面会讲redis是如何决定的)。
  2. 被交换出去的object关联的值(value)通过阻塞式被交换到硬盘。
  3. key的storage域被设置成REDIS_VM_SWAPPED,vm域也将改写,包括页表索引值,以及所占用的页数。
  4. 释放被swap的object的内存,在哈希表(hash table)中将对应的value的入口设置成NULL。

这个函数一直被调用,直到如下情况:swap空间已满,或者数据对象占用的内存小于sever.vm_max_memory。

替换算法

在Redis中,VM系统所用的替换算法和Cache里面的LRU有点不同(但有点类似,只是多加数据大小这个权重),redis是通过一个权值swappability来决定被替换出去的对象(swappability越大,表示越优先被交换),swappability的计算方法如下:

swappability = age*log(size_in_memory)

上式中的age是指对应的数据对象多久未被访问,而size_in_memory是指数据对象占用内存的大小。从这个算法可以看出,根据局部性原理,redis是想将最少访问的数据交换出去,同时减少交换次数(将大的数据交换)。但是后者的权重比较低,所以取了对数(有点不明白为什么要用这么复杂的函数,感觉这计算所耗的时间比较多)。

阻塞式的缺陷

阻塞式数数据交换的缺陷很明显,那就是如其名,阻塞。对于批处理的数据库应用,这也许不算缺陷,因为只在乎总的时间,而不在乎某个请求的响应时间。而对于实时系统,大部分应用都应该属于此类,却是致命的,在发生数据交换时,数据库访问的请求的响应延时会非常大。

为了解决这个问题,需要在后台完成数据交换,于是出现了非阻塞式的虚存系统,那就是线程式虚存(Threaded VM)。



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

  1. 2011年8月1日03:27 | #1

    你通不通过我的留言,我就在那里,不悲不喜

  2. 2011年7月26日07:50 | #2

    呵呵我也来看看了啊 u 广告顺道点点啊

  3. 2019年1月15日01:00 | #3

    I precisely needed to say thanks yet again. I do not know the things I could possibly have made to happen in the absence of the entire thoughts shared by you concerning my subject. It seemed to be an absolute difficult difficulty in my view, but observing your specialised way you managed the issue made me to cry over contentment. I am happy for this assistance and thus hope you know what a great job you have been accomplishing educating most people via a web site. More than likely you haven’t got to know all of us.

  4. 2019年1月15日08:51 | #4

    I really wanted to construct a small remark in order to thank you for those wonderful steps you are writing at this website. My long internet research has at the end been honored with reasonable strategies to exchange with my family and friends. I ‘d assume that most of us website visitors are unequivocally blessed to live in a great place with so many awesome individuals with helpful ideas. I feel truly privileged to have seen your site and look forward to so many more fun moments reading here. Thanks once again for everything.

  5. 2019年1月15日17:51 | #5

    A lot of thanks for your own work on this web page. Kate really loves managing internet research and it is easy to understand why. A number of us hear all concerning the lively way you provide advantageous strategies via this blog and improve response from other ones on this situation then our own daughter is without a doubt studying a lot of things. Have fun with the remaining portion of the new year. You are always doing a useful job.

  6. 2019年1月16日03:13 | #6

    I needed to compose you that little note to help give thanks the moment again about the pleasing information you’ve discussed on this page. This has been certainly generous with you to supply freely all many people could possibly have supplied for an e-book to help make some cash for their own end, specifically considering the fact that you could possibly have tried it in the event you desired. The smart ideas also acted as a good way to fully grasp most people have a similar zeal similar to my personal own to grasp good deal more in regard to this condition. I believe there are many more enjoyable situations up front for those who check out your blog post.

  7. 2019年1月16日10:46 | #7

    My wife and i ended up being very contented Peter could finish up his inquiry through the entire precious recommendations he made out of the web page. It’s not at all simplistic to simply continually be handing out helpful hints that many other folks may have been trying to sell. And now we do understand we have got you to thank because of that. All the explanations you’ve made, the easy website navigation, the relationships your site assist to engender – it is most fabulous, and it is facilitating our son in addition to us imagine that this situation is amusing, which is certainly wonderfully fundamental. Thanks for everything!

  8. 2019年1月16日12:23 | #8

    I wanted to send you a very small observation to be able to say thanks once again on the exceptional pointers you’ve shown in this case. This has been so surprisingly open-handed of you to provide freely exactly what a number of people would’ve distributed for an e-book to earn some bucks for themselves, most importantly now that you could have tried it in case you wanted. These basics also acted to provide a easy way to comprehend some people have similar passion really like my personal own to see very much more with reference to this matter. I believe there are millions of more enjoyable periods ahead for those who looked over your website.

  9. 2019年1月16日20:24 | #9

    I and also my guys appeared to be reading through the great secrets located on your website then all of the sudden got a terrible feeling I had not thanked the blog owner for those strategies. All the guys are actually for that reason warmed to learn them and have in effect in fact been enjoying them. Appreciate your genuinely indeed helpful and for making a choice on this form of wonderful ideas millions of individuals are really desperate to know about. Our honest apologies for not expressing appreciation to sooner.

  10. 2019年1月17日06:08 | #10

    My wife and i got so comfortable that John managed to complete his web research from the precious recommendations he received from your own site. It is now and again perplexing to simply happen to be offering tips and hints which often people may have been trying to sell. We really do know we’ve got you to appreciate for that. All the explanations you’ve made, the easy website menu, the relationships you will give support to engender – it is all superb, and it’s leading our son in addition to the family do think that issue is enjoyable, which is certainly very indispensable. Many thanks for all the pieces!

  11. 2019年1月17日14:10 | #11

    A lot of thanks for your whole labor on this web site. My mother takes pleasure in conducting internet research and it’s really easy to see why. Most people notice all of the lively means you give sensible solutions by means of this website and even strongly encourage response from website visitors on this area of interest then our girl has always been starting to learn a lot of things. Have fun with the rest of the new year. You are always carrying out a fantastic job.

  12. 2019年1月18日00:18 | #12

    My spouse and i ended up being now thrilled when John could carry out his investigations because of the ideas he had out of your web page. It is now and again perplexing just to possibly be freely giving strategies that many others have been selling. So we discover we have the website owner to be grateful to because of that. The main illustrations you have made, the straightforward website navigation, the relationships you can make it easier to create – it is mostly astonishing, and it’s helping our son and the family reckon that this issue is enjoyable, and that’s exceptionally mandatory. Many thanks for all!

  13. 2019年1月18日07:55 | #13

    Thanks for your whole hard work on this website. My mum really loves carrying out research and it’s easy to see why. Many of us learn all relating to the lively means you convey very helpful items via the blog and as well increase participation from people about this article so our favorite girl is now being taught so much. Have fun with the remaining portion of the new year. You’re doing a fantastic job.

  14. 2019年1月18日15:27 | #14

    I simply wanted to say thanks once again. I do not know what I would’ve used without these points discussed by you over that topic. It absolutely was a depressing concern in my view, however , taking a look at a new specialised tactic you resolved that made me to jump over fulfillment. Extremely grateful for your assistance as well as believe you are aware of an amazing job you were undertaking training some other people by way of your website. Most likely you have never come across any of us.

  15. 2019年1月19日01:11 | #15

    Thank you a lot for giving everyone an extraordinarily breathtaking opportunity to read critical reviews from this site. It’s usually very terrific plus stuffed with a lot of fun for me and my office friends to visit your blog at least thrice per week to find out the newest things you will have. And indeed, I’m also actually amazed concerning the mind-boggling principles served by you. Certain two tips in this article are definitely the most effective we have all ever had.

  16. 2019年1月19日07:59 | #16

    I in addition to my buddies ended up studying the excellent advice located on your website and then all of the sudden got an awful feeling I had not expressed respect to the site owner for those strategies. The women became for that reason stimulated to read them and already have honestly been using these things. Appreciate your getting indeed considerate and also for making a decision on variety of great tips millions of individuals are really desirous to be informed on. My personal sincere apologies for not saying thanks to earlier.

  17. 2019年1月19日08:34 | #17

    Thank you for all your valuable hard work on this website. My mother takes pleasure in going through internet research and it is simple to grasp why. Most people learn all regarding the powerful mode you provide sensible tactics through the web blog and as well inspire response from website visitors on this situation while my daughter is really starting to learn a lot. Take pleasure in the rest of the year. You are always carrying out a stunning job.

  18. 2019年1月19日15:58 | #18

    I must point out my admiration for your generosity in support of men who really want assistance with this one subject matter. Your personal commitment to getting the message all-around had been incredibly invaluable and has usually empowered most people like me to arrive at their pursuits. The invaluable guidelines denotes a great deal to me and additionally to my office colleagues. Thank you; from all of us.

  19. 2019年1月19日16:45 | #19

    I simply wished to say thanks once again. I am not sure the things I might have done in the absence of those concepts shared by you over this theme. It previously was a very terrifying matter in my position, but being able to view your specialized form you managed it took me to jump over gladness. Extremely thankful for this work and then sincerely hope you realize what a great job you happen to be doing teaching some other people with the aid of your website. Probably you’ve never met all of us.

  20. 2019年1月20日02:03 | #20

    I would like to express appreciation to the writer for rescuing me from such a challenge. Just after looking through the search engines and meeting suggestions which are not powerful, I was thinking my life was over. Existing minus the answers to the problems you have solved as a result of the blog post is a critical case, and the kind that might have negatively affected my career if I had not noticed your web page. Your personal mastery and kindness in controlling everything was very helpful. I am not sure what I would have done if I hadn’t encountered such a thing like this. I can also now relish my future. Thank you so much for the reliable and sensible help. I won’t be reluctant to refer your web page to any person who would need tips on this situation.

  21. 2019年1月20日08:45 | #21

    Thanks so much for giving everyone an extremely breathtaking chance to discover important secrets from this blog. It really is so brilliant and also packed with a great time for me and my office mates to search your web site more than three times in a week to read the fresh tips you have got. And indeed, I am just at all times contented for the tremendous solutions served by you. Certain 1 ideas in this article are unquestionably the finest I have ever had.

  22. 2019年1月20日17:37 | #22

    I as well as my friends ended up checking the nice pointers found on the website then instantly developed a horrible suspicion I had not thanked the website owner for them. The guys ended up stimulated to study all of them and already have sincerely been making the most of those things. We appreciate you really being very kind and for picking out certain helpful resources millions of individuals are really desirous to discover. Our honest apologies for not saying thanks to you sooner.

  23. 2019年1月21日09:33 | #23

    Thank you so much for giving everyone an exceptionally nice possiblity to check tips from this web site. It is often very amazing and as well , full of fun for me personally and my office acquaintances to visit your blog at the least thrice in a week to see the new items you have. Not to mention, we are usually astounded considering the breathtaking inspiring ideas served by you. Certain 4 tips in this post are ultimately the finest I have had.

  24. 2019年1月21日17:00 | #24

    I not to mention my guys appeared to be following the good items located on your site then before long got a horrible suspicion I had not expressed respect to the web site owner for those strategies. Those women happened to be for that reason happy to read them and already have extremely been taking pleasure in them. Thank you for simply being very helpful and then for figuring out this sort of remarkable ideas most people are really desperate to discover. My sincere regret for not expressing gratitude to you earlier.

  25. 2019年1月22日03:15 | #25

    I truly wanted to jot down a brief note in order to appreciate you for the amazing tips you are showing on this site. My considerable internet lookup has finally been paid with excellent points to talk about with my great friends. I would declare that most of us site visitors actually are rather fortunate to live in a superb network with very many perfect individuals with useful methods. I feel extremely happy to have seen the web page and look forward to some more enjoyable moments reading here. Thanks a lot again for everything.

  26. 2019年1月22日05:41 | #26

    My husband and i have been really ecstatic that Emmanuel managed to deal with his survey using the precious recommendations he acquired out of your blog. It is now and again perplexing just to always be offering points which the rest have been selling. Therefore we know we’ve got you to thank for this. The main illustrations you’ve made, the easy website navigation, the relationships your site make it possible to foster – it’s got most exceptional, and it is helping our son and the family feel that this content is entertaining, and that is rather fundamental. Thank you for all!

  27. 2019年1月22日11:30 | #27

    I simply wanted to construct a brief remark so as to appreciate you for those fantastic ways you are posting on this website. My extended internet search has finally been honored with good facts and strategies to share with my family. I would express that we site visitors actually are definitely blessed to be in a magnificent site with many brilliant people with very helpful tips and hints. I feel truly happy to have encountered the weblog and look forward to so many more entertaining moments reading here. Thank you again for a lot of things.

  28. 2019年1月22日19:34 | #28

    I and also my buddies were viewing the good guidelines found on the website and suddenly developed a horrible feeling I had not expressed respect to the site owner for those techniques. Those boys happened to be as a consequence joyful to learn all of them and have in effect certainly been having fun with these things. Thanks for actually being really kind and then for settling on certain essential subject matter millions of individuals are really eager to discover. My personal honest apologies for not saying thanks to you earlier.

  29. 2019年1月23日11:18 | #29

    I needed to compose you that little bit of word to help thank you very much as before on the extraordinary opinions you’ve provided here. It was so unbelievably generous with you to make publicly all a number of us could possibly have supplied as an ebook to generate some profit for themselves, chiefly considering the fact that you might have tried it in the event you considered necessary. These concepts as well worked to become a great way to fully grasp that most people have the same dreams really like mine to grasp a great deal more in respect of this matter. I believe there are some more pleasant opportunities ahead for folks who see your website.

  30. 2019年1月24日04:53 | #30

    Thanks for every one of your efforts on this web site. My aunt delights in participating in research and it’s obvious why. We all learn all regarding the dynamic means you make very helpful techniques on the web site and even invigorate contribution from the others on this area of interest while my girl is really understanding so much. Have fun with the rest of the new year. You have been conducting a fantastic job.

  31. 2019年1月24日11:58 | #31

    Needed to post you a bit of word in order to say thanks the moment again for those magnificent methods you’ve documented on this page. This is so particularly generous with you to make extensively precisely what some people could possibly have offered for sale as an e-book in making some profit for themselves, notably seeing that you might well have tried it if you ever desired. The techniques in addition worked to become a easy way to comprehend other individuals have the same fervor like mine to understand great deal more with regards to this issue. I am certain there are lots of more pleasurable times in the future for people who look into your site.

  32. 2019年1月24日21:26 | #32

    Thanks a lot for giving everyone such a marvellous opportunity to read critical reviews from this website. It is always so kind and jam-packed with fun for me personally and my office co-workers to visit your site at the least three times in one week to study the fresh issues you have. And definitely, we’re certainly pleased for the superb opinions you give. Some 2 points in this article are without a doubt the most suitable I’ve had.

  33. 2019年1月25日04:08 | #33

    Needed to put you one very little word just to give many thanks as before with the splendid guidelines you’ve featured in this article. It was quite incredibly open-handed with people like you to give extensively what exactly many of us would’ve advertised for an ebook to generate some money for their own end, especially since you could have tried it if you ever considered necessary. The principles as well worked to be the good way to fully grasp someone else have a similar interest the same as my own to find out whole lot more in respect of this problem. I know there are some more pleasurable situations in the future for individuals that start reading your blog.

  34. 2019年1月25日23:21 | #34

    I am only commenting to let you know what a wonderful discovery our child gained reading your site. She realized a lot of things, including what it is like to possess a very effective giving character to make others completely comprehend several extremely tough subject matter. You actually did more than visitors’ expectations. Thanks for presenting these precious, dependable, explanatory as well as unique tips about the topic to Sandra.

  35. 2019年1月25日23:38 | #35

    你通不通过我的留言,我就在那里,不悲不喜

  36. 2019年1月26日06:52 | #36

    I would like to point out my passion for your kindness supporting persons that actually need help on this content. Your real commitment to getting the solution all over became particularly productive and have without exception made regular people just like me to arrive at their goals. Your own useful help signifies so much to me and much more to my office colleagues. Regards; from each one of us.

  37. 2019年1月26日14:43 | #37

    I would like to express my gratitude for your kind-heartedness supporting persons that need assistance with this particular question. Your special commitment to passing the solution throughout turned out to be rather insightful and has really enabled somebody like me to get to their endeavors. Your personal warm and friendly tips and hints means this much to me and far more to my office colleagues. Many thanks; from everyone of us.

  38. 2019年1月27日08:11 | #38

    I really wanted to compose a quick comment to be able to thank you for all of the magnificent secrets you are writing at this site. My extended internet search has at the end of the day been recognized with pleasant facts and techniques to exchange with my classmates and friends. I would point out that most of us readers actually are really fortunate to dwell in a great site with so many lovely individuals with great suggestions. I feel pretty happy to have come across your entire site and look forward to so many more enjoyable minutes reading here. Thanks a lot again for everything.

  39. 2019年1月27日15:41 | #39

    I would like to get across my passion for your kind-heartedness for individuals that must have assistance with in this area of interest. Your personal commitment to passing the solution all around ended up being especially informative and has surely helped somebody much like me to get to their endeavors. Your entire valuable help implies this much a person like me and especially to my peers. Many thanks; from all of us.

  40. 2019年2月8日07:59 | #40

    I just want to say I’m beginner to blogging and certainly loved this website. Most likely I’m likely to bookmark your blog . You certainly have incredible article content. Kudos for revealing your website.

  41. 2019年2月9日20:02 | #41

    Spot on with this write-up, I truly suppose this web site wants way more consideration. I’ll in all probability be once more to read far more, thanks for that info.

  42. 2019年2月16日10:15 | #42

    I made a website using joomla and I have a domain and a server, the problem is whenever I go to that domain the site is still unviewable. Does onybody know how to help me out?.

  43. 2019年2月16日12:06 | #43

    Thank you, I’ve been looking for info about this subject for ages and yours is the best I have discovered so far.

  44. 2019年2月18日19:26 | #44

    What are the laws on republishing newspaper articles in a book? Are there copyright issues?

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

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