search

Home  >  Q&A  >  body text

node.js - 使用NodeJS做云端转码服务器,请问该怎么最大化使用云主机的资源?

我们的产品目前有一个需求是:用户上传某个文件,在服务器自动转格式,然后再让用户下载。
为了实现这个过程,我们需要在服务器端通过NodeJS调用CMD来执行一些操作。这个CMD是封装好的,我们目前使用Node-cmd包来调用它。告诉它要转的文件在哪儿,转完存到哪儿,然后就开始运行cmd,运行结束后,我们拿到成功或是失败的结果。

我们后端的设计思路是:用一个数据库文件来记录所有task的状态,比如刚传上来的文件处在Pending状态,正在转码中的叫Processing,转码结束的是Success或是Fail,然后基于这个数据库,我们就可以让Node很好的安排工作。比如让Node在某种情况下循环读取这个数据库的内容,如果发现Pending中的,就去处理它。如果发现Processing的,就告知前端用户还要等几个。

程序设计这里,我们都还比较自信。

但是考虑到大并发的情况,比如N多人同时提交转码要求。我们到底该怎么最大化地使用一台云主机(目前选用阿里云ECS)的资源?我们倾向于选择单一高性能主机,而不是买一堆小主机,使用负载均衡。一来是价格更合适,二来是管理起来单一主机也更方便。

比如我们选择了一台4核4G的主机,请问该怎么让NodeJS我们这种需求下,充分使用主机资源,降低单一用户的等待时间?

希望大牛能帮忙一下,多谢!

PS:我们团队都是用JS做开发的,因此必须使用Node做服务器后台。

阿神阿神2874 days ago417

reply all(2)I'll reply

  • PHP中文网

    PHP中文网2017-04-17 16:20:03

    If your node-CMD is really synchronized, it is recommended to change or replace it. The main server process should not block for worker processes. node provides asynchronous subprocess API (child_process.exec).

    I guess the bottleneck of transcoding is the CPU, maybe you can just use load to judge. For example, a new transcoding task will only be started when the average load in the last minute is lower than a certain value.

    You might as well try a few more depending on your resources and tasks.

    reply
    0
  • 黄舟

    黄舟2017-04-17 16:20:03

    According to your description, you don’t have to think about it. The current design will basically not cause the situation of “there are still CPU/memory resources that I can’t use up”.

    I don’t know if the part of the code packaged by your node-cmd consumes a lot of CPU. If it is synchronous code, it cannot be run in the main process of node. If it is asynchronous, the basic problem is not big.

    reply
    0
  • Cancelreply