数百/数千个 tcp 套接字对于 memcached 是否合理?
我正在使用 Merb::Cache 来存储 txt/xml,并且注意到我让 merbs 运行的时间越长,我打开的打开的 TCP 套接字数量就越大——我相信这会导致一些主要的性能问题。
lsof | grep 11211 | wc -l 494
merb 27206 root 71u IPv4 13759908 TCP localhost.localdomain:59756->localhost.localdomain:11211 (ESTABLISHED) merb 27206 root 72u IPv4 13759969 TCP localhost.localdomain:59779->localhost.localdomain:11211 (ESTABLISHED) merb 27206 root 73u IPv4 13760039 TCP localhost.localdomain:59805->localhost.localdomain:11211 (ESTABLISHED) merb 27206 root 74u IPv4 13760052 TCP localhost.localdomain:59810->localhost.localdomain:11211 (ESTABLISHED) merb 27206 root 75u IPv4 13760135 TCP localhost.localdomain:59841->localhost.localdomain:11211 (ESTABLISHED) merb 27206 root 76u IPv4 13760823 TCP localhost.localdomain:59866->localhost.localdomain:11211 (ESTABLISHED) merb 27206 root 77u IPv4 13760951 TCP localhost.localdomain:52095->localhost.localdomain:11211 (ESTABLISHED)
等等...
我的相关代码是:
if !exists?(:memcached) then register(:memcached, Merb::Cache::MemcachedStore, :namespace => 'mynamespace', :servers => ['127.0.0.1:11211']) end
&&
when :xml unless @hand_xml = Merb::Cache[:memcached].read("/hands/#{@hand.id}.xml") @hand_xml = display(@hand) Merb::Cache[:memcached].write("/hands/#{@hand.id}.xml", @hand_xml) end return @hand_xml
这段代码是直接错误的还是我使用了错误版本的内存缓存?
我有memcached 1.2.8 并具有以下内容:
libmemcached-0.25.14.tar.gz memcached-0.13.gem
这有点让我发疯..
I'm using Merb::Cache for storing txt/xml and have noticed that the longer I leave my merbs running the larger the amount of open tcp sockets I have open -- I believe this is causing some major performance problems.
lsof | grep 11211 | wc -l 494
merb 27206 root 71u IPv4 13759908 TCP localhost.localdomain:59756->localhost.localdomain:11211 (ESTABLISHED) merb 27206 root 72u IPv4 13759969 TCP localhost.localdomain:59779->localhost.localdomain:11211 (ESTABLISHED) merb 27206 root 73u IPv4 13760039 TCP localhost.localdomain:59805->localhost.localdomain:11211 (ESTABLISHED) merb 27206 root 74u IPv4 13760052 TCP localhost.localdomain:59810->localhost.localdomain:11211 (ESTABLISHED) merb 27206 root 75u IPv4 13760135 TCP localhost.localdomain:59841->localhost.localdomain:11211 (ESTABLISHED) merb 27206 root 76u IPv4 13760823 TCP localhost.localdomain:59866->localhost.localdomain:11211 (ESTABLISHED) merb 27206 root 77u IPv4 13760951 TCP localhost.localdomain:52095->localhost.localdomain:11211 (ESTABLISHED)
etc...
my relevant code is :
if !exists?(:memcached) then register(:memcached, Merb::Cache::MemcachedStore, :namespace => 'mynamespace', :servers => ['127.0.0.1:11211']) end
&&
when :xml unless @hand_xml = Merb::Cache[:memcached].read("/hands/#{@hand.id}.xml") @hand_xml = display(@hand) Merb::Cache[:memcached].write("/hands/#{@hand.id}.xml", @hand_xml) end return @hand_xml
is this code straight out wrong or am I using the wrong version of memcache??
I have memcached 1.2.8
and have the following:
libmemcached-0.25.14.tar.gz
memcached-0.13.gem
this is kind of driving me crazy..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
k 我想出了一些东西..
1)假设您使用的是利用 epoll 或其他东西的库,那么将数百/数千个套接字连接到 memcached 是合理的 - 但是,如果您像我一样使用 ruby,我不知道有一个库使用了 select() 或 poll() 之外的其他东西——因此这引发了这个问题/想立即退出
2) 如果你像我一样,你现在只有 1 个 memcached 服务器正在运行和几个杂种/ Thins 四处运行以处理请求..因此您的内存缓存连接应该有问题。 不超过你运行的杂种/稀有的数量(假设你只缓存 1 或两组东西)——这是我的情况,
这里是修复:
通过 memcached gem 设置 memcache,而不是 merb::cache (实际上包装无论你使用什么memcache lib,
/设置你的值:
坐下来喝一杯冷饮,因为当你这样做时:
你会看到像2或3这样的东西,而不是2036!
获取 连接首先要持久
k I figured out some stuff..
1) it CAN be reasonable to have hundreds/thousands of sockets connected to memcached assuming you are using a library that utilizes epoll or something else -- however, if you are using ruby like me I'm not aware of a lib that utilizes something else than select() or poll() -- therefore this strikes this question/want out immediately
2) if you are like me you only have 1 memcached server running right now and a couple of mongrels/thins running around taking care of requests..therefore your memcache connections should prob. be no more than the number of mongrels/thins you have running (assuming you only caching 1 or two sets of things) -- which was my case
here's the fix:
setup memcache through memcached gem rather than merb::cache (which actually wraps whatever memcache lib you are using
get/set your values:
sit back and drink a cold one cause now when you do this:
you see something like 2 or 3 instead of 2036!
props to reef for cluing me in that it's not uncommon for memcache connections to be persistent to begin with
我也许能帮忙,但我需要讲一个故事才能做到这一点。 这里是。
曾几何时,有一个由 10 个 apache(ssl) 服务器组成的集群,每个服务器配置正好有 100 个线程。 还有一个由 10 台 memcached 服务器组成的集群(位于同一台机器上),它们似乎都平静地生活着。 apache 和 memcached 都受到邪恶的 monit 守护进程的保护。然后
国王安装了第 11 个 apache(ssl) 服务器,memcached 开始每隔几个小时随机重新启动一次! 国王开始调查,他发现了什么? php memcache 模块文档中有一个错误,指出 memcache 连接对象的默认构造函数是不持久化的,但显然它是持久化的。 发生的情况是,每个 php 线程(大约有 1000 个)在需要时都会打开与池中每个 memcached 的连接,并且它会保留该连接。 每个 memcached 服务器都有 10*100 个连接,这很好,但是对于 11 个服务器,连接数为 1100,并且为 1024<1100。 memcached 的最大打开套接字数为 1024。当所有套接字都被占用时,monit 守护进程无法连接,因此他重新启动了 memcached。
每个故事都必须有一个寓意。 那么,国王对这一切做了什么呢? 他禁用了持久连接,他们从此过上了幸福的生活,集群上的连接数量达到了峰值 5(五)。 这些服务器正在提供大量数据,因此我们无法拥有 1000 个备用套接字,而且根据每个请求协商内存缓存连接会更便宜。
抱歉,我不了解 ruby,看起来您的线程数量非常多,或者您缓存错误。
祝你好运!
I might be able to help, but I need to tell a story to do that. Here it is.
Once upon a time there was a cluster of 10 apache(ssl) servers configured to have exactly 100 threads each. There also was a cluster of 10 memcached servers (on the same boxes), and they all seemed to live peacefully. Both apache's and memcached's were guarded by the evil monit daemon.
Then the King installed a 11th apache(ssl) server and memcached's started to restart randomly every few hours! The King started investigating and what did he found? There was a bug in the php memcache module documentation that said that the default constructor of memcache connection object is not persistent, but apparently it was. What happened was that every php thread (and there were like 1000 of them), opened a connection to every memcached in the pool when he needed one, and it held it. There were 10*100 connections to every memcached server and it was fine, but with 11 servers it was 1100 and as 1024<1100. Maximum number of open sockets for memcached was 1024. When all the sockets were taken, the monit daemon couldn't connect, so he restarted the memcached.
Every story has to have a moral. So, what did the King do with all of this? He disabled the persistent connections and they all lived happily ever after, with number of connections on the cluster peaking at 5 (five). Those servers were serving hudge amount of data, so We couldn't have 1000 spare sockets and it was cheaper to negotiate the memcache connection on every request.
I am sorry but I don't know ruby, it looks like You had an awful amount of threads or You are caching it wrong.
Good luck!