intro to node and non blocking io

26
Intro to Node.js Amy Hua

Upload: amy-hua

Post on 06-May-2015

4.958 views

Category:

Technology


2 download

DESCRIPTION

My introduction to Node.js with comparisons to Ruby on Rails. I explain non-blocking I/O by using a restaurant analogy to ordering food.

TRANSCRIPT

Page 1: Intro to node and non blocking io

Intro to Node.js

Amy Hua

Page 2: Intro to node and non blocking io

Agenda

What is Node.js?

What is non-blocking I/O?

When should you use Node?

Node vs Ruby on Rails

Page 3: Intro to node and non blocking io

What is Node.js? | summary

• is a command-line tool that can be run as a regular web server and lets one run Javascript programs

• utilizes the very fast V8 Javascript engine built in Google Chrome• is very good when you need to do several things at the same time• is event-based so all the wonderful Ajax like stuff can be done on

the server side• lets us share code between the browser and the backend

Source: http://stackoverflow.com/questions/5062614/how-to-decide-when-to-use-nodejs

Page 4: Intro to node and non blocking io

What is Node.js? | summary

• InfoWorld Technology of the Year Award (2012)• 2nd most popular repo on Github

Page 5: Intro to node and non blocking io

What is Node.js?

“Node is Javascript on the server…done right”

“Javascript everywhere”

Sounds confusing:• Node is not written in Javascript• It is written in C++, but developers interact with Node through

the look and feel of Javascript

• ~20,000 lines of C++• ~20,000 lines of Javascript

• Asynchronous server model

Page 6: Intro to node and non blocking io

What is Node.js?

Node is a set of libraries written on top of Chrome’s V8 Javascript Engine.

What is the V8 Javascript Engine?

• A Javascript engine is software that interprets & executes Javascript.• V8 Javascript Engine is written in C++ (and so is Node) and is extremely

fast at running Javascript.

Page 7: Intro to node and non blocking io

What is Node.js?

Announced in 2009, founded by Ryan Dahl.

Ryan Dahl originally built Node out of frustrations with

trying to build web servers in Ruby.

Page 8: Intro to node and non blocking io

What is Node.js?

Extremely young.

• 2009: Ryan Dahl announced Node.js

• 2010: became somewhat stable

• 11/2011: “stuff works in windows”

• 6/2012: “stuff works better”

Node is still changing

Page 9: Intro to node and non blocking io

What is Node.js?Node.js demonstrates “how I/O should be done”

– Ryan Dahl

What is I/O?

Page 10: Intro to node and non blocking io

What is Node.js?

event-driven, non-blocking I/O model

Page 11: Intro to node and non blocking io

What is Node.js? | I/O models

Blocking I/O Model

Example: ways in which a server can process orders from customers

Hi, my name is Apache. How may I take your order?

• The server serves one customer at a time.

• As each customer is deciding on their order, the server sits and waits.

• When the customer decides on an order, the server processes their order and moves on to the next customer.

Page 12: Intro to node and non blocking io

What is Node.js? | non-blocking I/O

Blocking I/O Model

Hmm… still thinking...

OMG she’s blocking me. I could have ordered by now.

Page 13: Intro to node and non blocking io

What is Node.js? | non-blocking I/O

Blocking I/O Model

Hmm… still thinking...

OMG she’s blocking me. I could have ordered by now. Pseudocode:

order1 = db.query(“SELECT * FROM menu WHERE preference = most”)

order1.process

order2.process

Page 14: Intro to node and non blocking io

What is Node.js? | non-blocking I/O

Blocking I/O Model

The more customers you want to serve at once, the more cashier lines you’ll need.

Cashier lines ~ threads in computing

Multi-threaded processing

Parallel code execution

Multiple CPUs run at a time, utilizing shared resources (memory)

Page 15: Intro to node and non blocking io

What is Node.js? | non-blocking I/O

Non-Blocking I/O Model

I’m still thinking, but callback to me when

I’m done.While he’s thinking, I’ll

order the salmon.

• Node loops through the customers and polls them to determine which ones are ready to order.

• During a function’s queue, Node can listen to another event.

• When the other customer is finally ready to order, he’ll issue a callback.

• Asynchronous callbacks: “come back to me when I’m finished”

• function called at the completion of a given task.

Page 16: Intro to node and non blocking io

What is Node.js? | non-blocking I/O

Non-Blocking I/O Model

I’m still thinking, but callback to me when

I’m done.While he’s thinking, I’ll

order the salmon.

Node code

console.log(‘Hello’);

setTimeout(function () { console.log(‘World’);}, 5000);

console.log(‘Bye’);

// Outputs:// Hello// Bye// World

Allows for high concurrency

Page 17: Intro to node and non blocking io

What is Node.js? | non-blocking I/O

Non-Blocking I/O Model

I’m still thinking, but callback to me when

I’m done.While he’s thinking, I’ll

order the salmon. Every function in Node

is non-blocking

Single-threaded

No parallel code execution

Single CPU

Page 18: Intro to node and non blocking io

What is Node.js? | non-blocking I/O

Node is great for applications with high concurrency

(Concurrency = number of concurrent clients or users)

Page 19: Intro to node and non blocking io

What is Node.js? | non-blocking I/O

nginx: non-blocking I/O

apache: blocking I/O

Page 20: Intro to node and non blocking io

Agenda

What is Node.js?

What is non-blocking I/O?

When should you use Node?

Node vs Ruby on Rails

Page 21: Intro to node and non blocking io

When should you use Node?

Should you always use Node?

Page 22: Intro to node and non blocking io

When should you use Node?

No

Page 23: Intro to node and non blocking io

When should you use Node?

Use Node when:

cost of I/O > cost of more difficult to write code

Cons:• Not useful for when I/O is light, CPU usage is high (e.g.,

video encoding software)• Still very barebones, not a powerful framework yet• You live on Mars

• can’t utilize any code or libraries in the blocking-I/O world (or else risk hanging your whole application)

Page 24: Intro to node and non blocking io

Agenda

What is Node.js?

What is non-blocking I/O?

When should you use Node?

Node vs Ruby on Rails

Page 25: Intro to node and non blocking io

Node.js vs Rails

Node.js Ruby on Rails

• Server framework. Requires Express.js or Mongoose, etc.

• One language: Javascript (and all of its pros and cons)

• Low-level, barebones framework with freeform MVC. Requires custom configuration of modules, views, and helpers. Handpicking over default configurations.

• Code sharing between server-side and client-side JS processes

• Still evolving libraries and tools, developing ecosystem

• No rapid prototyping

• Web framework

• Ruby, Javascript, SQL

• Full package, many built-in functions, more out of the box framework

• “Convention over Configuration” – spend less time on configuration, more on actual code

• Utilizes best practices

• Robust sources of libraries and tools, large ecosystem

• Rapid prototyping

Page 26: Intro to node and non blocking io

Questions