node.js in production

36
Node.js In Production 20.01.2011

Upload: felix-geisendoerfer

Post on 15-Jan-2015

22.334 views

Category:

Technology


1 download

DESCRIPTION

Talk given at the BerlinJS on Jan 20, 2011.

TRANSCRIPT

Page 1: Node.js in production

Node.jsIn Production

20.01.2011

Page 2: Node.js in production

About

Contributor Co-founder

node.js driver node-formidableFelix Geisendörfer

- Joined the mailing list in June 26, 2009- Co-Transloadit- When I joined there where #24 people- First patch in September 2009- Now mailing list has > 3400 members

Page 3: Node.js in production

File uploading & processing as a service for other web applications.

- The app we run in production- Not a “typical” app, no html rendering, only JSON APIs

Page 4: Node.js in production

Are you running node in production?

Page 5: Node.js in production

Our imagination

Page 6: Node.js in production

Our first attempt

Page 7: Node.js in production

Transloadit v1

• Complete failure

• Node randomly crashed with:

(evcom) recv() Success

• A mess of code, hard to maintain

~1 Year ago

* http://transloadit.com/blog/2010/04/to-launch-or-not

Page 8: Node.js in production

Today

Page 9: Node.js in production

Well, almost : )

Page 10: Node.js in production

Transloadit v2

• Processed > 2 TB of data

• Not seen a node bug in production yet

• Clean code base, 100% test coverage

Now

Page 11: Node.js in production

How to get there?

Page 12: Node.js in production

Hosting

Page 13: Node.js in production

Managed / Heroku-style

• Joyent (no.de)

• Nodejitsu (nodejitsu.com)

• Duostack (duostack.com)

• Nodefu (nodefu.com)

• Heroku (heroku.com)

Page 14: Node.js in production

Self-managed

• Amazon Ec2

• Linode

• Rackspace Cloud Servers

• etc.

Page 15: Node.js in production

Deployment

Page 16: Node.js in production

Deployment

• Heroku service

• Custom deploy script

Page 17: Node.js in production

Custom “deploy script”

$ git clone <my-project> /var/www/<my-project>$ nohup bin/server.js &

Enough to get started

Page 18: Node.js in production

Staying alive

Page 19: Node.js in production

The problem

SyntaxError: Unexpected token ILLEGAL at Object.parse (native) at Server.<anonymous> (/path/to/my-script.js:4:8) at Server.emit (events.js:45:17) at HTTPParser.onIncoming (http.js:862:12) at HTTPParser.onHeadersComplete (http.js:85:31) at Socket.ondata (http.js:787:22) at Socket._onReadable (net.js:623:27) at IOWatcher.onReadable [as callback] (net.js:156:10)

Your node process is killed by any runtime error

Page 20: Node.js in production

The wrong solution

process.on('uncaughtException', function(err) { console.log('Something bad happened: '+err); // continue on ...});

Continuing after an exception will leave your system in unpredictable state

Page 21: Node.js in production

The right solution

process.on('uncaughtException', function(err) { console.log('Something bad happened: '+err); // Every process has to die one day, it’s life process.exit(0);});

Exiting the program will prevent corrupted state

Page 22: Node.js in production

Even better

var Hoptoad = require('hoptoad-notifier').Hoptoad;Hoptoad.key = 'YOUR_API_KEY';

process.on('uncaughtException', function(err) { console.log('Something bad happened: '+err); Hoptoad.notify(err, function() { process.exit(0); });});

Send your exception to a logging service.

Page 23: Node.js in production

Your process is dead.

Now what?

Page 24: Node.js in production

Use a process monitor

• Monit *

• Upstart

• God (ruby)

• forever (node.js)

* http://kevin.vanzonneveld.net/techblog/article/run_nodejs_as_a_service_on_ubuntu_karmic/

Page 25: Node.js in production

Debugging

Page 26: Node.js in production

Debugging tools

• console.log

• node-inspector (use webkit inspector.)

• node debug <script.js> (new in 0.3.x)

Page 27: Node.js in production

Unit tests(Real ones, not integration tests)

Page 28: Node.js in production

Load balancing

Page 29: Node.js in production

Load Balancing

• Haproxy

• Amazon Load Balancer

• Nginx (supports only http 1.0)

Page 30: Node.js in production

Our stack

Page 31: Node.js in production

Our stack

:80 :443

haproxy stunnel

node nginx

php

:80 :443

haproxy stunnel

node nginx

php

Ec2 Load Balancer

:80 :443

haproxy stunnel

node nginx

php

Page 32: Node.js in production

Workers

• Use workers a la Unicorn

• All workers listen on a shared socket

• Master process starts & kills workers

Page 33: Node.js in production

Workers

• spark

• fugue

• custom

Page 34: Node.js in production

master.jsvar netBinding = process.binding('net');var spawn = require('child_process').spawn;var net = require('net');

var serverSocket = netBinding.socket('tcp4');netBinding.bind(serverSocket, 8080);netBinding.listen(serverSocket, 128);

for (var i = 0; i < 10; i++) { var fds = netBinding.socketpair(); var child = spawn(process.argv[0], [__dirname+'/worker.js'], { customFds: [fds[0], 1, 2] }); var socket = new net.Stream(fds[1], 'unix'); socket.write('hi', 'utf8', serverSocket);}

Page 35: Node.js in production

worker.jsvar net = require('net');var http = require('http');var socket = new net.Socket(0, 'unix');

socket .on('fd', function(fd) { startServer(fd); }) .resume();

function startServer(fd) { http.createServer(function(req, res) { res.writeHead(200); res.end('Hello from: '+process.pid+'\n'); }).listenFD(fd); console.log('Worker ready: '+process.pid);}