我正在没有配置任何交换空间的虚拟服务器上运行带有 apache+passenger 的 Rails 应用程序。
该网站获得了相当大的流量,每天有超过 20 万个请求,有时整个系统内存不足,导致整个系统出现奇怪的行为。
问题是有没有办法配置apache或passenger不耗尽内存(例如,当passenger实例开始使用时,比如超过300M的内存,优雅地重新启动它们)。
服务器有 4GB 内存,目前我正在使用乘客的 PassengerMaxRequests 选项,但它似乎不是最可靠的解决方案。
目前,我也无法切换到 nginx,因此这不是保留一些空间的选择。
欢迎我可能错过的任何聪明的想法。
编辑:我的临时解决方案
当 Rails 实例超过一定的内存使用量时,我没有重新启动它们。 Engine Yard 在 ActiveRecord 内存膨胀问题。这是我们对这个问题的主要怀疑。由于我没有太多时间来优化应用程序,因此我将 PassengerMaxRequests 设置为 300,并向服务器添加了额外的 2GB 内存。从那以后一切都很好。起初我担心不断重新启动 Rails 实例会使其变慢,但它似乎没有我应该担心的影响。
i'm running a rails application with apache+passenger on virtual servers that do not have any swap space configured.
The site gets decent amount of traffic with 200K+ daily requests and sometimes the whole system runs out of memory causing odd behaviour on whole system.
The question is that is there any way to configure apache or passenger not to run out of memory (e.g. gracefully restarting passenger instances when they start using, say more than 300M of memory).
Servers have 4GB of memory and currently i'm using passenger's PassengerMaxRequests option but it does not seem to be the most solid solution here.
At the moment, i also cannot switch to nginx so that is not an option to preserve some room.
Any clever ideas i'm probably missing are welcome.
Edit: My temporary solution
I did not go with restarting Rails instances when they exceed certain amount of memory usage. Engine Yard wrote great blog post on the ActiveRecord memory bloat issue. This is our main suspect on the subject. As i did not have much time to optimize application, i set PassengerMaxRequests to 300 and added extra 2GB memory to server. Things have been good since then. At first i was worried that re-starting Rails instances continuously makes it slow but it does not seem to have impact i should worry about.
发布评论
评论(4)
如果您的意思是“限制”为终止这些进程,并且这是服务器上唯一的应用程序并且它是 Linux,那么您有两种选择:
设置一个进程可以拥有的最大内存量:
或使用 cgroup 实现类似行为:
< a href="http://en.wikipedia.org/wiki/Cgroups" rel="nofollow">http://en.wikipedia.org/wiki/Cgroups
If you mean "limiting" as killing those processes and if this is the only application on the server and it is a Linux, then you have two choices:
Set maximum amount of memory one process can have:
Or use cgroups for similar behavior:
http://en.wikipedia.org/wiki/Cgroups
我建议不要重新启动超过“内存限制”的实例(如果可能的话),因为这可能会使您的系统陷入无限循环,其中进程反复达到该限制并重新启动。
也许您可以编写一个简单的守护进程来不断监视进程,并杀死任何超过一定内存量的进程。请务必记录有关执行此操作的进程的所有信息,以便您可以在问题出现时解决问题。
我还会考虑在那里获得一些真正的交换空间......这似乎是一个糟糕的黑客。
I would advise against restarting instances (if that is possible) that go over the "memory limit", because that may put your system in infinite loops where a process repeatedly reaches that limit and restarts.
Maybe you could write a simple daemon that constantly watches the processes, and kills any that go over a certain amount of memory. Be sure to log any information about the process that did this so you can fix the issue whenever it comes up.
I'd also look into getting some real swap space on there... This seems like a bad hack.
我遇到一个问题,乘客进程最终会失控并消耗太多内存。我编写了以下脚本,该脚本一直有助于控制事情,直到找到真正的解决方案 http://www.codeotaku.com/blog/2012-06/passenger-memory-check/index。这可能会有帮助。
Passenger Web 实例不包含重要状态(一般来说),因此杀死它们通常不是一个过程,并且 Passenger 将在需要时重新启动它们。
I have a problem where passenger processes end up going out of control and consuming too much memory. I wrote the following script which has been helping to keep things under control until I find the real solution http://www.codeotaku.com/blog/2012-06/passenger-memory-check/index. It might be helpful.
Passenger web instances don't contain important state (generally speaking) so killing them isn't normally a process, and passenger will restart them as and when required.
我没有固定的解决方案,但您可能想使用 Passenger 附带的两个命令来跟踪内存使用情况和进程数量:
passenger-status
和sudo Passenger-memory-统计数据
,参见Nginx 乘客用户指南 或
Apache 乘客用户指南。
I don't have a canned solution but you might want to use two commands that ship with Passenger to keep track of memory usage and nr of processes:
passenger-status
andsudo passenger-memory-stats
, seePassenger users guide for Nginx or
Passenger users guide for Apache.