This document discusses using Node to build scalable server components. It introduces Node and Express JS, explaining that Node allows running JavaScript on the server and is lightweight and fast. It describes Node's single-threaded architecture but how it handles concurrency using an event loop. It discusses scaling Node through clustering, load balancing, and offloading tasks. The document provides an example Express app and recommends Node may not be best for CPU-intensive tasks or if relying heavily on relational databases. Source code is available on GitHub for further reference.
2. Who are we?
Andhy Koesnandar
MBA – UW Foster, MS CS – Nebraska
7+ years software development @Bing,
Azure and Microsoft Dynamics
Oby Sumampouw
MS CS – Stanford
6+ years distributed computing and
engagement hacker @LinkedIn and @oracle
4. What is this talk about
Node JS
• Run JS on Server
• Single threaded
Express JS
• MVC Web Library for Node.Js
5. Why should you care
• Its Javascript and this is JakartaJS
• Javascript is fun
• Uses unified language across the stack
• Its lightweight and its super fast
“Ruby is like a minivan, with all the features and Node is like a Ferrari –
crazy efficient and fast”
Sri Viswanath, SVP Engineering Groupon
7. Its Single Thread app…
• Enter mighty Event Loop (libuv)
Client
Event loop
(main thread)
C++
Threadpool
(worker
threads)
Clients send HTTP requests
to Node.js server
An Event-loop is woken up by OS,
passes request and response objects
to the thread-pool
Long-running jobs run
on worker threads
13. What it means in code
• Traditional I/O
var result = db.query(“select x from table_Y”);
doSomethingWith(result); //wait for result!
doSomethingWithOutResult(); //execution is blocked!
• Non-traditional, Non-blocking I/O
db.query(“select x from table_Y”,function (result){
doSomethingWith(result); //wait for result!
});
doSomethingWithOutResult(); //executes without any delay!
14. Express JS
• The de-facto standard for the majority of Node.js web application
• Example:
22. Node Clustering Round Robin
• Algorithm to choose in 0.10 is doesn’t let everyone to party
• Choose the process in round robin fashion
• New in 0.12
• That way everyone gets to party
23. When not to use Node?
• CPU Hungry applications. Not much I/O
• Video transcoding
• Encryption software
• AI and Machine learning
• You use relational DB
• There is no mature ORM yet for Node
• Current popular packages:
• Sequelize
• Node ORM2
Node.js uses an event-driven, non-blocking I/O model, which makes it lightweight. (from nodejs.org!)
It makes use of event-loops via JavaScript’s callback functionality to implement the non-blocking I/O.
Programs for Node.js are written in JavaScript but not in the same JavaScript we are use to. There is no DOM implementation provided by Node.js, i.e. you can not do this:
var element = document.getElementById(“elementId”);
Everything inside Node.js runs in a single-thread.
EventMachine (Ruby)
or Python’s Twisted
or Perl’s AnyEvent framework
Better software can multitask.
Other threads of execution can run
while waiting.