5. Event loop var http = require('http');
http.createServer(function(req, res) {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('Hello');
}).listen(8888);
wrk2 -R 50000 -d 15s -t 4 -c 20 http://localhost:8888/
Running 15s test @ http://localhost:8888/
4 threads and 20 connections
Thread calibration: mean lat.: 3809.653ms, rate sampling interval: 13459ms
Thread calibration: mean lat.: 3809.566ms, rate sampling interval: 13467ms
Thread calibration: mean lat.: 3808.688ms, rate sampling interval: 13459ms
Thread calibration: mean lat.: 3810.143ms, rate sampling interval: 13459ms
Thread Stats Avg Stdev Max +/- Stdev
Latency 8.90s 953.41ms 10.75s 60.16%
Req/Sec nan nan 0.00 0.00%
212176 requests in 15.00s, 30.15MB read
Requests/sec: 14145.13
Transfer/sec: 2.01MB
2013 MacBook Pro Retina with a
2.4 GHz Intel Core i5 and 8 GB
1600 MHz DDR3
6. Event loop
HTTP pipelining
HTTP/1.1 assumes every connection is
persistent — i.e. both ends should keep
the TCP connection open — until one of
the parties sends a Connection: close
header. But even though the connection
is kept open, the client should still wait
for the server response before sending
in another request.
7. Architecture
V8 thread pool event loop
libuv
node bindings
node standard library
node execution stack
event loop
socket readable
http parse
load(index.html)
javascript
C
8. Load balancer wrk -d 30s -t 12 -c 5000
12997 mysite 20 0 657m 39m 5556 R 109 1.0 0:22.44
12998 mysite 20 0 656m 38m 5556 S 55 1.0 0:09.85
12995 mysite 20 0 656m 37m 5564 R 53 1.0 0:09.84
12994 mysite 20 0 656m 38m 5556 R 51 1.0 0:09.40
The numbers 109, 55, 53 and 51 represent the CPU usage here
1018 mysite 20 0 659m 58m 5576 R 68 1.5 0:04.60
1023 mysite 20 0 659m 58m 5576 R 68 1.5 0:05.00
1024 mysite 20 0 659m 58m 5576 S 62 1.5 0:05.00
1030 mysite 20 0 659m 58m 5576 S 56 1.5 0:04.28
NGINX NODE
15050 mysite 20 0 658m 55m 5572 S 38 1.4 1:31.88
15063 mysite 20 0 659m 56m 5568 S 38 1.4 1:33.23
15058 mysite 20 0 657m 55m 5568 R 37 1.4 1:30.06
15071 mysite 20 0 658m 55m 5568 S 37 1.4 1:30.41
NODEHAPROXY
9. Cluster
When using the cluster module in your application, your
application is free to spin up as many workers as necessary to
accommodate the load your application will receive. Though
generally it is suggested to match the number of workers to the
number of threads or logical cores your environment has
available to it.
The logic that encompasses your master process can be as
simple or as complex as you want it to be, at the very least it
will need to be able to spin up your workers when needed. It is
ideal to make sure your master is keeping track if workers exit
and if it's necessary to create new workers.
The logic that makes up a worker is dependent on your
application, but any socket opened in a worker will be shared
among all workers in the pool. The design is such that you can
write your logic once and spin up as many workers without
having to worry about the synchronization of sockets on your
own
A downside of this single-threaded approach is that Node.js doesn't
allow vertical scaling by increasing the number of CPU cores of the
machine it is running on without using an additional module, such as
cluster
const cluster = require('cluster');
const http = require('http');
const numCPUs = require('os').cpus().length;
if (cluster.isMaster) {
// Fork workers.
for (var i = 0; i < numCPUs; i++) {
cluster.fork();
}
cluster.on('exit', function(worker, code, signal) {
console.log(‘worker ${worker.process.pid} died’);
});
} else {
// Workers can share any TCP connection
// In this case it is an HTTP server
http.createServer(function(req, res) {
res.writeHead(200);
res.end('hello worldn');
}).listen(8000);
}
10. Node.js Application Area
Node.js can be used to create following
types of applications.
1. E-Commerce Web Applications
2. Social Media Applications
3. Proxy Server
4. Real-time Services
5. Real-time data Applications like
Multiplayer Games, Stock Trading,
Chat App etc.
6. Data Streaming Applications
7. Network Applications
8. High Concurrency Applications
9. File Uploading Tools
10. Process Monitoring Tools
11. HTTP Web Server
Who use Node.js
1. Walmart
2. E-bay
3. PayPal
4. Microsoft
5. LinkedIn
6. Yahoo
7. Google
8. SAP
9. IBM
10. Strong Loop
11. Dropbox