Scalable server component using NodeJS & ExpressJS

Post on 12-Jul-2015

357 views 0 download

Tags:

transcript

Using Node to build scalable server component

Who are we?

Andhy KoesnandarMBA – UW Foster, MS CS – Nebraska 7+ years software development @Bing, Azure and Microsoft Dynamics

Oby SumampouwMS CS – Stanford 6+ years distributed computing and engagement hacker @LinkedIn and @oracle

About Cermati

• Financial product comparison site

• location based credit card promotions

What is this talk about

Node JS

• Run JS on Server

• Single threaded

Express JS

• MVC Web Library for Node.Js

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

Node Architecture

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

I/O latency Pyramid

• L1: 3 cycles

• L2: 14 cycles

• RAM: 250 cycles

• DISK: 41,000,000 cycles

• NETWORK: 240,000,000 cycles

Apache vs nginx request / sec

Memory consumption in Apache vs nginx

What it means in code

• Traditional I/Ovar result = db.query(“select x from table_Y”);

doSomethingWith(result); //wait for result!

doSomethingWithOutResult(); //execution is blocked!

• Non-traditional, Non-blocking I/Odb.query(“select x from table_Y”,function (result){

doSomethingWith(result); //wait for result!

});

doSomethingWithOutResult(); //executes without any delay!

Express JS

• The de-facto standard for the majority of Node.js web application

• Example:

Scaling Node

• Offload to Http proxy server • Http caching Static Files

• Gzip compression

• SSL

• Application Scaling • Worker Roles

• Node clustering

• Cluster Round Robin Load Balancing

Background Tasks

• Spawn child process that can run on the background

Node Clustering

• Can take advantage of multi core machines

• Load balance across multiple processes

• Increase throughput

No Clustering With Clustering

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

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

Who uses Node

Source Code Available on GitHub

• https://github.com/akoesnan/express-cluster-tutorial

Nerds, questions?