real-time web a nodejs

64
REALTIME WEB A NODE.JS realtime servery snadno a rychle Jakub Nešetřil @jakubnesetril pátek, 25. března 2011

Upload: jakub-nesetril

Post on 12-Jan-2015

1.371 views

Category:

Technology


2 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Real-time Web a NodeJS

REALTIME WEB A NODE.JS realtime servery snadno a rychle

Jakub Nešetřil@jakubnesetril

pátek, 25. března 2011

Page 2: Real-time Web a NodeJS

REALTIME WEB

co je realtime?

řekněme, že to znamená “rychle”

zhruba 100ms

pátek, 25. března 2011

Page 3: Real-time Web a NodeJS

REALTIME WEB

Na rychlosti záleží!

Google: +500ms => -20% traffic

Amazon: +100ms => -1% sales

pátek, 25. března 2011

Page 4: Real-time Web a NodeJS

OBSAH

Latence

Push vs. Pull

Blokující vs. neblokující provoz

Node.js, kód

pátek, 25. března 2011

Page 5: Real-time Web a NodeJS

LATENCE

pátek, 25. března 2011

Page 6: Real-time Web a NodeJS

TROCHA HISTORIE

1981: internet protocol – IP (RFC 791)

packety, IP adresy, směrování

1981: transmission control protocol – TCP (RFC 793)

datový přenos, spolehlivost, řízení toku, multiplexing, spojování, bezpečnost

pátek, 25. března 2011

Page 7: Real-time Web a NodeJS

TROCHA HISTORIE

1981: internet protocol – IP (RFC 791)

packety, IP adresy, směrování

1981: transmission control protocol – TCP (RFC 793)

datový přenos, spolehlivost, řízení toku, multiplexing, spojování, bezpečnost

pátek, 25. března 2011

Page 8: Real-time Web a NodeJS

TCP

“This is achieved by… requiring apositive acknowledgment (ACK) fromthe receiving TCP. If the ACK is notreceived within a timeout interval,the data is retransmitted.”

-RFC 793

pátek, 25. března 2011

Page 9: Real-time Web a NodeJS

TCP

Client Server

pátek, 25. března 2011

Page 10: Real-time Web a NodeJS

TCP

Client Server

GET /index.html

pátek, 25. března 2011

Page 11: Real-time Web a NodeJS

TCP

Client Server

GET /index.html

HTTP/1.1 200 OK

pátek, 25. března 2011

Page 12: Real-time Web a NodeJS

TCP

Client Server

GET /index.html

HTTP/1.1 200 OK

Content-Type: text/html

<html><title>Hi</title>…

pátek, 25. března 2011

Page 13: Real-time Web a NodeJS

TCP

Client Server

GET /index.html

HTTP/1.1 200 OK

Content-Type: text/html

<html><title>Hi</title>…

ACKACKACK

pátek, 25. března 2011

Page 14: Real-time Web a NodeJS

ROK 1981

213 počítačů

http://www.faqs.org/rfcs/rfc1296.html

pátek, 25. března 2011

Page 15: Real-time Web a NodeJS

1986

pátek, 25. března 2011

Page 16: Real-time Web a NodeJS

LEDEN 1986VÝBUCH CHALLENGERU

pátek, 25. března 2011

Page 17: Real-time Web a NodeJS

DUBEN 1986VÝBUCH ČERNOBYLU

pátek, 25. března 2011

Page 18: Real-time Web a NodeJS

ŘÍJEN 1986VÝBUCH INTERNETU

pátek, 25. března 2011

Page 19: Real-time Web a NodeJS

5089 počítačů

ROK 1986

0

1200

2400

3600

4800

6000

Počítače

1981 1982 19831984

19851986

http://www.faqs.org/rfcs/rfc1296.html

pátek, 25. března 2011

Page 20: Real-time Web a NodeJS

NAPROSTÉ ZAHLCENÍ SÍTĚ

“The critical congestion problems theARPANET is experiencing causes [...]mail messages from MILNEThosts to take up to 2-3 days to bedelivered to BBNNET hosts.”

- Nancy Cassidy in mod.risks, September 22 1986

pátek, 25. března 2011

Page 21: Real-time Web a NodeJS

TCP

“Should the round-trip timeexceed the maximum retransmissioninterval for any host, that hostwill begin to introduce more andmore copies of the same datagramsinto the net. The network is nowin serious trouble.”

http://www.faqs.org/rfcs/rfc896.html

pátek, 25. března 2011

Page 22: Real-time Web a NodeJS

TCP SLOW START

uvádí maximální počet přenášených nepotvrzených (ACK) packetů a funkci podle které se zvyšuje v čase

každé nové TCP spojení se postupně “rozjíždí”

maximální rychlosti (počet packetů) dosáhne zhruba po 9 otáčkách

http://www.faqs.org/rfcs/rfc1122.html

http://mike.bailey.net.au/2010/07/tcp-and-the-lower-bound-of-web-performance/

pátek, 25. března 2011

Page 23: Real-time Web a NodeJS

LATENCE JE ZCELA ZÁSADNÍ

http://mike.bailey.net.au/2010/07/latency-is-a-killer/

15msvs

250ms

pátek, 25. března 2011

Page 24: Real-time Web a NodeJS

LATENCE JE ZCELA ZÁSADNÍ

15msvs

250ms

pátek, 25. března 2011

Page 25: Real-time Web a NodeJS

CO S TÍM?

minimalizujte počet TCP spojení!

kombinujte JS, CSS

HTTP Keep-Alive

používejte YSlow a PageSpeed!

pátek, 25. března 2011

Page 26: Real-time Web a NodeJS

PUSH VS PULL

pátek, 25. března 2011

Page 27: Real-time Web a NodeJS

PULL

Client Server

GET /index.html

HTTP/1.1 200 OK

pátek, 25. března 2011

Page 28: Real-time Web a NodeJS

PULL

jak se dozvědět o aktualizaci?

nový email, nový tweet

pátek, 25. března 2011

Page 29: Real-time Web a NodeJS

POLLING

pátek, 25. března 2011

Page 30: Real-time Web a NodeJS

pátek, 25. března 2011

Page 31: Real-time Web a NodeJS

pátek, 25. března 2011

Page 32: Real-time Web a NodeJS

PUSHPUB-SUB, COMET, WEBSOCKET

pátek, 25. března 2011

Page 33: Real-time Web a NodeJS

PUSH

Client Server

HTTP/1.1 200 OK

pátek, 25. března 2011

Page 34: Real-time Web a NodeJS

PUSH

browsery ani servery jej nepodporují

(až na HTML5 browsery s WebSocket podporou)

existují způsoby jak nekompatibilitu obcházet

pátek, 25. března 2011

Page 35: Real-time Web a NodeJS

LONG POLLING

Client Server

GET /index.html

HTTP/1.1 200 OK

GET /index.html

pátek, 25. března 2011

Page 36: Real-time Web a NodeJS

PUSH

Our experiment shows that if we want high data coherence and high network performance, we

should choose the push approach.

http://swerl.tudelft.nl/twiki/pub/Main/TechnicalReports/TUD-SERG-2007-016.pdf

pátek, 25. března 2011

Page 37: Real-time Web a NodeJS

PUSH

However, push brings some scalability issues; the server application CPU usage is 7 times higher as in

pull. According to our results, the server starts to saturate at 350-500 users.

http://swerl.tudelft.nl/twiki/pub/Main/TechnicalReports/TUD-SERG-2007-016.pdf

pátek, 25. března 2011

Page 38: Real-time Web a NodeJS

WTF?!

pátek, 25. března 2011

Page 39: Real-time Web a NodeJS

BLOKUJÍCÍ A NEBLOKUJÍCÍ PROVOZ

pátek, 25. března 2011

Page 40: Real-time Web a NodeJS

ČÍSLA, KTERÁ BYSTE MĚLI ZNÁT

Building Software Systems at GoogleJeffrey Dean

(AdWords, AdSense, crawler, Protocol Buffers, MapReduce, BigTable)

http://www.stanford.edu/class/ee380/Abstracts/101110.html

pátek, 25. března 2011

Page 41: Real-time Web a NodeJS

ČÍSLA, KTERÁ BYSTE MĚLI ZNÁT

L1 cache reference 0.5 nsBranch mispredict 5 nsL2 cache reference 7 nsMutex lock/unlock 100 nsMain memory reference 100 nsCompress 1K bytes with Zippy 10,000 nsSend 2K bytes over 1 Gbps network 20,000 nsRead 1 MB sequentially from memory 250,000 nsRound trip within same datacenter 500,000 nsDisk seek 10,000,000 nsRead 1 MB sequentially from network 10,000,000 nsRead 1 MB sequentially from disk 30,000,000 nsSend packet CA->Netherlands->CA 150,000,000 ns

pátek, 25. března 2011

Page 42: Real-time Web a NodeJS

ČÍSLA, KTERÁ BYSTE MĚLI ZNÁT

L1 cache reference 0.5 nsBranch mispredict 5 nsL2 cache reference 7 nsMutex lock/unlock 100 nsMain memory reference 100 nsCompress 1K bytes with Zippy 10,000 nsSend 2K bytes over 1 Gbps network 20,000 nsRead 1 MB sequentially from memory 250,000 nsRound trip within same datacenter 500,000 nsDisk seek 10,000,000 nsRead 1 MB sequentially from network 10,000,000 nsRead 1 MB sequentially from disk 30,000,000 nsSend packet CA->Netherlands->CA 150,000,000 ns

pátek, 25. března 2011

Page 43: Real-time Web a NodeJS

ČÍSLA, KTERÁ BYSTE MĚLI ZNÁT

L1 cache reference 0.5 nsBranch mispredict 5 nsL2 cache reference 7 nsMutex lock/unlock 100 nsMain memory reference 100 nsCompress 1K bytes with Zippy 10,000 nsSend 2K bytes over 1 Gbps network 20,000 nsRead 1 MB sequentially from memory 250,000 nsRound trip within same datacenter 500,000 nsDisk seek 10,000,000 nsRead 1 MB sequentially from network 10,000,000 nsRead 1 MB sequentially from disk 30,000,000 nsSend packet CA->Netherlands->CA 150,000,000 ns

pátek, 25. března 2011

Page 44: Real-time Web a NodeJS

BLOKUJÍCÍ PROVOZ

<?phpvar $db = new mysqli("host", "user", "password", "database");var $result = db->mysqli_query('SELECT Name FROM City LIMIT 10');foreach ($result as $row) …?>

1

2

3

pátek, 25. března 2011

Page 45: Real-time Web a NodeJS

APACHE + PHP

Apache

Request

pátek, 25. března 2011

Page 46: Real-time Web a NodeJS

APACHE + PHP

Apache

Request

ApacheWorker

ApacheWorker

ApacheWorker

PHP PHP PHP

pátek, 25. března 2011

Page 47: Real-time Web a NodeJS

APACHE + PHP

Apache

Request

ApacheWorker

ApacheWorker

ApacheWorker

PHP PHP PHP

pátek, 25. března 2011

Page 48: Real-time Web a NodeJS

pátek, 25. března 2011

Page 49: Real-time Web a NodeJS

pátek, 25. března 2011

Page 50: Real-time Web a NodeJS

APACHE + PHP

Apache

Request

ApacheWorker

ApacheWorker

ApacheWorker

PHP PHP PHP

5-10MB 5-10MB 5-10MB

pátek, 25. března 2011

Page 51: Real-time Web a NodeJS

APACHE + PHP

Apache

Request

ApacheWorker

ApacheWorker

ApacheWorker

PHP PHP PHP

5-10MB 5-10MB 5-10MB

x 500 users = 2.5GB - 5GB

pátek, 25. března 2011

Page 52: Real-time Web a NodeJS

BLOKUJÍCÍ PROVOZ

<?phpvar $db = new mysqli("host", "user", "password", "database");var $result = $db->mysqli_query('SELECT Name FROM City LIMIT 10');foreach ($result as $row) …?>

1

2

3

pátek, 25. března 2011

Page 53: Real-time Web a NodeJS

NE-BLOKUJÍCÍ PROVOZ

client.connect();client.query('SELECT Name FROM City LIMIT 10', function(err, results) { for (var i=0; i < results.length; i++) { … };});

1

2

3

4

pátek, 25. března 2011

Page 54: Real-time Web a NodeJS

NE-BLOKUJÍCÍ PROVOZ

client.connect();client.query('SELECT Name FROM City LIMIT 10', function(err, results) { for (var i=0; i < results.length; i++) { … }; });

1

2

3

4

5

6

pátek, 25. března 2011

Page 55: Real-time Web a NodeJS

<script src="jquery.js" type="text/javascript" charset="utf-8"></script><script>$('#my-form button').click( function(e) { // process my button click });</script>

1

2

3

4

5

6

7

8

pátek, 25. března 2011

Page 56: Real-time Web a NodeJS

DEMOSwarmation, Scrumblr, WordSquared

pátek, 25. března 2011

Page 57: Real-time Web a NodeJS

NODE.JS

pátek, 25. března 2011

Page 58: Real-time Web a NodeJS

NODE.JS

runtime (není to mod_nodejs, ani node_d)

postavené na v8 (Google Chrome JS engine)

one event-loop, single-threaded

asynchronous and non-blocking

pátek, 25. března 2011

Page 59: Real-time Web a NodeJS

DEMOnode_chat

pátek, 25. března 2011

Page 60: Real-time Web a NodeJS

SOCKET.IO

funguje všude (i bez podpory WebSocket)

pokud nutno, používá long polling, +ash aj.

automatický reconnect, heartbeat

super-jednoduché použití

pátek, 25. března 2011

Page 61: Real-time Web a NodeJS

EXPRESS

npm install express

var app = express.createServer();

app.get('/', function(req, res){ res.send('Hello World');});

app.listen(3000);

pátek, 25. března 2011

Page 62: Real-time Web a NodeJS

NPM

…a přes 1400 dalších modulů

http://search.npmjs.org/

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

pátek, 25. března 2011

Page 63: Real-time Web a NodeJS

ALTERNATIVY

EventMachine (Ruby)

Twisted (Python)

Jetty (Java)

Javascript se dobře hodí

pátek, 25. března 2011

Page 64: Real-time Web a NodeJS

DÍKY ZA POZORNOST

OTÁZKY?

pátek, 25. března 2011