4. How software lives in hardware?
• Operating systems are process based
• Each process has assigned
processor, registers, memory
http://www.cs.uic.edu/~jbell/CourseNotes/OperatingSystems/4_Threads.html
5. How software lives in hardware?
• Process paralelism using
threads (thread pools)
• Switching processor over
processes/threads causes
context switching
http://www.cs.uic.edu/~jbell/CourseNotes/OperatingSystems/4_Threads.html
6. How software lives in hardware?
1. Avoid context switching = wasting time
7. How software lives in hardware?
In trivial, sequential approach
• Each operation is executed
sequentially:
O(t) > O(t+1)
• if O(t) stucks, O(t+1) waits…
http://cs.brown.edu/courses/cs196-5/f12/handouts/async.pdf
8. How software lives in hardware?
This is cool, software flow is predictible
But not in high throughput I/O
10. How software lives in hardware?
High throughput I/O doesn’t mean:
• Memory operations
• Fast single-thread computing
11. How software lives in hardware?
High throughput I/O means:
• HTTP requests
• Database connections
• Queue system dispatching
• HDD operations
12. Single-threaded, event loop model
Problem:
Imagine a man, who has a task:
1. Walk around
2. When bucket is full of water,
just pour another bucket
3. Go to next bucket
http://www.nightmare.com/medusa/async_sockets.html
13. Single-threaded, event loop model
What is nonblocking I/O?
Imagine a man, who has a task:
1. Walk around
2. When bucket is full of water,
just pour another bucket
3. Go to next bucket
http://www.nightmare.com/medusa/async_sockets.html
14. Single-threaded, event loop model
Problem:
Imagine a man, who has a task:
1. Walk around
2. When bucket is full of water,
just pour another bucket,
if not… continue
3. Go to next bucket
http://www.nightmare.com/medusa/async_sockets.html
15. Single-threaded, event loop model
How it is realised in low-level operating system?
• select()
• /dev/pool descriptors
• kqueue
• pool
• epool
16. Single-threaded, event loop model
How it is realised in low-level operating system?
#include <sys/types.h>
#include <sys/socket.h>
int select(int nfds, fd_set *readfds, fd_set *writefds,
fd_set *exceptfds, const struct timeval *timeout);
24. Node.js architecture
• Everything runs in parallel except your code
• When currently code is running,
(not waiting for I/O descriptors)
whole event loop is blocked
25. „Parallelism”
• Let’s compute Fibonacci
function fib(n) {
return (n < 2) ? 1 : (fib(n-2)+fib(n-1));
}
This will simply block main (single) thread.
27. „Parallelism”
• … but next iteration over function is delayed into next
event loop iteration
• That means each descriptor is checked before
computation.
29. „Parallelism”
You can still fork another process
• threads_a_gogo
pool = require('threads_a_gogo').createPool(5)
pool.any.eval('myFunction( ... )')
• Fork process
var fork = require('child_process').fork;
var child = fork(__filename, [ 'arg1' ]);
31. Callback hell
• Language construction – callback function
• When operation is ready, call function passed as an
argument
someOperation(arg1, arg2, function() {
console.log('cool!');
})
41. Events
var eventbus = require('events').EventEmitter
eventbus.on('myFunctionIsDone', function(data) {
console.log('cool! ')
})
Trigger waits until all listeners are done. So do not
make long-running operations in event subscribers.
44. Scaling EventEmitter
We need to use external queue system
to touch distributed nodes
(3rd party for our application written in Node.js)
45. Scaling EventEmitter
RabbitMQ is cool because:
• based on AMQP protocol
(so integrates 3rd party software – an abstraction)
• Queue async system
• Publish-subscribe async model
• Request-Response model
49. Not only Node.js
Node.js is not a religion! It is only an implementation of async programming model
50. Not only Node.js
Node.js is not a religion. Last time - hype.
It only utilizes event-loop model, known from a long time…
• GUI frameworks
• Tornado – Python
• Linkedin parseq – Java
• Async and Await – C#
• Cramp – Ruby
• reactphp – PHP, ehhh… WAT?!
51. Event loop, single-threaded
model is not solution for all
your problems…
Especially for long-running computings in single thread.
It’s good for high throughput I/O.