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.

Webconf nodejs-production-architecture


Published on

Published in: Technology
  • Be the first to comment

Webconf nodejs-production-architecture

  1. 1. node.jsproduction site architecture
  2. 2. About me Ben LinA full time entrepreneur & JavaScript loverFall in love with node.js on Jun 2011
  3. 3. About meCOKE - Full stack node.js MVC frameworkVodka - Functional testing frameworkThunder - The lightning fast template engine
  4. 4. Agenda 1. Super brief intro to node.js 2. Run node as a service 3. What if it crashes? 4. Monit VS Forever 5. Nginx or not 6. Scaling node.js 7. Deployment 8. Cloud service 9. Some useful tips 10. Q & A
  5. 5. What is node? node is an evented, non-blocking I/O server-side Javascript built on chrome V8
  6. 6. What is node?Ryan Dahl talks about the history ofnode.js
  7. 7. What is node? But why node? * node is fast * node handles a lot of connection with very little resources * You can develop your whole app with one language * Easy to scale
  8. 8. What is node? Achieves this by all making network I/O nonblocking and all file I/O asynchronous.
  9. 9. What is node? I/O latency L1: 3 cycles L2: 14 cycles RAM: 250 cycles DISK: 41,000,000 cycles NETWORK: 240,000,000 cycles
  10. 10. What is node? What’s wrong with PHP, Ruby, Python, etc...?
  11. 11. Run node as a service Run your node app as daemon with upstart
  12. 12. Run node as a service # Start your app start your_app_name # Stop your app stop your_app_name # Restart your app restart your_app_name
  13. 13. What if it crashes? Every little shit can fuck up your node app
  14. 14. Monitor your node app Installation on Ubuntu $ sudo apt-get install monit Edit configs * edit /etc/default/monit and set the "startup" variable to 1 * edit /etc/monit/monitrc and use the example file monitrc Run $ sudo /etc/init.d/monit start $ sudo monit start your_app_name
  15. 15. Monit VS Forever
  16. 16. Monit VS Forever With `forever` we kind of use an unstable tool to monitor another unstable project?!
  17. 17. Nginx or not?* Nginx runs faster with serving static files* Use it as a reverse proxy to load balance withmultiple nodes
  18. 18. Scaling node.js
  19. 19. Scaling node.jsStart small with built-in static server, node.js app & database all on the same server.
  20. 20. Scaling node.jsUse nginx as static server for better performance.
  21. 21. Scaling node.jsUse nginx as as proxy server as well to load balance requests.The number of node.js app instance depends on how many CPU cores on the machine.
  22. 22. Scaling node.jsSplit static files to different server for easier maintenance.
  23. 23. Scaling node.jsUse aws S3 for easier setup and maintenance.
  24. 24. Scaling node.jsSplit database to another server. Make the node.js app server an unit.
  25. 25. Scaling node.jsAdd a load balancer, add more app unit as the site scales up.
  26. 26. Scaling node.jsAdd replica set if the database hits its limit.
  27. 27. Scaling node.jsAdd CDN for static files for cross reign performance.
  28. 28. Scaling node.jsSplit app to difference services as it scales up. Previous scaling steps apply to those services too.
  29. 29. Deployment
  30. 30. DeploymentWith ssh, git; The server must stop during deployment.
  31. 31. DeploymentWith ssh, git; 0 downtime deployment is possible since we have more than 1 instance( repo ).
  32. 32. DeploymentWith ssh, git; Split static file makes it easier to deploy with multi instance( repo ) app.
  33. 33. DeploymentDeploying with multi machine it’s better to use image files on aws ec2.
  34. 34. Cloud service
  35. 35. Cloud service How about using services like Heroku, Nodejitsu, mongoHQ?
  36. 36. Some useful tips
  37. 37. Some useful tips Set your max file limit sudo vi /etc/security/limits.conf # add the following 4 lines root soft nofile 51200 root hard nofile 51200 * soft nofile 51200 * hard nofile 51200 sudo vi /etc/pam.d/common-session # add session required sudo vi /etc/profile # add ulimit -SHn 51200
  38. 38. Some useful tipsThis ensure that your node app never dies butuse with care process.on(uncaughtException, function (e) { console.dir(e); });
  39. 39. THE END Thanks
  40. 40. QUESTIONS?
  41. 41. HIRING Web & iOS DevYour Github account speak for you, Please mail to