0

所以我试图加快我的数据库驱动的网站,我遇到了 memcached,我显然可以用它来“缓存”内存中数据库中的频繁键/值。我的问题是,如何强制数据留在内存中,因为通常如果服务器正在运行其他应用程序,则有可能memcached hashtable(或其中的一部分)会在一个页面被写回硬盘来自另一个应用程序的页面替换了 memcached 中的页面。那么在工业界,他们有单独的机器只运行 memcached 吗?或者他们是否调整了操作系统的内部结构,以使 memcached 中的页面不会被换出?

这个问题可以推广到任何其他应用程序,知道如何强制数据留在内存中是非常有益的。

4

2 回答 2

2

By default memcached pages will be swapped out to the hard disk if your machine is using more physical memory than it has available. You can specify the -k parameter however to specify that the pages memcached is using be locked into physical memory with mlockall(). See this blog post for more information on how to do this.

http://threebrothers.org/brendan/blog/using-memcached-k-prevent-paging/

于 2013-02-09T19:11:48.107 回答
0

The OS writers have written good algorithms for paging over the years; algo that have stood the test of time. The paging algorithm tries to optimize the pages it swaps out.

'it would be very beneficial to to force the data to stay in memory' - sure about that? Would you rather have your Apache code being swapped out, rather than a page of data that was not used for an hour? If Apache swaps out, who would use that data?

It is difficult to design something that beats native OS paging algo. Can be done, but usually requires more than just locking memory for 1 program.

I would recommend setting memcache size appropriately so that the machine does not use paging.

If you run memcache solely on the system, which many people do, there is not much need to fiddle with -k switch. Or that machine can be set to swap = 0.

于 2013-03-13T03:23:43.567 回答