简体   繁体   English

为什么Heroku在Node.js中推荐Foreman?

[英]Why is Foreman Recommended by Heroku in Node.js?

This may be a silly question for more experienced Node and back-end guys, and let me hope there are no long debates here, but ... 对于经验丰富的Node和后端人员来说,这可能是一个愚蠢的问题,让我希望这里不再有争论,但是...

Why does Heroku recommend using procfile and launch a separate web and a separate worker process with foreman? 为什么Heroku建议使用procfile并与工头一起启动单独的Web和单独的工作进程?

Link: https://devcenter.heroku.com/articles/node-best-practices 链接: https//devcenter.heroku.com/articles/node-best-practices

Procfile: Procfile:

web: bin/web
worker: bin/worker

Here is my thinking and 3 reasons they would recommend it, but I'd like somebody to help me understand if I am on the right track... 这是我的想法以及他们会推荐它的3个原因,但是我希望有人能帮助我了解我是否处在正确的轨道上...

Let's imagine this scenario: 让我们想象一下这种情况:

1) A "web" app is for handling APIs 2) A "worker" app is for stuff that takes time or requires a retry on failure. 1)“ Web”应用程序用于处理API 2)“ worker”应用程序用于处理需要时间或需要重试失败的内容。 Ex. 防爆。 sending an e-mail. 发送电子邮件。 An API just doesn't want to wait long for an HTTP response, so yeah. API只是不想长时间等待HTTP响应,所以是的。 3) Then, there is a hypothetical "web + worker" app that does both. 3)然后,有一个假想的“网络+工作者”应用程序可以同时执行这两种操作。

A) Is having a separate web and worker apps better for performance? A)是否有单独的Web应用程序和辅助应用程序更好地提高了性能?

I don't see any performance benefits in having the app separated into aa separate web and worker apps. 将应用程序分离为单独的Web应用程序和辅助应用程序,看不到任何性能上的好处。 In a 2 CPU machine, if we have 2 web processes and 2 worker processes, due to single-threaded nature of Node.js, it will be just as performant as having 2 processes of a "combined app that does both web + worker job". 在2个CPU的计算机中,如果我们有2个Web进程和2个工作进程,由于Node.js的单线程性质,它的性能与具有2个同时执行Web +工作任务的组合应用程序的进程一样”。

B) Is separating for the sake of readability of the code? B)是否为了代码的可读性而分开?

Maybe, I can agree with this one. 也许我可以同意这一点。

We can have classes, functions, etc.. that can be only used in workers, and some that can be only used in the web part. 我们可以有只能在worker中使用的类,函数等,而有些只能在web部件中使用。

C) Is it for robustness? C)是否出于鲁棒性?

In theory, yes... Let's say for the sake of example, 1. If we have a 2 CPU machine, 2. We don't separate into web and worker, and the app's code does both "worker and web" work, 3. We spin 2 processes of that app with the help of clusters, 4. we schedule 2 very long and time-consuming tasks (CPU BOUND, without any I/O, just for the sake of example so it actually hangs a Node process), the whole app just hangs because two processes are busy handling these 2 tasks. 从理论上讲,是的...让我们举个例子:1.如果我们有2个CPU的计算机,2.我们不分离为web和worker,并且应用程序的代码同时执行“ worker和web”工作, 3.我们在群集的帮助下旋转了该应用程序的2个进程,4.我们安排了2个非常耗时的任务(CPU BOUND,没有任何I / O,仅出于示例目的,因此它实际上挂起了Node进程) ),整个应用只是挂起,因为两个进程正在忙于处理这两个任务。

However, in this case, I would create 2 web processes and just 1 worker process. 但是,在这种情况下,我将创建2个Web进程和1个工作进程。 That way if worker is super busy, the web on the remaining process can still take API requests and respond to clients. 这样,如果工作人员非常忙,则其余进程上的Web仍可以接收API请求并响应客户端。 I think so... 我认同...

Is my thinking correct? 我的想法正确吗? Why is Heroku recommending splitting Node.js code into worker and web apps? 为什么Heroku建议将Node.js代码拆分为工作程序和Web应用程序?

The zen of Heroku is The 12 Factor App . Heroku的禅宗是12要素应用程序 Heroku encourages separate process types for web and workers, that will run on separately scalable dynos. Heroku鼓励为Web和工作人员使用单独的进程类型,这些进程类型将在单独的可伸缩dynos上运行。 See https://12factor.net/concurrency . 请参阅https://12factor.net/concurrency

In addition, for node.js apps, Heroku makes recommendations and provides support for optimizing application concurrency using node.js clustering , to maximize performance of node.js apps running on multi-core dynos. 此外,对于node.js应用程序,Heroku提出了建议,并提供了使用node.js集群 优化应用程序并发性的支持,以最大化在多核dyno上运行的node.js应用程序的性能。 This enables you to squeeze maximum performance out of each dyno instance, before resorting to vertical scaling (ie increasing compute resources allocated to your dynos) or horizontal scaling (ie increasing the number of dyno instances per process type in your app). 这使您能够在诉诸垂直缩放(即增加分配给dyno的计算资源)或水平缩放(即增加应用程序中每个进程类型的dyno实例的数量)之前,从每个dyno实例中挤出最大性能。

Note that it is theoretically possible for you to run both Web and Worker processes in a single dyno, but it is simply not recommended. 请注意,从理论上讲,您可以在单个dyno中同时运行Web和Worker进程,但是完全不建议这样做。 In your procfile you could have some "main" process type that when run would spawn additional processes. 在procfile中,您可能有一些“主要”进程类型,运行时会产生其他进程。 However, then you will get into issues such as how to monitor whether your processes are running properly, and other issues. 但是,您将遇到一些问题,例如如何监视流程是否正常运行以及其他问题。 All of that is taken care of you automatically by Heroku, if you "play by the rules" (see eg Dyno crash restart policy ). 如果您“遵守规则”(请参阅​​例如Dyno崩溃重新启动策略 ),那么Heroku会自动照顾好所有这些。

The bottom line of this is: You might be able to reduce your dyno costs by running both Web and Worker processes in a single dyno, but that is really not what Heroku intends for you to do, and if you do so, you may encounter unexpected problems. 这样做的底线是:您可以通过在单个dyno中运行Web和Worker进程来减少dyno成本,但这实际上不是Heroku想要的,如果这样做,您可能会遇到意外的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM