Download - Webconf nodejs-production-architecture
![Page 1: Webconf nodejs-production-architecture](https://reader033.vdocuments.net/reader033/viewer/2022042518/5404c8fe8d7f72a6768b4870/html5/thumbnails/1.jpg)
node.jsproduction site architecture
![Page 2: Webconf nodejs-production-architecture](https://reader033.vdocuments.net/reader033/viewer/2022042518/5404c8fe8d7f72a6768b4870/html5/thumbnails/2.jpg)
Ben Lin
A full time entrepreneur & JavaScript lover
Fall in love with node.js on Jun 2011
twitter.com/dreamerslabgithub.com/dreamerslab
About me
![Page 3: Webconf nodejs-production-architecture](https://reader033.vdocuments.net/reader033/viewer/2022042518/5404c8fe8d7f72a6768b4870/html5/thumbnails/3.jpg)
About me
COKE - Full stack node.js MVC frameworkVodka - Functional testing frameworkThunder - The lightning fast template engine
https://github.com/dreamerslab
![Page 4: Webconf nodejs-production-architecture](https://reader033.vdocuments.net/reader033/viewer/2022042518/5404c8fe8d7f72a6768b4870/html5/thumbnails/4.jpg)
![Page 5: Webconf nodejs-production-architecture](https://reader033.vdocuments.net/reader033/viewer/2022042518/5404c8fe8d7f72a6768b4870/html5/thumbnails/5.jpg)
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
![Page 6: Webconf nodejs-production-architecture](https://reader033.vdocuments.net/reader033/viewer/2022042518/5404c8fe8d7f72a6768b4870/html5/thumbnails/6.jpg)
What is node?
node is an evented, non-blocking I/O server-side Javascript built on chrome V8
![Page 7: Webconf nodejs-production-architecture](https://reader033.vdocuments.net/reader033/viewer/2022042518/5404c8fe8d7f72a6768b4870/html5/thumbnails/7.jpg)
What is node?
http://www.youtube.com/watch?v=SAc0vQCC6UQ
Ryan Dahl talks about the history of node.js
![Page 8: Webconf nodejs-production-architecture](https://reader033.vdocuments.net/reader033/viewer/2022042518/5404c8fe8d7f72a6768b4870/html5/thumbnails/8.jpg)
What is 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
But why node?
![Page 9: Webconf nodejs-production-architecture](https://reader033.vdocuments.net/reader033/viewer/2022042518/5404c8fe8d7f72a6768b4870/html5/thumbnails/9.jpg)
What is node?
Achieves this by all making network I/O nonblocking and all file I/O asynchronous.
![Page 10: Webconf nodejs-production-architecture](https://reader033.vdocuments.net/reader033/viewer/2022042518/5404c8fe8d7f72a6768b4870/html5/thumbnails/10.jpg)
What is node?
L1: 3 cyclesL2: 14 cyclesRAM: 250 cyclesDISK: 41,000,000 cycles NETWORK: 240,000,000 cycles
I/O latency
![Page 11: Webconf nodejs-production-architecture](https://reader033.vdocuments.net/reader033/viewer/2022042518/5404c8fe8d7f72a6768b4870/html5/thumbnails/11.jpg)
What is node?
What’s wrong with PHP, Ruby, Python, etc...?
![Page 12: Webconf nodejs-production-architecture](https://reader033.vdocuments.net/reader033/viewer/2022042518/5404c8fe8d7f72a6768b4870/html5/thumbnails/12.jpg)
Run node as a service
Run your node app as daemon with upstart
![Page 13: Webconf nodejs-production-architecture](https://reader033.vdocuments.net/reader033/viewer/2022042518/5404c8fe8d7f72a6768b4870/html5/thumbnails/13.jpg)
Run node as a service
# Start your appstart your_app_name
# Stop your appstop your_app_name
# Restart your apprestart your_app_name
![Page 14: Webconf nodejs-production-architecture](https://reader033.vdocuments.net/reader033/viewer/2022042518/5404c8fe8d7f72a6768b4870/html5/thumbnails/14.jpg)
What if it crashes?
Every little shit can fuck up your node app
![Page 15: Webconf nodejs-production-architecture](https://reader033.vdocuments.net/reader033/viewer/2022042518/5404c8fe8d7f72a6768b4870/html5/thumbnails/15.jpg)
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
![Page 16: Webconf nodejs-production-architecture](https://reader033.vdocuments.net/reader033/viewer/2022042518/5404c8fe8d7f72a6768b4870/html5/thumbnails/16.jpg)
Monit VS Forever
![Page 17: Webconf nodejs-production-architecture](https://reader033.vdocuments.net/reader033/viewer/2022042518/5404c8fe8d7f72a6768b4870/html5/thumbnails/17.jpg)
Monit VS Forever
With `forever` we kind of use an unstable tool to monitor another unstable project?!
![Page 18: Webconf nodejs-production-architecture](https://reader033.vdocuments.net/reader033/viewer/2022042518/5404c8fe8d7f72a6768b4870/html5/thumbnails/18.jpg)
Nginx or not?
* Nginx runs faster with serving static files* Use it as a reverse proxy to load balance with multiple nodes
![Page 19: Webconf nodejs-production-architecture](https://reader033.vdocuments.net/reader033/viewer/2022042518/5404c8fe8d7f72a6768b4870/html5/thumbnails/19.jpg)
Scaling node.js
![Page 20: Webconf nodejs-production-architecture](https://reader033.vdocuments.net/reader033/viewer/2022042518/5404c8fe8d7f72a6768b4870/html5/thumbnails/20.jpg)
Start small with built-in static server, node.js app & database all on the same server.
Scaling node.js
![Page 21: Webconf nodejs-production-architecture](https://reader033.vdocuments.net/reader033/viewer/2022042518/5404c8fe8d7f72a6768b4870/html5/thumbnails/21.jpg)
Use nginx as static server for better performance.
Scaling node.js
![Page 22: Webconf nodejs-production-architecture](https://reader033.vdocuments.net/reader033/viewer/2022042518/5404c8fe8d7f72a6768b4870/html5/thumbnails/22.jpg)
Use 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.
Scaling node.js
![Page 23: Webconf nodejs-production-architecture](https://reader033.vdocuments.net/reader033/viewer/2022042518/5404c8fe8d7f72a6768b4870/html5/thumbnails/23.jpg)
Split static files to different server for easier maintenance.
Scaling node.js
![Page 24: Webconf nodejs-production-architecture](https://reader033.vdocuments.net/reader033/viewer/2022042518/5404c8fe8d7f72a6768b4870/html5/thumbnails/24.jpg)
Use aws S3 for easier setup and maintenance.
Scaling node.js
![Page 25: Webconf nodejs-production-architecture](https://reader033.vdocuments.net/reader033/viewer/2022042518/5404c8fe8d7f72a6768b4870/html5/thumbnails/25.jpg)
Split database to another server. Make the node.js app server an unit.
Scaling node.js
![Page 26: Webconf nodejs-production-architecture](https://reader033.vdocuments.net/reader033/viewer/2022042518/5404c8fe8d7f72a6768b4870/html5/thumbnails/26.jpg)
Add a load balancer, add more app unit as the site scales up.
Scaling node.js
![Page 27: Webconf nodejs-production-architecture](https://reader033.vdocuments.net/reader033/viewer/2022042518/5404c8fe8d7f72a6768b4870/html5/thumbnails/27.jpg)
Add replica set if the database hits its limit.
Scaling node.js
![Page 28: Webconf nodejs-production-architecture](https://reader033.vdocuments.net/reader033/viewer/2022042518/5404c8fe8d7f72a6768b4870/html5/thumbnails/28.jpg)
Add CDN for static files for cross reign performance.
Scaling node.js
![Page 29: Webconf nodejs-production-architecture](https://reader033.vdocuments.net/reader033/viewer/2022042518/5404c8fe8d7f72a6768b4870/html5/thumbnails/29.jpg)
Split app to difference services as it scales up. Previous scaling steps apply to those services too.
Scaling node.js
![Page 30: Webconf nodejs-production-architecture](https://reader033.vdocuments.net/reader033/viewer/2022042518/5404c8fe8d7f72a6768b4870/html5/thumbnails/30.jpg)
Deployment
![Page 31: Webconf nodejs-production-architecture](https://reader033.vdocuments.net/reader033/viewer/2022042518/5404c8fe8d7f72a6768b4870/html5/thumbnails/31.jpg)
With ssh, git; The server must stop during deployment.
Deployment
![Page 32: Webconf nodejs-production-architecture](https://reader033.vdocuments.net/reader033/viewer/2022042518/5404c8fe8d7f72a6768b4870/html5/thumbnails/32.jpg)
With ssh, git; 0 downtime deployment is possible since we have more than 1 instance( repo ).
Deployment
![Page 33: Webconf nodejs-production-architecture](https://reader033.vdocuments.net/reader033/viewer/2022042518/5404c8fe8d7f72a6768b4870/html5/thumbnails/33.jpg)
With ssh, git; Split static file makes it easier to deploy with multi instance( repo ) app.
Deployment
![Page 34: Webconf nodejs-production-architecture](https://reader033.vdocuments.net/reader033/viewer/2022042518/5404c8fe8d7f72a6768b4870/html5/thumbnails/34.jpg)
Deploying with multi machine it’s better to use image files on aws ec2.
Deployment
![Page 35: Webconf nodejs-production-architecture](https://reader033.vdocuments.net/reader033/viewer/2022042518/5404c8fe8d7f72a6768b4870/html5/thumbnails/35.jpg)
Cloud service
![Page 36: Webconf nodejs-production-architecture](https://reader033.vdocuments.net/reader033/viewer/2022042518/5404c8fe8d7f72a6768b4870/html5/thumbnails/36.jpg)
Cloud service
How about using services like Heroku, Nodejitsu, mongoHQ?
![Page 37: Webconf nodejs-production-architecture](https://reader033.vdocuments.net/reader033/viewer/2022042518/5404c8fe8d7f72a6768b4870/html5/thumbnails/37.jpg)
Some useful tips
![Page 38: Webconf nodejs-production-architecture](https://reader033.vdocuments.net/reader033/viewer/2022042518/5404c8fe8d7f72a6768b4870/html5/thumbnails/38.jpg)
Some useful tips
Set your max file limit
sudo vi /etc/security/limits.conf# add the following 4 linesroot soft nofile 51200root hard nofile 51200* soft nofile 51200* hard nofile 51200
sudo vi /etc/pam.d/common-session# addsession required pam_limits.so
sudo vi /etc/profile# addulimit -SHn 51200
![Page 39: Webconf nodejs-production-architecture](https://reader033.vdocuments.net/reader033/viewer/2022042518/5404c8fe8d7f72a6768b4870/html5/thumbnails/39.jpg)
Some useful tips
This ensure that your node app never dies but use with care
process.on('uncaughtException', function (e) { console.dir(e);});
![Page 40: Webconf nodejs-production-architecture](https://reader033.vdocuments.net/reader033/viewer/2022042518/5404c8fe8d7f72a6768b4870/html5/thumbnails/40.jpg)
THE ENDThanks
![Page 41: Webconf nodejs-production-architecture](https://reader033.vdocuments.net/reader033/viewer/2022042518/5404c8fe8d7f72a6768b4870/html5/thumbnails/41.jpg)
QUESTIONS?