Real-time Web a NodeJS

Post on 12-Jan-2015

1,368 views 2 download

description

 

transcript

REALTIME WEB A NODE.JS realtime servery snadno a rychle

Jakub Nešetřil@jakubnesetril

pátek, 25. března 2011

REALTIME WEB

co je realtime?

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

zhruba 100ms

pátek, 25. března 2011

REALTIME WEB

Na rychlosti záleží!

Google: +500ms => -20% traffic

Amazon: +100ms => -1% sales

pátek, 25. března 2011

OBSAH

Latence

Push vs. Pull

Blokující vs. neblokující provoz

Node.js, kód

pátek, 25. března 2011

LATENCE

pátek, 25. března 2011

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

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

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

TCP

Client Server

pátek, 25. března 2011

TCP

Client Server

GET /index.html

pátek, 25. března 2011

TCP

Client Server

GET /index.html

HTTP/1.1 200 OK

pátek, 25. března 2011

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

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

ROK 1981

213 počítačů

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

pátek, 25. března 2011

1986

pátek, 25. března 2011

LEDEN 1986VÝBUCH CHALLENGERU

pátek, 25. března 2011

DUBEN 1986VÝBUCH ČERNOBYLU

pátek, 25. března 2011

ŘÍJEN 1986VÝBUCH INTERNETU

pátek, 25. března 2011

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

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

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

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

LATENCE JE ZCELA ZÁSADNÍ

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

15msvs

250ms

pátek, 25. března 2011

LATENCE JE ZCELA ZÁSADNÍ

15msvs

250ms

pátek, 25. března 2011

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

PUSH VS PULL

pátek, 25. března 2011

PULL

Client Server

GET /index.html

HTTP/1.1 200 OK

pátek, 25. března 2011

PULL

jak se dozvědět o aktualizaci?

nový email, nový tweet

pátek, 25. března 2011

POLLING

pátek, 25. března 2011

pátek, 25. března 2011

pátek, 25. března 2011

PUSHPUB-SUB, COMET, WEBSOCKET

pátek, 25. března 2011

PUSH

Client Server

HTTP/1.1 200 OK

pátek, 25. března 2011

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

LONG POLLING

Client Server

GET /index.html

HTTP/1.1 200 OK

GET /index.html

pátek, 25. března 2011

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

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

WTF?!

pátek, 25. března 2011

BLOKUJÍCÍ A NEBLOKUJÍCÍ PROVOZ

pátek, 25. března 2011

ČÍ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

ČÍ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

ČÍ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

ČÍ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

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

APACHE + PHP

Apache

Request

pátek, 25. března 2011

APACHE + PHP

Apache

Request

ApacheWorker

ApacheWorker

ApacheWorker

PHP PHP PHP

pátek, 25. března 2011

APACHE + PHP

Apache

Request

ApacheWorker

ApacheWorker

ApacheWorker

PHP PHP PHP

pátek, 25. března 2011

pátek, 25. března 2011

pátek, 25. března 2011

APACHE + PHP

Apache

Request

ApacheWorker

ApacheWorker

ApacheWorker

PHP PHP PHP

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

pátek, 25. března 2011

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

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

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

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

<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

DEMOSwarmation, Scrumblr, WordSquared

pátek, 25. března 2011

NODE.JS

pátek, 25. března 2011

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

DEMOnode_chat

pátek, 25. března 2011

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

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

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

ALTERNATIVY

EventMachine (Ruby)

Twisted (Python)

Jetty (Java)

Javascript se dobře hodí

pátek, 25. března 2011

DÍKY ZA POZORNOST

OTÁZKY?

pátek, 25. března 2011