Node.js is a server-side JavaScript runtime built on Google's V8 engine. It uses non-blocking I/O and an event loop to handle concurrent connections efficiently without threads. This asynchronous model improves performance compared to traditional blocking I/O and threads. The event loop iterates through callbacks and executes them in order, allowing Node.js to handle many concurrent connections with a single thread.
Node.js is a server-side JavaScript runtime based on Google V8. It supports CommonJS modules, asynchronous non-blocking I/O, enabling highly concurrent programming.
Discussion on blocking vs non-blocking I/O approaches, illustrating costs such as 3 cycles for L1 to 41,000,000 cycles for DISK, illustrating performance nuances.
Explores the challenges of using threads in programming including difficulty, shared state, deadlocks, and race conditions that complicate concurrency.
Identifies scenarios where threads are beneficial, especially for multi-core CPUs, CPU-heavy tasks, and low shared state—optimizing performance.
Introduces the Event Loop's lifecycle, its operations including iterating events and handling non-blocking I/O, all managed within a single thread.
Highlights limitations of the Event Loop requiring fast callbacks, avoiding CPU-heavy work, and addresses multi-core CPUs via Web Workers and message passing.
Demonstrates a simple HTTP server example in Node.js that responds with 'Hello World' after a timeout, illustrating asynchronous behavior.
Discusses the sleep() function as a blocking call, highlighting JavaScript's non-blocking nature, and the implications of emulating sleep with loops.
Provides additional resources for learning Node.js, including links to GitHub repositories, forums, and other educational websites.
An indication of a forthcoming demo related to the previously discussed topics.
node.js
• Server-side Javascriptruntime
• Google V8
• CommonJS modules
• API for highly concurrent programming
• All I/O is non-blocking (asynchronous)
• Achieved using Event Loop
5.
Two approaches toI/O
// blocking I/O + threads
var urls = db.query("select * from urls"); // wait
urls.each(function (url) {
var page = http.get(url); // wait
save(page); // wait
});
// non-blocking I/O + event loop
db.query("select * from urls", function (urls) {
urls.each(function (url) {
http.get(url, function (page) {
save(page);
});
});
});
When threads aregood
• Support Multi-core CPUs
• CPU-heavy work
• Little or no shared state
26.
When threads aregood
• Support Multi-core CPUs
• CPU-heavy work
• Little or no shared state
• Threads count == CPU cores count
27.
Event Loop
userperspective
• You already use it everyday for I/O
• You register callbacks for events
• Your callback is eventually fired
$("a").click(function () {
console.log("clicked!");
});
$.ajax(..., function (...) {
console.log("internets!");
});
Event Loop
life cycle
• Initialize empty event loop (just an array)
30.
Event Loop
life cycle
• Initialize empty event loop (just an array)
• Read and “execute” code
31.
Event Loop
life cycle
• Initialize empty event loop (just an array)
• Read and “execute” code
• Execute non-I/O code
32.
Event Loop
life cycle
• Initialize empty event loop (just an array)
• Read and “execute” code
• Execute non-I/O code
• Add every I/O call to the event loop
33.
Event Loop
life cycle
• Initialize empty event loop (just an array)
• Read and “execute” code
• Execute non-I/O code
• Add every I/O call to the event loop
• End of source code reached
34.
Event Loop
life cycle
• Initialize empty event loop (just an array)
• Read and “execute” code
• Execute non-I/O code
• Add every I/O call to the event loop
• End of source code reached
• Event loop starts iterating
35.
Event Loop
life cycle
• Initialize empty event loop (just an array)
• Read and “execute” code
• Execute non-I/O code
• Add every I/O call to the event loop
• End of source code reached
• Event loop starts iterating
• All this happens in a single thread
Event Loop
life cycle
• Iterate over a list of events and callbacks
38.
Event Loop
life cycle
• Iterate over a list of events and callbacks
• Perform I/O using non-blocking kernel
facilities: epoll, kqueue, /dev/poll, select
39.
Event Loop
life cycle
• Iterate over a list of events and callbacks
• Perform I/O using non-blocking kernel
facilities: epoll, kqueue, /dev/poll, select
• Event Loop goes to sleep
40.
Event Loop
life cycle
• Iterate over a list of events and callbacks
• Perform I/O using non-blocking kernel
facilities: epoll, kqueue, /dev/poll, select
• Event Loop goes to sleep
• Kernel notifies the Event Loop
41.
Event Loop
life cycle
• Iterate over a list of events and callbacks
• Perform I/O using non-blocking kernel
facilities: epoll, kqueue, /dev/poll, select
• Event Loop goes to sleep
• Kernel notifies the Event Loop
• Event Loop executes and removes a callback
42.
Event Loop
life cycle
• Iterate over a list of events and callbacks
• Perform I/O using non-blocking kernel
facilities: epoll, kqueue, /dev/poll, select
• Event Loop goes to sleep
• Kernel notifies the Event Loop
• Event Loop executes and removes a callback
• Program exits when Event Loop is empty
Event Loop limitations
•Callbacks have to return fast
• No CPU-heavy stuff
• What about multi-core CPUs
47.
Event Loop limitations
•Callbacks have to return fast
• No CPU-heavy stuff
• What about multi-core CPUs
• Web Workers using separate processes
48.
Event Loop limitations
•Callbacks have to return fast
• No CPU-heavy stuff
• What about multi-core CPUs
• Web Workers using separate processes
• Can do CPU-heavy stuff
49.
Event Loop limitations
•Callbacks have to return fast
• No CPU-heavy stuff
• What about multi-core CPUs
• Web Workers using separate processes
• Can do CPU-heavy stuff
• No shared memory - no locks
50.
Event Loop limitations
•Callbacks have to return fast
• No CPU-heavy stuff
• What about multi-core CPUs
• Web Workers using separate processes
• Can do CPU-heavy stuff
• No shared memory - no locks
• Communication using message passing
51.
Hello World
var sys= require('sys'),
http = require('http');
http.createServer(function (req, res) {
setTimeout(function () {
res.sendHeader(200, {'Content-Type': 'text/plain'});
res.write('Hello World');
res.close();
}, 2000);
}).listen(8000);
sys.puts('Server running at http://127.0.0.1:8000/');
sleep()
• It isa blocking call per definition
• Browser JS has no blocking functions
55.
sleep()
• It isa blocking call per definition
• Browser JS has no blocking functions
• Except synchronous AJAX which is
supposed to be used in “unload” event
56.
sleep()
• It isa blocking call per definition
• Browser JS has no blocking functions
• Except synchronous AJAX which is
supposed to be used in “unload” event
• You can emulate sleep() using a “while”
loop and checking wall clock but what’s the
point...
57.
sleep()
• It isa blocking call per definition
• Browser JS has no blocking functions
• Except synchronous AJAX which is
supposed to be used in “unload” event
• You can emulate sleep() using a “while”
loop and checking wall clock but what’s the
point...
• The real reason: JavaScript exceution and
Event Loop live in a single thread
58.
Where to gonext
• github.com/ry/node
• groups.google.com/group/nodejs
• howtonode.org
• dailyjs.org
• search twitter for node.js
• github.com/ncr/flickr_spy