- Software Engineer at ilegra
- More than 10 years working with Software development (1 year with node.js)
- tv shows addicted and a video game player!!
Jéferson Machado
ABOUT ME
BLOCKING IO
READS ARE BLOCKING:- when waiting for a read, a thread is completely idle- writing blocking too, but is not typically significant- idle connections consume memory
SERVER
handle data from A
handle data from A
handle data from B
handle data from C
Connection B
Thread
Thread
Thread
Connection A
Connection C
Idle time
WHY WE NEED TO CARE ABOUT IT ?
SIMPLE MATH
Assuming:- each thread with 2MB- running with 8G of RAM
==
Theoretical maximum of 4000 concurrent connections + cost of
context-switching between threads
handle data from A
handle data from A
handle data from B
handle data from C
Thread
Thread
Thread
Idle time
SERVER handle data from A
Connection BThread
Idle timeConnection A
Connection C
NON-BLOCKING IO
- Event notification when I/O is ready
- Save memory reducing idle time
handle data from C
handle data from B
THE REACTOR PATTERN
EVENT LOOP
Event Demultiplexer
OperationResource Handler
OperationResource Handler
Event Queue
Handler
Event Handler
Event
Event Handler
Application
RequestI/O
Execute Handler
2
6 3
4
5a
1
5b
THE RECIPE FOR NODE.JS
Node.js
Core Javascript API (node-core)
Bindings
libuvV8
Userland modules and applications
ADVANTAGES AND DISADVANTAGES
Advantages
● NPM (huge base of libraries)
● Good to deal with I/O
● Use of javascript language
● Same code for both client and server*
● There is no race conditions issues and multiple
threads to synchronize
Disadvantages
● Callback hell (promises helps a little bit)
● Difficult to find developers with experience
WHEN TO USE/ NOT USE?
When to use
● Real-time / multi-user applications
● Dashboard systems
● Data Streaming
When not use
● Server side application with Relational DB
● Heavy server side computation / processing
FINAL CONSIDERATIONS
● Don't forget about architecture
● Change your programming mindset
● Check project trade-offs