26
Using Node to build scalable server component

Scalable server component using NodeJS & ExpressJS

Embed Size (px)

Citation preview

Page 1: Scalable server component using NodeJS & ExpressJS

Using Node to build scalable server component

Page 2: Scalable server component using NodeJS & ExpressJS

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

Page 3: Scalable server component using NodeJS & ExpressJS

About Cermati

• Financial product comparison site

• location based credit card promotions

Page 4: Scalable server component using NodeJS & ExpressJS

What is this talk about

Node JS

• Run JS on Server

• Single threaded

Express JS

• MVC Web Library for Node.Js

Page 5: Scalable server component using NodeJS & ExpressJS

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

Page 6: Scalable server component using NodeJS & ExpressJS

Node Architecture

Page 7: Scalable server component using NodeJS & ExpressJS

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

Page 8: Scalable server component using NodeJS & ExpressJS

I/O latency Pyramid

• L1: 3 cycles

• L2: 14 cycles

• RAM: 250 cycles

• DISK: 41,000,000 cycles

• NETWORK: 240,000,000 cycles

Page 9: Scalable server component using NodeJS & ExpressJS

Apache vs nginx request / sec

Page 10: Scalable server component using NodeJS & ExpressJS

Memory consumption in Apache vs nginx

Page 11: Scalable server component using NodeJS & ExpressJS
Page 12: Scalable server component using NodeJS & ExpressJS
Page 13: Scalable server component using NodeJS & ExpressJS

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!

Page 14: Scalable server component using NodeJS & ExpressJS

Express JS

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

• Example:

Page 15: Scalable server component using NodeJS & ExpressJS

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

Page 16: Scalable server component using NodeJS & ExpressJS

Background Tasks

• Spawn child process that can run on the background

Page 17: Scalable server component using NodeJS & ExpressJS

Node Clustering

• Can take advantage of multi core machines

• Load balance across multiple processes

• Increase throughput

Page 18: Scalable server component using NodeJS & ExpressJS
Page 19: Scalable server component using NodeJS & ExpressJS
Page 20: Scalable server component using NodeJS & ExpressJS

No Clustering With Clustering

Page 21: Scalable server component using NodeJS & ExpressJS
Page 22: Scalable server component using NodeJS & ExpressJS

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

Page 23: Scalable server component using NodeJS & ExpressJS

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

Page 24: Scalable server component using NodeJS & ExpressJS

Who uses Node

Page 25: Scalable server component using NodeJS & ExpressJS

Source Code Available on GitHub

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

Page 26: Scalable server component using NodeJS & ExpressJS

Nerds, questions?