Scale

  • Scale-out is more cost efficient than scale-up
  • Persist with filesystems only read-only data like logging and backups

Scale-out with App Clusters

  • single-server form of scaling out
  • create on independent server for each core (CPU)
  • don’t create more than one cluster per core otherwise the extra servers will be useless

The Pros

  • Maximize server performance (hardware or virtual machine)
  • low overhead to test your app under parallel conditions
  1. // Web development with Node & Express pg. 134
  2. var cluster = require('cluster');
  3. function startWorker() {
  4. var worker = cluster.fork();
  5. console.log('CLUSTER: Worker %d started', worker.id);
  6. }
  7. if (cluster.isMaster) {
  8. require('os').cpus().forEach(() => {
  9. startWorker();
  10. });
  11. // log any workers that disconnect; if a worker disconnects, it
  12. // should then exit so we'll wait for the exit event to spawn
  13. // a new worker to replace it
  14. cluster.on('disconnect', (worker) => {
  15. console.log('CLUSTER: WOrker % disconnected from the cluster.', worker.id);
  16. });
  17. // when a worker dies (exits), create a worker to replace it
  18. cluster.on('exit', (worker, code, signal) {
  19. console.log('CLUSTER: Worker %d died with exit code %d (%s)', worker.id, code, signal);
  20. startWorker();
  21. });
  22. } else {
  23. // start our app on worker;
  24. require('./meadowlark.js')();
  25. }
  • when a script is run directly, require.main === module will be true.
  • if false, it means your script has been loaded from another script using require.
  • cluster
  • virtual machines default to a single core.

Scaling Out with Multiple Servers

  • To scale out you need a proxy server (often called a reverse proxy or forward-facing proxy.)
  • Most popular: Nginx and HAProxy
  • Dev proxies: proxy and node-http-proxy

Working with proxies

  • if you use a proxy server, make sure to tell Express to trust it with app.enable('trust proxy');
  • this will ensure that req.ip, req.protocol, and req.secure reflect details about the connection client <> proxy, and not client<>server
  • req.ips will be an array of ips with original client and the names or IP addresses of intermediate proxies