最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Is Next js Backend multithreaded by default? - Stack Overflow

programmeradmin1浏览0评论

Currently i'm starting my project with build-in Node js sever inside Next js , using next start mand , but I'm not sure that if it has multithreading enabled .

My question is , Do you remend using built-in Node backend or starting Next js project with a custom server and enable multithreading using node clusters ,for maximum performance and speed?

Currently i'm starting my project with build-in Node js sever inside Next js , using next start mand , but I'm not sure that if it has multithreading enabled .

My question is , Do you remend using built-in Node backend or starting Next js project with a custom server and enable multithreading using node clusters ,for maximum performance and speed?

Share Improve this question edited Jul 4, 2022 at 10:27 Moein Moeinnia asked Jul 3, 2022 at 18:31 Moein MoeinniaMoein Moeinnia 2,3914 gold badges14 silver badges27 bronze badges 5
  • 2 I suggest using node normally and not do multithreading at all unless you are doing CPU intensive stuff like mp3 encoding etc. Node's speed es from it being single-threaded allowing it to avoid expensive memory allocations and context switching. You have always been able to wait on multiple events in parallel in a single thread in any programming language. Node.js just makes it easy (it's the default). Node's ability to wait in parallel (not execute code in parallel) allows it to leverage the multithreading power of other... – slebetman Commented Jul 4, 2022 at 12:00
  • 2 ... services you connect to like databases, API servers, file servers etc. Only do multithreading when you really need it. Not when you just think you need to "speed up". In 99% of cases adding multithreading to Node.js gives you close to 0% increase in speed. – slebetman Commented Jul 4, 2022 at 12:01
  • with multithreading , I meant having multiple node processes with clustreing and having multiple node instances running alongside each other – Moein Moeinnia Commented Jul 4, 2022 at 12:46
  • 1 Yes. I've benchmarked both clustering and worker_threads in an app that was mostly doing CRUD operations (get data from MySQL, write data to MySQL) and sometimes got around 0% speed increase on average. If you are using PM2 it is very simple to cluster your server without modifying your code so you can benchmark it yourself (note: I also tested using the cluster module manually as well as worker_threads). Because of the 0% speed increase result I quickly shifted focus to instead optimize my MySQL settings and queries. – slebetman Commented Jul 5, 2022 at 1:04
  • 1 Remember, waiting for result from an SQL query or waiting to read files from disk uses exactly 0% CPU time (your code just register itself with your OS event handling mechanism and your OS just set up interrupt handlers - after that waiting for events does not involve any code execution) so 0% CPU time is exactly the same if you use one thread or if you are using 50 threads because 50 * 0 = 0. In a properly designed system like node.js or Python's Twisted framework waiting is basically free – slebetman Commented Jul 5, 2022 at 1:06
Add a ment  | 

2 Answers 2

Reset to default 2

Instead of multithreading and clustering you can run Next.js on multiple instances, for example containers, behind a load balancer. It gives you horizontal scaling.

Node is single threaded by default so as ‘next start’. I use pm2 cluster mode, and haven’t got any issues about it.

to start cluster you do following:

  1. create pm2.json file next to your package.json
{
    "apps": [
        {
            "name": "myNextApp",
            "script": "node_modules/next/dist/bin/next",
            "args": "start",
            "cwd": "./",
            "instances": "max",
            "exec_mode": "cluster",
            "out_file": "/var/log/myNextApp/myNextApp.log",
            "error_file": "/var/log/myNextApp/myNextAppError.log",
            "watch": "true"
        }
    ]
}

pls note the "script": "node_modules/next/dist/bin/next" is used if you are under Typescript in Next, this config is 6 month old maybe you gonna need to change this line, check the pm2 docs

  1. in package.json add:
{
   ...
   "scripts: {
      ...,
      "pm2": "pm2"
      },
   ...,
}
  1. add devDependencie pm2 (yarn add -D pm2)

  2. to start cluster you should be able to use:

npm run pm2 start pm2.json
or
yarn pm2 start pm2.json

from project root

p.s. I have some doubts on whether you need to add pm2 to your project deps, I think there is some differences when you install yarn/pm2 with npm i -G and apt-get that might result a lighter config, but I'm not sure...

发布评论

评论列表(0)

  1. 暂无评论