12

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)

1 Answer 1

7

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.

Sign up to request clarification or add additional context in comments.

5 Comments

you mean that if I have a very memory efficient app, I may as well be able te have more than 4 clusters per dyno? Or I still have the number of CPUs as a constrain (because there is no mention of amount of CPUs available in the new document)?
It does depend on the type of app you are writing. It's true that if you are writing a memory-efficient but CPU intensive app then you might need to use a different technique. But I'm guessing that most (serious) node apps that are taking advantage of clusters will be limited by the RAM constraints on a Heroku dyno instead of the CPU constraints.
but the point is if heroku now allows me to fork more than 4 clusters in a regular dyno if I do not use too much memory, do you know if I can di that?
You've always been able to fork more than 4 workers. Heroku used to recommend you fork as many as you have CPU cores, but now they recommend to go by memory usage instead.
I'd be keen to understand why this has been downvoted - please let me know and I can update my answer accordingly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.