node.js presentation

30

Upload: exist

Post on 05-Dec-2014

2.251 views

Category:

Technology


4 download

DESCRIPTION

Exist's software engineer Martin Villasica presentation on Node.js during a tech session at Exist Cebu office.

TRANSCRIPT

Page 1: Node.js Presentation
Page 2: Node.js Presentation

What is node.js?

● Server-side Javascript● Built on Google’s V8● Evented, non-blocking I/O.● EventMachine or Twisted.● CommonJS module system.● 8000 lines of C/C++, 2000 lines of Javascript, 14

contributors.

Page 3: Node.js Presentation

Main Goal

To provide a purely evented,non-blocking infrastructure to

script highly concurrent programs.

Page 4: Node.js Presentation

Why Evented?

Page 5: Node.js Presentation

Apache vs NGINXconcurrency ×

reqs/sec

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

Page 6: Node.js Presentation

Apache vs NGINX

concurrency × memory

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

Page 7: Node.js Presentation

Apache vs NGINX

The difference?

Apache uses one thread perConnection.

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

For massive concurrency, cannotuse an OS thread for eachconnection.

Page 8: Node.js Presentation

Javascript designed specifically to be used with an event loop:

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

The culture of Javascript is already geared towards evented programming.

Page 9: Node.js Presentation

Why non-blocking?

Page 10: Node.js Presentation
Page 11: Node.js Presentation

Design Goals

Page 12: Node.js Presentation

Design Goals

● No function should direct perform I/O.● To receive info from disk, network, or

another process there must be a callback.

Page 13: Node.js Presentation

Code like this

either blocks the entire process or implies multiple execution stacks.

Page 14: Node.js Presentation

But a line of code like this

allows the program to return to the event loop immediately.

No machinery required.

This is how I/O should be done.

Page 15: Node.js Presentation

Design Goals

● Low-level.● Stream everything; never force the buffering of

data.● Have built-in support for the most important

protocols:● TCP● DNS● HTTP

Page 16: Node.js Presentation

Design Goals

● Support many HTTP features.● Chunked requests and responses.● Keep-alive.● Hang requests for comet applications.

Page 17: Node.js Presentation

Design Goals

● The API should be both familiar to client-side JS programmers and old school UNIX hackers.

● Be platform independent.

Page 18: Node.js Presentation

Speed

Page 19: Node.js Presentation

Benchmarks

● nginx v0.7.65● node v0.1.91● tornado v0.2 (python 2.6.4)● thin v1.2.7 (ruby 1.9.1-p376)

In Linux using a Intel Core 2 Due 2.53, 4 GB memory

Page 20: Node.js Presentation

The standard ‘hello world’ concurrency benchmark.

(Approximately 100 byte response for each.)

Page 21: Node.js Presentation
Page 22: Node.js Presentation

How do the servers perform whenthe concurrency is fixed at 300, but

serve different response sizes.

This is what the node version lookslike, approximately

Page 23: Node.js Presentation
Page 24: Node.js Presentation
Page 25: Node.js Presentation

Wow. Node sucks at serving large files.

Well over 3 second responses for 256 kilobyte files at 300 concurrent connections.

Page 26: Node.js Presentation

V8 has a generational garbage collector. Moves objects around randomly.

Node can’t get a pointer to raw stringdata to write to socket.

What’s happening:

Page 27: Node.js Presentation

Using Node’s new Buffer object,the results change.

Page 28: Node.js Presentation
Page 29: Node.js Presentation
Page 30: Node.js Presentation

https://github.com/joyent/node/wiki/Installation

Installation