This 15 minute presentation discusses non-blocking I/O, event loops, and Node.js. It builds on previous work by Ryan Dahl, explaining how threads can be expensive due to context switching and memory usage, and how Node.js uses an event-driven, non-blocking model to avoid these costs. Code examples demonstrate getting and printing a policy object, handling HTTP requests asynchronously without blocking additional connections, and using callbacks to chain asynchronous actions together.
1. This is a 15 minute presentation I did
at work recently about threads,
polling and node.js.
It rests heavily on the shoulders of
giants, especially Ryan Dahls original
node.js talk at jsconf.eu.
10. HTTP request latency; glibc 2.3.2 on Linux 2.6
HTTP-anropslatens; glibc 2.3.2 på Linux 2.6
1 thread per call
1 process per call
http://bulk.fefe.de/scalable-networking.pdf
Open connections
11. HTTP request latency; glibc 2.3.2 on Linux 2.6
1 thread per call
1 process per call
Which is the reason why we use thread pools!
Open connections
27. The weight of I/O
L1-cache A big squirrel
L2-cache A medium-sized cat
28. The weight of I/O
L1-cache A big squirrel
L2-cache A medium-sized cat
RAM Mattias, basically
29. The weight of I/O
L1-cache A big squirrel
L2-cache A medium-sized cat
RAM Mattias, basically
Disk Like a 100 blue whales
30. The weight of I/O
L1-cache A big squirrel
L2-cache A medium-sized cat
RAM Mattias, basically
Disk Like a 100 blue whales
Network Belarus yearly
wheat import
35. cmServer.getPolicy(function (Policy p) {
System.out.println(
Sover tills dess p finns
p.getContentId().toString()
);
});
// And down here we can do other stuff
42. % node hello.js
% ab -n 1000 -c 200 http://127.0.0.1:8080/
...
Time per request: 2010.070 [ms] (mean)
min mean[+/-sd] median max
Connect: 0 2 1.9 1 8
Processing: 2001 2006 2.8 2005 2013
Waiting: 2000 2003 2.0 2003 2009
Total: 2002 2008 3.5 2006 2015
Percentage of the requests served within a certain time (ms)
50% 2006
66% 2008
75% 2012
80% 2013
90% 2013
95% 2014
98% 2014
99% 2014
100% 2015 (longest request)
43. var stat = require('posix').stat,
puts = require('sys').puts;
var promise = stat('/etc/passwd');
promise.addCallback(function (s) {
puts('modified: ' + s.mtime);
});
45. % node web.test.js
% ab -n 10000 -c 250 http://127.0.0.1:8080/marcus
...
Requests per second: 4851.93 [#/sec] (mean)
Time per request: 51.526 [ms] (mean)
Time per request: 0.206 [ms] (mean, across all c requests)
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 14 111.8 1 999
Processing: 4 35 15.8 31 103
Waiting: 4 35 15.8 30 103
Total: 14 49 112.5 32 1035
Percentage of the requests served within a certain time (ms)
50% 32
66% 34
75% 35
80% 36
90% 57
95% 92
98% 102
99% 986
100% 1035 (longest request)