+ All Categories
Home > Technology > Node.js for Rubists

Node.js for Rubists

Date post: 15-May-2015
Category:
Upload: sagiv-ofek
View: 865 times
Download: 2 times
Share this document with a friend
Popular Tags:
35
Speaker: Sagiv Ofek
Transcript
Page 1: Node.js for Rubists

Speaker: Sagiv Ofek

Page 2: Node.js for Rubists

why?

Node's goal is to provide an easy way to build scalable network programs. --Ryan Dahl

Page 3: Node.js for Rubists

how?

Keep slow operations from blocking other operations.

Page 4: Node.js for Rubists

Traditional I/O

var pickUpLine = file.read('file.txt');tryYourBestWithThat(pickUpLine);

can you tell wat's wrong here?

//zzzzzzz

Page 5: Node.js for Rubists

ruby blocking sample

parties = Party.where(:line => ['Free Drinks', 'Supermodels','Koby Peretz']).all

parties.go

Page 6: Node.js for Rubists

Modern Computer Latency

● L1: 3 cycles● L2: 14 cycles● RAM: 250 cycles—————————

● DISK: 41,000,000 cycles● NETWORK: 240,000,000 cycles

Blocking

Non - Blocking

Page 7: Node.js for Rubists

Modern Computer Latency

● reaching RAM is like going from here to the Red Light District.

● Accessing the network is like going to the moon.

so, what shall we do?

Page 8: Node.js for Rubists

we have processes!

time to use our gizillions cores!● does not scale well, hundreds of connections

means hundreds of processes.● Spinning up a new process takes

significantly more memory, a megabyte on some platforms.

Page 9: Node.js for Rubists

we have threads!

● Coarse-grained locks – more blocking● Fine-grained locks – more complexity● Risk for deadlocks● Hard to reason about and therefore to get

right● Context switching overhead

Page 10: Node.js for Rubists

we have libraries!

There are asynchronous I/O libraries for Ruby as well, like EventMachine or Cool.IO for example. And of course there's Twisted for Python, Async, Event and EV for Perl, Rx for .NET, Async Computation Expressions for F# and libaio, libevent and libev for C. (BTW: Node.js is actually implemented using libev)

Page 11: Node.js for Rubists

we have libraries!

When you are within the event loop, you cannot make any blocking calls. However, pretty much the entire Ruby IO, File and Dir classes, as well as the networking and database libraries and so on are all mostly synchronous and blocking. (And the same is true for pretty much all of the other languages as well.) So, when you write code in EventMachine or Cool.IO or Twisted, you have to be very careful which methods you call and which libraries you use in order to avoid accidentally making blocking I/O calls.

Page 12: Node.js for Rubists

nginx vs. apache

● Apache uses 1 thread per connection.○ 1 thread per connection is limiting for massive

concurrency● Nginx doesn't use threads

○ it runs an event loop○ small memory allocation per connection○ Especially relevant when dealing with I/O All code

runs in a single thread○ No need for multithreading – no locks!

Page 13: Node.js for Rubists

nginx vs. apache

Page 14: Node.js for Rubists

nginx vs. apache

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

Page 15: Node.js for Rubists

benchmarking - req/sec

100 concurrent clients1 megabyte response ● node 822 ● nginx 708● thin 85● mongrel 4

Page 16: Node.js for Rubists

why should i care?

● I love Ruby● I can use EventMachine for async● And, after all, my Rails app doesn't have

youporn’s traffic

Page 17: Node.js for Rubists

what do we want?

● closures● event-driven language (callbacks)● easy to code● non-blocking, no processes/threads hassle● widely usewindow.onload = function() { alert("Apollo 11 landed!")}d

Page 18: Node.js for Rubists

● Ruby is a programming language and Rails is a web application framework.

● Node.js is neither a language nor an application framework, it's an asynchronous I/O library.

● ECMAScript is crap- can't read files, load scripts, access the network. You can't even access the friggin' web! (which is kind of ironic for a web scripting language) => Node is great!

Page 19: Node.js for Rubists

● A set of bindings to Google’s V8 Javascript VM (fast, super-duper fast)

● A purely evented, non-blocking infrastructure that makes it super simple to build highly concurrent programs

● Ability to handle thousands of concurrent connections with minimal overhead on a single process

● all blocking I/O written from scratch (in C)● CommonJS module format● one code to rule them all!

Page 20: Node.js for Rubists

node is

● only exposes non-blocking asynchronous interfaces

● only one thread, one stack (like browser)● low level features: fs, tcp, udp● has killer HTTP support

Page 21: Node.js for Rubists
Page 22: Node.js for Rubists

Async I/O

file.read('file.txt', function(pickUpLine) { tryYourBestWithThat(pickUpLine);});flirtWithTheUglyOne();

win!

Page 23: Node.js for Rubists

installing node.js

> brew install node.jsor> git clone http://github.com/ry/node.git && cd node> ./configure && make && make install

Page 24: Node.js for Rubists

time to code!

demo - node console, global, process, version, env, pid, hello world

Page 25: Node.js for Rubists

CommonJS & Modules

CommonJS is a community driven effort to standardize packaging of JavaScript libraries, known as modules. Modules written which comply to this standard provide portability between other compliant frameworks such as narwhal, and in some cases even browsers.

Page 26: Node.js for Rubists

CommonJS & Modules

hello.jsexports.world = function() { return 'Hello World';};

app.jsvar hello = require('./hello');var sys = require('sys');sys.puts(hello.world());

Page 27: Node.js for Rubists

http server

var sys = require('sys'), http = require('http');http.createServer(function (req, res) {

res.writeHead(200, {'Content-Type': 'text/plain'});

res.write('Hello World');res.end();

}).listen(8000);sys.puts('Running at http://127.0.0.1:8000/');

Page 28: Node.js for Rubists

tcp server

var tcp = require('net');tcp.createServer(function(socket) { socket.addListener('connect', function() { socket.send("Hi, How Are You?\n> "); }); socket.addListener('receive', function(data) { socket.send(data); });}).listen(4000);

Page 29: Node.js for Rubists

child process

var spawn = require('child_process').spawn, ls = spawn('ls');ls.stdout.on('data', function (data) { console.log('stdout: ' + data);});console.log('Spawned child pid: ' + ls.pid);ls.stdin.end();

Page 30: Node.js for Rubists

libraries

● Connect (middleware)● Express (sinatra)● Mongoose (db)● Expresso / should.js (TDD)● node_redis (guess)● npm● much (much) more...

Page 31: Node.js for Rubists

'ruby is so yesterday's news'...

● Tower.js● RailwayJS

Page 32: Node.js for Rubists

RailwayJS

● full stack MVC framework.● Doing to Node what Rails has done to Ruby.

> sudo npm install railway -g> railway init blog && cd blog> npm install -l> railway generate crud post title content> railway server 8888> open http://127.0.0.1:8888/posts

Page 33: Node.js for Rubists

hello world!TEXT_IO.PUT_LINE ("Hello, World!"); Response.Write("Hello, World!")10 PRINT "Hello, World!"System.Console.WriteLine("Hello, World!");std::cout << "Hello, World!\n";DISPLAY "Hello, World!".fmt.Printf("Hello, World!\n");System.out.println("Hello, World!");(format t "Hello World!~%")printf( "Hello, World!\n" );print "Hello, World!\n";echo 'Hello, World!';:- write('Hello world'),nl.puts "Hello, World!"

adaaspbasicc#c++cobolgojavalispobjectiveCperlPHPprologruby

Page 34: Node.js for Rubists

Javascript ftw!

one language to rule them alll

Page 35: Node.js for Rubists

alert('the end!');


Recommended