Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Node.js cluster

232 views

Published on

An overview of how Node.js cluster module works, what you can do with it and what you should take care of when using it.

Published in: Technology
  • Be the first to comment

Node.js cluster

  1. 1. Globalcode – Open4education Node.js Cluster Derek Stavis Software Engineer
  2. 2. Globalcode – Open4education Who? Derek Stavis github.com/derekstavis Software Engineer @ Pagar.me Ruby, JavaScript, Python, Go, C Node, React & Webpack Advocate
  3. 3. Globalcode – Open4education Node.js Runtime
  4. 4. Globalcode – Open4education single threaded non-blocking asynchronous concurrent engine
  5. 5. Globalcode – Open4education Heap Memory Available Task Queue listOnTimeout listOnTimeout listOnTimeout listOnTimeout listOnTimeout Delayed Tasks Call Stack write() saveFile() updateTime() tryOnTimeout listOnTimeout Execution Contexts Vendor APIs (Node, Web) Runtime and Platform Abstraction Event Loop Task Scheduler
  6. 6. Globalcode – Open4education Single threaded
  7. 7. Globalcode – Open4education Isn’t concurrent sufficient?
  8. 8. Globalcode – Open4education We have multiple processors for a while
  9. 9. Globalcode – Open4education We want to distribute the load
  10. 10. Globalcode – Open4education Enter cluster module
  11. 11. Globalcode – Open4education const cluster = require('cluster'); const http = require('http'); const numCPUs = require('os').cpus().length How to
  12. 12. Globalcode – Open4education if (cluster.isMaster) { console.log(`Master ${process.pid} is running`); // Fork workers. for (let i = 0; i < numCPUs; i++) { cluster.fork(); } cluster.on('exit', (worker, code, signal) => { console.log(`worker ${worker.process.pid} died`); }); } How to
  13. 13. Globalcode – Open4education else { // Workers can share any TCP connection // In this case it is an HTTP server http.createServer((req, res) => { res.writeHead(200); res.end('hello worldn'); }).listen(8000); console.log(`Worker ${process.pid} started`); } How to
  14. 14. Globalcode – Open4education
  15. 15. Globalcode – Open4education What?
  16. 16. Globalcode – Open4education Two processes binding on the same port?
  17. 17. Globalcode – Open4education cluster is just a wrapper for child_process.fork
  18. 18. Globalcode – Open4education Socket.listen is aware of cluster module
  19. 19. Globalcode – Open4education When a “slave” process uses a Socket
  20. 20. Globalcode – Open4education The underlying implementation listens on a file descriptor
  21. 21. Globalcode – Open4education That is fed by the master process
  22. 22. Globalcode – Open4education Master process acts like a load balancer
  23. 23. Globalcode – Open4education
  24. 24. Globalcode – Open4education Perk: You can do clustering with anything that is socket based
  25. 25. Globalcode – Open4education Here comes the Jabá
  26. 26. Globalcode – Open4education TeleMMO is a RPG game into a Telegram bot
  27. 27. Globalcode – Open4education We needed to make our game multicore
  28. 28. Globalcode – Open4education Telegram API can use two modes: Polling and Webhook
  29. 29. Globalcode – Open4education Webhooks are based on a HTTP server
  30. 30. Globalcode – Open4education We use a third-party module to integrate with Telegram
  31. 31. Globalcode – Open4education Cluster came in handy
  32. 32. Globalcode – Open4education We just followed the standard path (click me)
  33. 33. Globalcode – Open4education
  34. 34. Globalcode – Open4education What more?
  35. 35. Globalcode – Open4education You can do nice things
  36. 36. Globalcode – Open4education Zero downtime code reload
  37. 37. Globalcode – Open4education Process management
  38. 38. Globalcode – Open4education Automatic software update
  39. 39. Globalcode – Open4education var recluster = require('recluster'), path = require('path'); var cluster = recluster(path.join(__dirname, 'server.js')); cluster.run(); process.on('SIGUSR2', function() { console.log('Got SIGUSR2, reloading cluster...'); cluster.reload(); }); console.log("spawned cluster, kill -s SIGUSR2", process.pid, "to reload"); https://github.com/doxout/recluster
  40. 40. Globalcode – Open4education Is it all flowers?
  41. 41. Globalcode – Open4education NOPE
  42. 42. Globalcode – Open4education Processes are heavy
  43. 43. Globalcode – Open4education They are subject to operating system scheduling
  44. 44. Globalcode – Open4education Context switches are very expensive
  45. 45. Globalcode – Open4education Memory usage is high
  46. 46. Globalcode – Open4education Each worker reloads the entire application into memory
  47. 47. Globalcode – Open4education Kinda hacky, huh?
  48. 48. Globalcode – Open4education So, use it wisely
  49. 49. Globalcode – Open4education Don’t do more processes than the number of CPUs
  50. 50. Globalcode – Open4education Don’t use it to fix your bad code performance
  51. 51. Globalcode – Open4education
  52. 52. Globalcode – Open4education Thanks for watching Questions? github.com/derekstavis twitter.com/derekstavis facebook.com/derekstavis

×