why nodejs guilin shanghai

69
Why by 桂林 桂糊涂@weibo guilin1981@twitter

Upload: jacksontian

Post on 05-Nov-2014

2.043 views

Category:

Technology


1 download

DESCRIPTION

http://event.weibo.com/133410@Guilin Why NodeJS 2011-06-18

TRANSCRIPT

Page 1: Why Nodejs Guilin Shanghai

Why

by 桂林

桂糊涂@weibo

guilin1981@twitter

Page 2: Why Nodejs Guilin Shanghai

About meAbout me

2003, Delphi.

2006, Java.

2009, Python.

2010, Node.js.

Author of mongoskinmongoskin and stormjsstormjs

Page 3: Why Nodejs Guilin Shanghai

What is node.js?What is node.js?

Evented I/Ofor

V8 JavaScript.

Page 4: Why Nodejs Guilin Shanghai

What can node.js do?What can node.js do?

Page 5: Why Nodejs Guilin Shanghai

What can node.js do?What can node.js do?

Node.js as webserver

Page 6: Why Nodejs Guilin Shanghai

What can node.js do?What can node.js do?

Node.js as webserverC10k problem

Page 7: Why Nodejs Guilin Shanghai

What can node.js do?What can node.js do?

Node.js as webserverC10k problem

Comet

Page 8: Why Nodejs Guilin Shanghai

What can node.js do?What can node.js do?

Node.js as webserverC10k problem

Comet

Websocket

Page 9: Why Nodejs Guilin Shanghai

What can node.js do?What can node.js do?

Node.js as webserverC10k problem

Comet

Websocket

Node.js as TCP server

Page 10: Why Nodejs Guilin Shanghai

What can node.js do?What can node.js do?

Node.js as webserverC10k problem

Comet

Websocket

Node.js as TCP server

Node.js as CLI tools

Page 11: Why Nodejs Guilin Shanghai

What can node.js do?What can node.js do?

Node.js as webserverC10k problem

Comet

Websocket

Node.js as TCP server

Node.js as CLI tools

Node.js as GUI tools

Page 12: Why Nodejs Guilin Shanghai

Why node.jsWhy node.js

Why asynchronous IO?Why asynchronous IO?

Page 13: Why Nodejs Guilin Shanghai

Why asynchronous IO?Why asynchronous IO?IO is the bottle-neck of application.

Page 14: Why Nodejs Guilin Shanghai

Why asynchronous IO?Why asynchronous IO?IO is the bottle-neck of application.

Network IO

File IO

Database IO

Page 15: Why Nodejs Guilin Shanghai

Blocking socket IOBlocking socket IOclass ServerThread extends Thread { public ServerThread(socket){ this.socket = socket; } public void run(){ BufferedReader reader = new BufferedReader(this.socket.getInputStream()); String line; while((line = reader.readLine()) != null){ // block here // handle line } }}...ServerSocket serverSocket = new ServerSocket(port);while (true) { Socket s = serverSocket.accept(); // block here new ServerThread(s).start();}

Page 16: Why Nodejs Guilin Shanghai

Non-blocking socket IONon-blocking socket IOnet.createServer(function(socket){ socket.on('data', function(data){ // handle data }); socket.on('close', function(has_error){ // handle close }); socket.on('error', function(err){ // handle error });}).listen(port);

Page 17: Why Nodejs Guilin Shanghai

Non-blocking IO vs Blocking IONon-blocking IO vs Blocking IO

nginx(non-blocking) vs apache(blocking)

Page 18: Why Nodejs Guilin Shanghai

Node.js IO APINode.js IO API

stream

fs

http

net

dns

Page 19: Why Nodejs Guilin Shanghai

Why node.jsWhy node.js

Why asynchronous IO?

Why javascr ipt?Why javascr ipt?

Page 20: Why Nodejs Guilin Shanghai

Why javascript?Why javascript?

Page 21: Why Nodejs Guilin Shanghai

Why javascript?Why javascript?

Page 22: Why Nodejs Guilin Shanghai

Why javascript?Why javascript?

Page 23: Why Nodejs Guilin Shanghai

Why javascript?Why javascript?

Browsers war improving javascript.

Page 24: Why Nodejs Guilin Shanghai

Why javascript?Why javascript?

Browsers war improving javascript.

Javascript is one of the fastestfastest dynamic programming language.

Page 25: Why Nodejs Guilin Shanghai

Why javascript?Why javascript?

Browsers war improving javascript.

Javascript is one of the fastestfastest dynamic programming language.

Javascript is one of the most popularmost popular programming language.

Page 26: Why Nodejs Guilin Shanghai

Why javascript?Why javascript?

Browsers war improving javascript.

Javascript is one of the fastestfastest dynamic programming language.

Javascript is one of the most popularmost popular programming language.

Javascript has c losureclosure(anonymoused function).

Page 27: Why Nodejs Guilin Shanghai

Why javascript?Why javascript?

Browsers war improving javascript.

Javascript is one of the fastestfastest dynamic programming language.

Javascript is one of the most popularmost popular programming language.

Javascript has c losureclosure(anonymoused function).

Javascript is cross-platform.

Page 28: Why Nodejs Guilin Shanghai

Why node.jsWhy node.js

Why asynchronous IO?

Why javascript?

Why v8?Why v8?

Page 29: Why Nodejs Guilin Shanghai

About v8About v8

Page 30: Why Nodejs Guilin Shanghai

About v8About v8

V8 javascript VM is used in Google ChromeGoogle Chrome.

Page 31: Why Nodejs Guilin Shanghai

About v8About v8

V8 javascript VM is used in Google ChromeGoogle Chrome.

V8 team is led by Lars BakLars Bak , one of the leading VM engineers in theworld with 20 years of experience in building VM.

Page 32: Why Nodejs Guilin Shanghai

About v8About v8

V8 javascript VM is used in Google ChromeGoogle Chrome.

V8 team is led by Lars BakLars Bak , one of the leading VM engineers in theworld with 20 years of experience in building VM.

Lars BakLars Bak was the technical leadtechnical lead behind HotSpot(Sun’s Java VM).HotSpot improved Java’s performance 20x t imes20x t imes.

Before HotSpot, Lars BakLars Bak worked on a smalltalk VMsmalltalk VM .

Page 33: Why Nodejs Guilin Shanghai

How fast v8 is?How fast v8 is?

Fast Property Access

Dynamic Machine Code Generation

Efficient Garbage Collection

Page 34: Why Nodejs Guilin Shanghai

V8 exampleV8 exampleThe JavaScript code to access property x from a Point object is:point.x

Page 35: Why Nodejs Guilin Shanghai

V8 exampleV8 exampleThe JavaScript code to access property x from a Point object is:point.x

In V8, the machine code generated for accessing x is:# ebx = the point objectcmp [ebx,<hidden class offset>],<cached hidden class>jne <inline cache miss>mov eax,[ebx, <cached x offset>]

Page 36: Why Nodejs Guilin Shanghai

Why node.jsWhy node.js

Why asynchronous IO?

Why javascript?

Why v8?

Why threadless?Why threadless?

Page 37: Why Nodejs Guilin Shanghai

Why threadless?Why threadless?

Page 38: Why Nodejs Guilin Shanghai

Why threadless?Why threadless?

2mb stack per thread

Page 39: Why Nodejs Guilin Shanghai

Why threadless?Why threadless?

2mb stack per thread

Dead-locking

Page 40: Why Nodejs Guilin Shanghai

Why threadless?Why threadless?

2mb stack per thread

Dead-locking

Thread-safe?

Page 41: Why Nodejs Guilin Shanghai

Why threadless?Why threadless?

2mb stack per thread

Dead-locking

Thread-safe?

Thread is design for blocking IO.

Page 42: Why Nodejs Guilin Shanghai

Why node.jsWhy node.js

Why asynchronous IO?

Why javascript?

Why v8?

Why threadless?

Why node. js special?Why node. js special?

Page 43: Why Nodejs Guilin Shanghai

Why node.js special?Why node.js special?Pythoner:

Page 44: Why Nodejs Guilin Shanghai

Why node.js special?Why node.js special?Pythoner:

Python is one of the most popular dynamic language too.

Page 45: Why Nodejs Guilin Shanghai

Why node.js special?Why node.js special?Pythoner:

Python is one of the most popular dynamic language too.

Performance of python is OK.

Page 46: Why Nodejs Guilin Shanghai

Why node.js special?Why node.js special?Pythoner:

Python is one of the most popular dynamic language too.

Performance of python is OK.

We have good non-blocking web framework.

Page 47: Why Nodejs Guilin Shanghai

Tornado by facebookTornado by facebook

Tornado is a non-blocking web server, open-source by facebook.

Page 48: Why Nodejs Guilin Shanghai

Hello TornadoHello Tornadoimport tornado.ioloopimport tornado.web

class MainHandler(tornado.web.RequestHandler): def get(self): self.write("Hello, world")

application = tornado.web.Application([ (r"/", MainHandler),])

if __name__ == "__main__": application.listen(8888) tornado.ioloop.IOLoop.instance().start()

Page 49: Why Nodejs Guilin Shanghai

Hello NodejsHello Nodejsvar http = require('http');http.createServer(function (req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello Node.js\n');}).listen(3000, "127.0.0.1");

Page 50: Why Nodejs Guilin Shanghai

Tornado with IOTornado with IOimport pymongoimport tornado.web

class Handler(tornado.web.RequestHandler): @property def db(self): if not hasattr(self, '_db'): # block here self._db = pymongo.Connection( host='127.0.0.1', port=27017).test return self._db

def get(self): try: # block here user = self.db.users.find_one({'username': self.current_user}) self.render('template', full_name=user['full_name']) except: raise tornado.web.HTTPError(500)

Page 51: Why Nodejs Guilin Shanghai

Tornado with async IOTornado with async IOimport asyncmongoimport tornado.web

class Handler(tornado.web.RequestHandler): @property def db(self): if not hasattr(self, '_db'): self._db = asyncmongo.Client(pool_id='mydb', host='127.0.0.1', port=27017, maxcached=10, maxconnections=50, dbname='test') return self._db

@tornado.web.asynchronous def get(self): self.db.users.find({'username': self.current_user}, limit=1, callback=self._on_response)

def _on_response(self, response, error): if error: raise tornado.web.HTTPError(500) self.render('template', full_name=respose['full_name'])

Page 52: Why Nodejs Guilin Shanghai

Node.js with IONode.js with IOAnonymoused function, is very important for Evented IOvar mongo = require('mongoskin'), http = require('http'), jade = require('jade'), db = mongo.db('localhost/test');

http.createServer(function (req, res) { res.writeHead(200, {'Content-Type': 'text/html'}); db.users.findOne({'username': req.session.current_user}, function(err, user){ jade.renderFile('template', {user: user}, function(err, html){ res.end(html); }); });}).listen(3000, "127.0.0.1");

Page 53: Why Nodejs Guilin Shanghai

What make node.js special?What make node.js special?

Page 54: Why Nodejs Guilin Shanghai

What make node.js special?What make node.js special?

Javascript is special.

Page 55: Why Nodejs Guilin Shanghai

What make node.js special?What make node.js special?

Javascript is special.

Most of node.js modules are asynchronoused.

Page 56: Why Nodejs Guilin Shanghai

What make node.js special?What make node.js special?

Javascript is special.

Most of node.js modules are asynchronoused.

It’s easy to write threadless module.

Page 57: Why Nodejs Guilin Shanghai

What make node.js special?What make node.js special?

Javascript is special.

Most of node.js modules are asynchronoused.

It’s easy to write threadless module.

Node.js community is very active.

Page 58: Why Nodejs Guilin Shanghai

How many modules?How many modules?

2320 modules can beinstall via npm2011, June 07.

Page 59: Why Nodejs Guilin Shanghai

Node.js communityNode.js community

What do you thinkabout nodecommunity?

Page 60: Why Nodejs Guilin Shanghai

Arunoda:Arunoda:

Its the freedom followed by tools like github and npm. And everythingis growing fast.

Page 61: Why Nodejs Guilin Shanghai

Pau:Pau:

People is more open minded than in other programming communities.There is people coming from ruby, python, c, haskell, java, erlang,php…

Page 62: Why Nodejs Guilin Shanghai

Mikeal Rogers:Mikeal Rogers:

People using node.js are building stuff. the project seeks to make thelives better of people building products.

Page 63: Why Nodejs Guilin Shanghai

Mikeal Rogers:Mikeal Rogers:

People using node.js are building stuff. the project seeks to make thelives better of people building products.My experience with Python and Ruby is that their primary reason forworking on the project is around with a new language and/or vm. thepeople who work on “core” don’t build products and probably neverwill.

Page 64: Why Nodejs Guilin Shanghai

Mikeal Rogers:Mikeal Rogers:

People using node.js are building stuff. the project seeks to make thelives better of people building products.My experience with Python and Ruby is that their primary reason forworking on the project is around with a new language and/or vm. thepeople who work on “core” don’t build products and probably neverwill.Node.js is not a language and it doesn’t write it’s own vm. it’s notattractive to people who don’t care about building stuff with it.

Page 65: Why Nodejs Guilin Shanghai

桂林:桂林:

潮不等于装13

Page 66: Why Nodejs Guilin Shanghai

桂林:桂林:

潮不等于装13 , node.js很酷,也很务实。

Page 67: Why Nodejs Guilin Shanghai

Why node.jsWhy node.js

Page 68: Why Nodejs Guilin Shanghai

Why node.jsWhy node.js

Why asynchronous IO?Never blocking, cpu efficient

Why javascript?Right and popluar language

Why v8?Extremely fast VM

Why threadless?Easy, memory efficient

Why node.js special?Active and creative community

Page 69: Why Nodejs Guilin Shanghai

Thank youThank you