Slide 1

Slide 1 text

A language for the Internet Why JavaScript and Node.js is right for Internet Applications Tom Hughes-Croucher @sh1mmer

Slide 2

Slide 2 text

Me (@sh1mmer)

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

Scalable Server-Side Code with JavaScript Tom Hughes-Croucher Node Up and Running

Slide 5

Slide 5 text

Internet?

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

She’s called Eleanor

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

No content

Slide 14

Slide 14 text

More features More users More devices More data

Slide 15

Slide 15 text

Stuff Cost

Slide 16

Slide 16 text

How do we cope with the increase in demand?

Slide 17

Slide 17 text

Internet Applications

Slide 18

Slide 18 text

How about search?

Slide 19

Slide 19 text

Browser Spidering Server Web Sites

Slide 20

Slide 20 text

Would take forever!

Slide 21

Slide 21 text

Browser Front-end Server Database Computation

Slide 22

Slide 22 text

Client → Server Computation Batch Computing

Slide 23

Slide 23 text

No content

Slide 24

Slide 24 text

Client → Server Server → DB Computation Computation Internet Computing

Slide 25

Slide 25 text

“Traditional” Approach

Slide 26

Slide 26 text

No content

Slide 27

Slide 27 text

Server

Slide 28

Slide 28 text

Request

Slide 29

Slide 29 text

No content

Slide 30

Slide 30 text

No content

Slide 31

Slide 31 text

FULL

Slide 32

Slide 32 text

Event-driven Approach

Slide 33

Slide 33 text

Request Place- holder

Slide 34

Slide 34 text

No content

Slide 35

Slide 35 text

Shared Work Resources

Slide 36

Slide 36 text

Welcome to Node.js

Slide 37

Slide 37 text

Node.js? • Server Side JavaScript runtime • Built on top of V8 JavaScript engine from Google Chrome • Non-blocking I/O APIs • Easy to extend APIs and modules

Slide 38

Slide 38 text

$Enki:~ $ node

Slide 39

Slide 39 text

$Enki:~ $ node > 3 > 2 > 1 false > true == 1 true > true === 1 false

Slide 40

Slide 40 text

> console.log('Hello World'); Hello World > .help .clear Break, and also clear the local context. .exit Exit the prompt .help Show repl options > .clear Clearing context... > .exit Enki:~ $

Slide 41

Slide 41 text

Enki:~ $ node > var foo = "bar"; > foo; 'bar' > .clear Clearing context... > foo ReferenceError: foo is not defined at [object Context]:1:1 at Interface. (repl:98:19) at Interface.emit (events:27:15) at Interface._ttyWrite (readline:295:12) at Interface.write (readline:132:30) at Stream. (repl:79:9) at Stream.emit (events:27:15) at IOWatcher.callback (net:489:16)

Slide 42

Slide 42 text

var http = require('http'); http.createServer(function (req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello World\n'); }).listen(8124, "127.0.0.1"); console.log('Server running at http://127.0.0.1:8124/');

Slide 43

Slide 43 text

var http = require('http'); //include the http library

Slide 44

Slide 44 text

http.createServer(function (req, res) { }).listen(8124, "127.0.0.1"); //create an http server //when ‘stuff’ happens call this anonymous function //listen on port 8124 of the IP 127.0.0.1

Slide 45

Slide 45 text

http.createServer(function (req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello World\n'); }) //when ‘stuff’ happens my function fires //I get a request object and a response object //I write to the response object header //HTTP status 200 and content-type ‘text/plain’ //close the response with the body: //Hello World

Slide 46

Slide 46 text

console.log('Server running at http://127.0.0.1:8124/'); //write Server is running at http://127.0.0.1:8124/ //to the console

Slide 47

Slide 47 text

Why is Node.js suited to Internet Apps?

Slide 48

Slide 48 text

No content

Slide 49

Slide 49 text

What is the event loop?

Slide 50

Slide 50 text

No content

Slide 51

Slide 51 text

Multi-tasking, one thing at a time.

Slide 52

Slide 52 text

No content

Slide 53

Slide 53 text

No content

Slide 54

Slide 54 text

var http = require('http'); server = http.createServer(); server.on('request', function (req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello World\n'); }); server.listen(8124, "127.0.0.1"); console.log('Server running at http://127.0.0.1:8124/');

Slide 55

Slide 55 text

var http = require('http'); server = http.createServer(); server.on('request', function (req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello World\n'); }); server.listen(8124, "127.0.0.1"); console.log('Server running at http://127.0.0.1:8124/'); Step 1. Evaluate 'Main'

Slide 56

Slide 56 text

var http = require('http'); server = http.createServer(); server.on('request', function (req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello World\n'); }); server.listen(8124, "127.0.0.1"); console.log('Server running at http://127.0.0.1:8124/'); Step 1. variables: http -> http module server -> http server listeners: server.request -> function

Slide 57

Slide 57 text

var http = require('http'); server = http.createServer(); server.on('request', function (req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello World\n'); }); server.listen(8124, "127.0.0.1"); console.log('Server running at http://127.0.0.1:8124/'); Step 2. Event Loop *

Slide 58

Slide 58 text

var http = require('http'); server = http.createServer(); server.on('request', function (req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello World\n'); }); server.listen(8124, "127.0.0.1"); console.log('Server running at http://127.0.0.1:8124/'); Step 2. Do we have active listeners? listeners: server.request -> function Yes! Wait for listeners. *

Slide 59

Slide 59 text

var http = require('http'); server = http.createServer(); server.on('request', function (req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello World\n'); }); server.listen(8124, "127.0.0.1"); console.log('Server running at http://127.0.0.1:8124/'); Step 3. Event Calls

Slide 60

Slide 60 text

var http = require('http'); server = http.createServer(); server.on('request', function (req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello World\n'); }); server.listen(8124, "127.0.0.1"); console.log('Server running at http://127.0.0.1:8124/'); Step 3. 'request' is called. Since listeners: server.request -> function Call function

Slide 61

Slide 61 text

var http = require('http'); server = http.createServer(); server.on('request', function (req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello World\n'); }); server.listen(8124, "127.0.0.1"); console.log('Server running at http://127.0.0.1:8124/'); Step 3. Loop! (go to Step 2.)

Slide 62

Slide 62 text

Why non-blocking matters

Slide 63

Slide 63 text

Event Loop vs. Threads

Slide 64

Slide 64 text

//node.js style JavaScript query = mysql.query('SELECT * FROM data'); query.on(‘result’, function(result) { for(var i=0;i

Slide 65

Slide 65 text

//threaded style JavaScript result = mysql.query('SELECT * FROM data'); for(var i=0;i

Slide 66

Slide 66 text

var x = "I am a string" ~1ns Running 1 instruction 2ns Data from l1 cpu cache 5ns Data from l2 cpu cache 80ns Data from ram

Slide 67

Slide 67 text

Server mySQL query result

Slide 68

Slide 68 text

result = mysql.query('SELECT * FROM data'); ~100ms Time to run a query in database 50ms Time to roundtrip query over the network

Slide 69

Slide 69 text

Local variable

Slide 70

Slide 70 text

No content

Slide 71

Slide 71 text

Remote operation

Slide 72

Slide 72 text

No content

Slide 73

Slide 73 text

Cat 100 Blue Whales

Slide 74

Slide 74 text

Blocking is as bad as stopping

Slide 75

Slide 75 text

Threading wastes memory for a long time because of programming style

Slide 76

Slide 76 text

2mb PHP 8gb ram 8000/2 = 4000 ~4000 users per machine

Slide 77

Slide 77 text

8gb ram 8000/0.006 = 1.3m 1.3m/2 ~ 650k users Node.js TCP = 2kb HTTP = 6kb

Slide 78

Slide 78 text

Apache vs NGINX concurrency × reqs/sec http://blog.webfaction.com/a-little-holiday-present

Slide 79

Slide 79 text

Apache vs NGINX concurrency × memory http://blog.webfaction.com/a-little-holiday-present

Slide 80

Slide 80 text

Node.js is designed for communication, just like your applications

Slide 81

Slide 81 text

Demo

Slide 82

Slide 82 text

Thanks!

Slide 83

Slide 83 text

Follow me @sh1mmer Tom Hughes-Croucher @sh1mmer