如何正确地使用集群扩展heroku上的nodejs应用程序

时间:2021-12-30 20:12:31

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

我注意到最近heroku日志中有一条警告,对我来说似乎是新的

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

网络。1:检测到512mb可用内存,每个进程512mb的限制(WEB_MEMORY)

web.1: Recommending WEB_CONCURRENCY=1

网络。1:推荐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)

我做了一些研究,发现了这个集群文章,这是在node . js中使用集群的“默认”方式,但是它完全与新更新的文章相矛盾,这篇文章包含了这个新的WEB_CONCURRENCY环境变量,并且对每个dyno(它比btw小得多)提出了不同的大小建议。

1 个解决方案

#1


6  

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).

第一个链接是从2014年7月开始的,过去是推荐的做事方式。然而,Heroku的dynos非常以内存为中心,当使用每个CPU的最大内核数时,很容易超过分配的内存占用(正如第一篇文章所建议的)。

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:

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

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.

通过使用WEB_MEMORY变量,Heroku可以根据正在运行的dyno的大小生成WEB_CONCURRENCY值,因此可以提供正确的进程数量,以确保应用程序不会超过内存限制。

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.

顺便提一下,如果您确实超过了内存分配(1x dyno为512MB),那么将使用交换空间来分配多余的内存。这会减慢你的应用程序,导致请求时间增加,通常会导致缓慢。如果超过了内存使用量(大约是分配的三倍),Heroku将重新启动dyno。

#1


6  

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).

第一个链接是从2014年7月开始的,过去是推荐的做事方式。然而,Heroku的dynos非常以内存为中心,当使用每个CPU的最大内核数时,很容易超过分配的内存占用(正如第一篇文章所建议的)。

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:

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

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.

通过使用WEB_MEMORY变量,Heroku可以根据正在运行的dyno的大小生成WEB_CONCURRENCY值,因此可以提供正确的进程数量,以确保应用程序不会超过内存限制。

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.

顺便提一下,如果您确实超过了内存分配(1x dyno为512MB),那么将使用交换空间来分配多余的内存。这会减慢你的应用程序,导致请求时间增加,通常会导致缓慢。如果超过了内存使用量(大约是分配的三倍),Heroku将重新启动dyno。