84
node.js [email protected] May 5, 2010

Node.js

Embed Size (px)

DESCRIPTION

node.js is a set of bindings to the V8javascript VM.Allows one to script programs that doI/O in javascript.Focused on performance.

Citation preview

Page 1: Node.js

node.js

[email protected]

May 5, 2010

Page 2: Node.js

node.js is a set of bindings to the V8javascript VM.

Allows one to script programs that doI/O in javascript.

Focused on performance.

Page 3: Node.js

First a silly benchmark:

Page 4: Node.js

100 concurrent clients1 megabyte response

node 822 req/secnginx 708thin 85mongrel 4

(bigger is better)

Page 5: Node.js

The code:

1 http = require(’http’)2 Buffer = require(’buffer’).Buffer;3

4 n = 1024*1024;5 b = new Buffer(n);6 for (var i = 0; i<n; i++) b[i] = 100;7

8 http.createServer(function (req, res) {9 res.writeHead(200);

10 res.end(b);11 }).listen(8000);

Page 6: Node.js

Please take with a grain of salt. Veryspecial circumstances.

NGINX peaked at 4mb of memory

Node peaked at 60mb.

Page 7: Node.js

I/O needs to be done differently.

Page 8: Node.js

Many web applications have codelike this:

result = query(’select * from T’);// use result

What is the software doing while itqueries the database?

Page 9: Node.js

In many cases, just waiting for theresponse.

Page 10: Node.js

Modern Computer Latency

L1: 3 cycles

L2: 14 cycles

RAM: 250 cycles

—————————

DISK: 41,000,000 cycles

NETWORK: 240,000,000 cycles

Page 11: Node.js

“Non-blocking”

L1, L2, RAM

—————————

“Blocking”

DISK, NETWORK

Page 12: Node.js

result = query(’select * from T’);// use result

BLOCKS

Page 13: Node.js

Better software can multitask.

Other threads of execution can runwhile waiting.

Page 14: Node.js

Is that the best that can be done?

A look at Apache and NGINX.

Page 15: Node.js

NGINX

Apache

0 1000 2000 3000 40000

2000

4000

6000

8000

104

Concurrent Clients

Req/sec

http://blog.webfaction.com/a-little-holiday-present

Page 16: Node.js

NGINX

Apache

0 1000 2000 3000 40000

10

20

30

40

Concurrent Clients

Memory (MB)

http://blog.webfaction.com/a-little-holiday-present

Page 17: Node.js

Apache vs NGINX

The difference?

Apache uses one thread perconnection.

NGINX doesn’t use threads. It usesan event loop.

Page 18: Node.js

I Context switching is not freeI Execution stacks take up memory

For massive concurrency, cannotuse an OS thread for eachconnection.

Page 19: Node.js

Green threads or coroutines canimprove the situation dramatically

BUT there is still machinery involvedto create the illusion of holdingexecution on I/O.

Page 20: Node.js

Code like this

result = query(’select..’);// use result

either blocks the entire process orimplies multiple execution stacks.

Page 21: Node.js

But a line of code like this

query(’select..’, function (result) {// use result

});

allows the program to return to theevent loop immediately.

No machinery required.

Page 22: Node.js

query(’select..’, function (result) {// use result

});

This is how I/O should be done.

Page 23: Node.js

So why isn’t everyone using eventloops, callbacks, and non-blockingI/O?

For reasons both cultural andinfrastructural.

Page 24: Node.js

Cultural BiasWe’re taught I/O with this:

1 puts("Enter your name: ");2 var name = gets();3 puts("Name: " + name);

We’re taught to demand input and donothing until we have it.

Page 25: Node.js

Cultural Bias

Code like

1 puts("Enter your name: ");2 gets(function (name) {3 puts("Name: " + name);4 });

is rejected as too complicated.

Page 26: Node.js

Missing Infrastructure

So why isn’t everyone using eventloops?

Single threaded event loops requireI/O to be non-blocking

Most libraries are not.

Page 27: Node.js

Missing InfrastructureI POSIX async file I/O not available.

I Man pages don’t state if a function will access thedisk. (e.g getpwuid())

I No closures or anonymous functions in C; makescallbacks difficult.

I Database libraries (e.g. libmysql client) do notprovide support for asynchronous queries

I Asynchronous DNS resolution not standard on mostsystems.

Page 28: Node.js

Too Much InfrastructureEventMachine, Twisted, AnyEventprovide very good event loopplatforms.

Easy to create efficent servers.

But users are confused how tocombine with other availablelibraries.

Page 29: Node.js

Too Much Infrastructure

Users still require expert knowledgeof event loops, non-blocking I/O.

Page 30: Node.js

Javascript designed specifically to beused with an event loop:

I Anonymous functions, closures.I Only one callback at a time.I I/O through DOM event callbacks.

Page 31: Node.js

The culture of Javascript is alreadygeared towards eventedprogramming.

Page 32: Node.js

This is the node.js project:

To provide a purely evented,non-blocking infrastructure toscript highly concurrent programs.

Page 33: Node.js

Design Goals

Page 34: Node.js

Design Goals

No function should directly performI/O.

To receive info from disk, network, oranother process there must be acallback.

Page 35: Node.js

Design GoalsLow-level.

Stream everything; never force thebuffering of data.

Do not remove functionality presentat the POSIX layer. For example,support half-closed TCPconnections.

Page 36: Node.js

Design Goals

Have built-in support for the mostimportant protocols:

DNS, HTTP, TLS

(Especially because these are sodifficult to do yourself)

Page 37: Node.js

Design Goals

Support many HTTP features.

I Chunked encodingI Pipelined messagesI Hanging requests for comet

applications.

Page 38: Node.js

Design Goals

The API should be both familiar toclient-side JS programmers andold school UNIX hackers.

Be platform independent.

Page 39: Node.js

Design Goals

Simply licensed (Almost 100%MIT/BSD)

Few dependencies

Static linking

Page 40: Node.js

Design Goals

Make it enjoyable.

Page 41: Node.js

Architecture

Page 42: Node.js

V8

thread pool

(libeio)

event loop

(libev)

Node bindings

Node standard library

C

JavaScript

Page 43: Node.js

The JavaScript layer can access themain thread.

The C layer can use multiple threads.

Page 44: Node.js

Deficiency? No. Feature.

Threads should be used by expertsonly.

Page 45: Node.js

Jail users in non-blockingenvironment.

Don’t allow I/O trickery.

There is exactly one execution stackin Node.

Page 46: Node.js

Node execution stack

ev_loop()

Page 47: Node.js

Node execution stack

ev_loop()

socket_readable(1)

Page 48: Node.js

Node execution stack

ev_loop()

socket_readable(1)

http_parse(1)

Page 49: Node.js

Node execution stack

ev_loop()

socket_readable(1)

http_parse(1)

load("index.html")

Page 50: Node.js

Node sends a request to the threadpool to load "index.html".

The stack unwinds...

Page 51: Node.js

Node execution stack

ev_loop()

socket_readable(1)

http_parse(1)

Page 52: Node.js

Node execution stack

ev_loop()

socket_readable(1)

Page 53: Node.js

Node execution stack

ev_loop()

Page 54: Node.js

The request is sent to the disk.

Millions of clock cycles will passbefore the process hears back fromit.

Page 55: Node.js

In the meantime someone elseconnects to the server.

This time requesting an in-memoryresource.

Page 56: Node.js

Node execution stack

ev_loop()

Page 57: Node.js

Node execution stack

ev_loop()

socket_readable(2)

Page 58: Node.js

Node execution stack

ev_loop()

socket_readable(2)

http_parse(2)

Page 59: Node.js

Node execution stack

ev_loop()

socket_readable(2)

http_parse(2)

http_respond(2)

Page 60: Node.js

Node execution stack

ev_loop()

socket_readable(2)

http_parse(2)

Page 61: Node.js

Node execution stack

ev_loop()

socket_readable(2)

Page 62: Node.js

Node execution stack

ev_loop()

Page 63: Node.js

The process sits idle.

The first request is still hanging.

Eventually the disk responds:

Page 64: Node.js

Node execution stack

ev_loop()

Page 65: Node.js

Node execution stack

ev_loop()

file_loaded()

Page 66: Node.js

Node execution stack

ev_loop()

file_loaded()

http_respond(1)

Page 67: Node.js

Node execution stack

ev_loop()

file_loaded()

Page 68: Node.js

Node execution stack

ev_loop()

Page 69: Node.js

Usage andExamples(using node 0.1.93)

Page 70: Node.js

Download, configure, compile, andmake install it.

http://nodejs.org/

No dependencies other than Pythonfor the build system. V8 is included inthe distribution.

Page 71: Node.js

1 var sys = require(’sys’);2

3 setTimeout(function () {4 sys.puts(’world’);5 }, 2000);6 sys.puts(’hello’);

A program which prints “hello”, waits2 seconds, outputs “world”, and thenexits.

Page 72: Node.js

1 var sys = require(’sys’);2

3 setTimeout(function () {4 sys.puts(’world’);5 }, 2000);6 sys.puts(’hello’);

Node exits automatically when thereis nothing else to do.

Page 73: Node.js

% node hello_world.jshello

2 seconds later...

% node hello_world.jshelloworld%

Page 74: Node.js

A program which:

I starts a TCP server on port 8000I send the peer a messageI close the connection

Page 75: Node.js

1 net = require(’net’);2

3 var s = net.createServer();4 s.addListener(’connection’,5 function (c) {6 c.end(’hello!\n’);7 });8

9 s.listen(8000);

Page 76: Node.js

File I/O is non-blocking too.

(Something typically hard to do.)

Page 77: Node.js

As an example, a program thatoutputs the last time /etc/passwdwas modified:

1 var stat = require(’fs’).stat,2 puts = require(’sys’).puts;3

4 stat(’/etc/passwd’, function (e, s) {5 if (e) throw e;6 puts(’modified: ’ + s.mtime);7 });

Page 78: Node.js

Road Map

Page 79: Node.js

Now:

Solution for real-time problems alongside traditional stack.

(long poll, WebSocket)

Page 80: Node.js

Internet

NGINX

Rails Rails Rails Rails Node

Page 81: Node.js

As frameworks built on Node mature,use it for entire websites.

For security set it behind a stableweb server.

Load balancing unnecessary.

Page 82: Node.js

Internet

NGINX

Node

Page 83: Node.js

When Node is stable, remove thefront-end web server entirely.

Internet

Node

Page 84: Node.js

Questions...?http://nodejs.org/

[email protected]