如何使用集群正确缩放heroku上的nodejs应用程序 [英] How to properly scale nodejs app on heroku using clusters

查看:81
本文介绍了如何使用集群正确缩放heroku上的nodejs应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我在heroku日志中发现了一条警告,说这对我来说似乎很陌生。b / b
$ b


web.1:检测到512 MB可用内存,512每个进程的MB限制(WEB_MEMORY)

web.1:推荐WEB_CONCURRENCY = 1


我做了一些研究并找到了这个集群文章,这是在集群中使用集群的默认方式nodejs,但它完全矛盾于一个最近更新的文章,其中包含这个新的 WEB_CONCURRENCY 环境变量,并为每个dyno提供不同大小的建议(这是小得多的,顺便说一句)

解决方案

第一个链接来自2014年7月,并且曾经是推荐的做事方式。然而,Heroku的dynos是以内存为中心的,在使用每个CPU的最大内核数量时,超出分配的内存容量是非常容易的(正如第一篇文章所暗示的那样)。

相反,新的建议是分析您的应用程序并确定每个进程需要多少内存。将环境变量 WEB_MEMORY 设置为该值,然后将集群代码更新为以下内容:

  var cluster = require('cluster'); 
var numWorkers = process.env.WEB_CONCURRENCY;

if(cluster.isMaster){
//主进程:为我们的子进程fork
for(var i = 0; i< numWorkers; i ++){
cluster.fork();
}

//重新生成任何死于
cluster.on('exit',function(){
cluster.fork();
});

} else {
//子进程,在这里放置应用程序初始化代码。

$ / code>

通过使用 WEB_MEMORY 变量,Heroku可以生成一个 WEB_CONCURRENCY 值,具体取决于您正在运行的测试仪的大小,因此需要分配正确数量的进程以确保您的应用程序不会超出内存允许。

另外,如果你确实超过了内存分配(1x dyno的每个dyno 512MB),交换空间将用于超额。这会减慢你的应用程序,导致请求时间增加,并通常会导致缓慢。如果你的内存使用量超过了(大约是分配的三倍),Heroku会重新启动你的d​​yno。


I noticed a warning in heroku logs lately saying that seemed new to me

web.1: Detected 512 MB available memory, 512 MB limit per process (WEB_MEMORY)

web.1: Recommending WEB_CONCURRENCY=1

I did some research and found this cluster article, which is the "default" way to use clusters in nodejs, but it totally contradicts with a newly updated article that contains this new WEB_CONCURRENCY environment variable and with a different size suggestion for each dyno (which is much smaller, btw)

解决方案

The first link is from July 2014, and used to be the recommended way of doing things. However, Heroku's dynos are quite memory-centric, and it is very easy to exceed the allocated memory allowance when using the maximum number of cores per CPU (as the first article suggests).

Instead, the new recommendation is to profile your app and figure out how much memory is required per process. Set an environment variable WEB_MEMORY to this value, and then update your cluster code to the following:

var cluster = require('cluster');
var numWorkers = process.env.WEB_CONCURRENCY;

if(cluster.isMaster) {
  // Master process: fork our child processes
  for (var i = 0; i < numWorkers; i++) {
    cluster.fork();
  }

  // Respawn any child processes that die
  cluster.on('exit', function() {
    cluster.fork();
  });

} else {
  // Child process, put app initialisation code here.
}

By using the WEB_MEMORY variable, Heroku can generate a WEB_CONCURRENCY value depending on the size of the dyno you are running, and hence fork the correct number of processes to ensure that your app doesn't exceed memory allowance.

As an aside, if you do exceed the memory allocation (512MB per dyno for a 1x dyno), swap space will be used for the excess. This will slow down your app, causing request times to increase and will generally contribute to sluggishness. If you exceed the memory usage by too much (approx three times the allocation), Heroku will restart your dyno.

这篇关于如何使用集群正确缩放heroku上的nodejs应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆