How to manage multiple threads in Node JS

In this post, I'm going to show you how to potentially triple the performance of your Node application by managing multiple threads. This is an essential tutorial in which the techniques and examples shown will give you everything you need to set up efficient flow control.





Child Processes, Clustering and Worker Threads

Node , , , Worker Threads.





0.10. , .





, 4, . PM2.





, , , :





1. /





Node , , libuv. -, , TCP/UDP, , libuv, , libuv.





2. Child Processes ( )/Worker Threads JavaScript.





Worker Threads JavaScript, , , .. - Worker Threads, .





3. .





, , . , , , .





, Node. , , .





Worker Pool

, , Worker Pool. , Worker Pool , . , Node JS, Worker Threads, Web Workers .





Worker Pool , :





  • Worker Pool





Worker Pool - npm install workerpool





  • Worker Pool





Worker Pool .





  • Middleware Layer





Middleware Layer ( ) JavaScript- Worker Pool, .









, , Worker Pool.





Worker Pool

2 : NodeJS ( workerpool bcryptjs), GitHub NodeJS.





, 06-multithreading. npm install. 06-multithreading, .





worker-pool 2 : - Worker Pool (controller.js). () , ... , (thread-functions.js).





worker-pool/controller.js

'use strict'

const WorkerPool = require('workerpool')
const Path = require('path')

let poolProxy = null

// FUNCTIONS
const init = async (options) => {
  const pool = WorkerPool.pool(Path.join(__dirname, './thread-functions.js'), options)
  poolProxy = await pool.proxy()
  console.log(`Worker Threads Enabled - Min Workers: ${pool.minWorkers} - Max Workers: ${pool.maxWorkers} - Worker Type: ${pool.workerType}`)
}

const get = () => {
  return poolProxy
}

// EXPORTS
exports.init = init
exports.get = get
      
      



controller.js workerpool



. 2 , init



get



. init



. Worker Pool , , thread-functions.js



. , , . get



.





worker-pool/thread-functions.js

'use strict'

const WorkerPool = require('workerpool')
const Utilities = require('../2-utilities')

// MIDDLEWARE FUNCTIONS
const bcryptHash = (password) => {
  return Utilities.bcryptHash(password)
}

// CREATE WORKERS
WorkerPool.worker({
  bcryptHash
})
      
      



thread-functions.js



-, Worker Pool. BcryptJS



. 10 , , . utilities.js



, . , , bcryptHash



workerpool



. , , .





2-utilities.js

'use strict'

const BCrypt = require('bcryptjs')

const bcryptHash = async (password) => {
  return await BCrypt.hash(password, 8)
}

exports.bcryptHash = bcryptHash
      
      



.env

NODE_ENV="production"
PORT=6000
WORKER_POOL_ENABLED="1"
      
      



.env



NODE_ENV



"production". , Worker Pool, WORKER_POOL_ENABLED



"1" "0".





1-app.js

'use strict'

require('dotenv').config()

const Express = require('express')
const App = Express()
const HTTP = require('http')
const Utilities = require('./2-utilities')
const WorkerCon = require('./worker-pool/controller')

// Router Setup
App.get('/bcrypt', async (req, res) => {
  const password = 'This is a long password'
  let result = null
  let workerPool = null

  if (process.env.WORKER_POOL_ENABLED === '1') {
    workerPool = WorkerCon.get()
    result = await workerPool.bcryptHash(password)
  } else {
    result = await Utilities.bcryptHash(password)
  }

  res.send(result)
})

// Server Setup
const port = process.env.PORT
const server = HTTP.createServer(App)

;(async () => {
  // Init Worker Pool
  if (process.env.WORKER_POOL_ENABLED === '1') {
    const options = { minWorkers: 'max' }
    await WorkerCon.init(options)
  }

  // Start Server
  server.listen(port, () => {
    console.log('NodeJS Performance Optimizations listening on: ', port)
  })
})()
      
      



, 1-app.js



, . .env



. Express /bcrypt



. , Worker Pool. , Worker Pool bcryptHash



, thread-functions.js



. , , bcryptHash



Utilities



. Worker Pool , bcryptHash



Utilities



.





1-app.js



, . async/await, Worker Pool. Worker Pool, . , - minWorkers



"max". , Worker Pool , , 1 , . 6 , , 12 . minWorkers



"max", Worker Pool 11 . , - 6000.





Worker Pool

Worker Pool : get



http://localhost:6000/bcrypt



. , AutoCannon, / Worker Pool. AutoCannon .





, Node. Node.






"Node.js Developer". , .








All Articles