Node.js as a solid pillar of your web/mobile app

Tomasz Banasiak

Node.js - A solid pillar for your web/mobile app.

WHo i am?

Node.js - A solid pillar for your web/mobile app.

WEBMESSENGER SERVER

TFL NOTIFICATIONS

AUTOGUARD

NODE.js IN RST

Here we go!

Node.js - A solid pillar for your web/mobile app.

TO BE OR NOT TO BE...

SYNCHRONOUS

Node.js - A solid pillar for your web/mobile app.

$db = $this->getDatabase();

$query = 'SELECT giant_field, COUNT(field) AS counter
    FROM massive_table mt
    LEFT JOIN even_bigger_table ebt ON mt.field1 = ebt.field2
    WHERE ebt.field3 = \'Zosia\'
    ORDER BY mt.date DESC
    HAVING counter > 1000';

$result = $db->query($query)->fetchAll();

Node.js - A solid pillar for your web/mobile app.

What is the software doing while it queries a database? 

NOTHING

It just waits for the response...

Node.js - A solid pillar for your web/mobile app.

Is this something to really deal about?

Node.js - A solid pillar for your web/mobile app.

What SERVER's CPU is doing

Process

request

Query

Database

Fetching

Result

Process

Response

Send 

Response

Node.js - A solid pillar for your web/mobile app.

What about THE REST?

Process

request

Query

Database

Fetching

Result

Process

Response

Send 

Response

  ?                            ?                                                  ?                                ?                      ?                 ?

Node.js - A solid pillar for your web/mobile app.

Server is waiting for the I/O

Node.js - A solid pillar for your web/mobile app.

Node.js - A solid pillar for your web/mobile app.

Node.js - A solid pillar for your web/mobile app.

EVENT LOOP CPU UTILIZATION

  • JuSt small gaps for context switching

  • One thread, many consequent requests

  • Separated IO operations

 

Node.js - A solid pillar for your web/mobile app.

var mysql = require('mysql');
var db = mysql.connection('user:pass@localhost:3306/nodetest1');

var sql = 'SELECT giant_field, COUNT(field) AS counter
    FROM massive_table mt
    LEFT JOIN even_bigger_table ebt ON mt.field1 = ebt.field2
    WHERE ebt.field3 = \'Zosia\'
    ORDER BY mt.date DESC
    HAVING counter > 1000';

db.query(sql, function(data) {
    response.setData(data);
    response.send();
}

Node.js - A solid pillar for your web/mobile app.

PROBLEM?

Node.js - A solid pillar for your web/mobile app.

app.get('/user/:userId', function(req, res, next) {
    db.get('users', userId, function(err, user) {
        /** code **/
        callbackCounter++;
        locals.user = user;
        if (callbackCounter == 2) {
            res.render('user-profile', locals);
        }
    });
    db.query('posts', {userId: userId}, function(err, posts) {
        /** code **/
        locals.posts = posts;
        callbackCounter++;
        if (callbackCounter == 2) {
            res.render('user-profile', locals);
        }
    });
});

Node.js - A solid pillar for your web/mobile app.

ASYNC.js

Node.js - A solid pillar for your web/mobile app.

app.get('/user/:userId', function(req, res, next) {
  async.parallel([
    function(callback) {
      db.get('users', userId, function(err, user) {
          locals.user = user;
          callback();
        });
    },
    function(callback) {
      db.query('posts', {userId: userId}, function(err, posts) {
         locals.posts = posts;
         callback();
      });
    }
 ], function(err) { 
      if (err) return next(err); 
      res.render('user-profile', locals);
   });
});

NODE.jS VS.  OTHERS

Node.js - A solid pillar for your web/mobile app.

One request memory usage

Tomcat 
IIS
Apache
lighttpd
nginx
Node.js

198 MB
155 MB
50 MB
18 MB
8 MB
3 MB

Source: jsconf.eu/2014

Node.js - A solid pillar for your web/mobile app.

Node.js vs Apache2

Node.js - A solid pillar for your web/mobile app.

Node.js vs Apache2

Node.js - A solid pillar for your web/mobile app.

IS NODE.js PERFECT SOLUTION FOR ALL PROBLEMS?

NO.

Node.js - A solid pillar for your web/mobile app.

NODE.js IS PERFECT SOLUTION FOR:

  • One-to-many notifications

  • Middleware, eg. for socket.io

  • SPA application backend

  • Lots of short, non CPU-heavy tasks

  • Event bus

Node.js - A solid pillar for your web/mobile app.

NODE.js IS Nice SOLUTION FOR:

  • Lightweight backend for mobile apps

  • embedded software for raspberry pi

  • arduino controller backend

  • every kind of chat applications

  • server processing monitor

  • STANDALONE APPS :) 

Node.js - A solid pillar for your web/mobile app.

NODE.js IS WRONG SOLUTION FOR:

  • Handle mainly all backend services

  • Uplodad/serve large files (but they're about to fix it)

  • loadbalanced services

  • CDN services

  • Computation services

Node.js - A solid pillar for your web/mobile app.

NODE.js IS STILL A REALLY YOUNG TECHNOLOGY

  • Created in 2009

  • First "usable" release in 2011

Node.js - A solid pillar for your web/mobile app.

NODE.js IS FUTURE TECHNOLOGY

  • JAVASCRIPT IS A RISING POWER

  • NATIVE OPERATION ON ANY DEVICE

  • LOW LEARNING CURVE

Node.js - A solid pillar for your web/mobile app.

NODE.js IS FUTURE TECHNOLOGY

  • Java            +4,2%

  • C#              +3,4%

  • .NET             +10,1%

  • PHP              +8,8%

  • Node.js/JS +21,6% 

  • Android     +12,4%

careers.stackoverflow.com

Node.js - A solid pillar for your web/mobile app.

NODE.js IS FUTURE TECHNOLOGY

http://adambard.com/blog/top-github-languages-2014/

Node.js - A solid pillar for your web/mobile app.

NODE.js SUCCESS STORIES

Node.js - A solid pillar for your web/mobile app.

PayPal

https://www.paypal-engineering.com/2013/11/22/node-js-at-paypal/

Build a Node.js version of their Java-based app in half the time with fewer developers

  • Written in 33% fewer lines of code

  • Constructed with 40% fewer files

Node.js - A solid pillar for your web/mobile app.

PayPal

https://www.paypal-engineering.com/2013/11/22/node-js-at-paypal/

Node.js - A solid pillar for your web/mobile app.

PayPal

https://www.paypal-engineering.com/2013/11/22/node-js-at-paypal/

  • Double the requests per second vs. the Java application

  • 35% decrease in the average response time

  • FROM 1 TO 12 Node apps IN six months

Node.js - A solid pillar for your web/mobile app.

PayPal

"We are moving every product & every site within PayPal to Node. We started the journey by using it as prototyping framework, then at start of this year we hardened it to work with all PayPal systems. We have several of our beta products out live on NodeJS and half dozen other products in flight. By end of 2014 we hope to have all major experiences redesigned, and rewritten on nodejs."

Bill Scott, nov 2013

Node.js - A solid pillar for your web/mobile app.

GROUPON

https://engineering.groupon.com/2014/misc/shopping-cart/

  • EASILY HANDLING 50K REQS/MIN WITH HALF OF USED HARDWARE

  • 45% decrease in the average response time

  • WHOLE SHOPPING PROCESS HANDLED BY Node.js

Node.js - A solid pillar for your web/mobile app.

WALLMART

http://thechangelog.com/116/

  • 55% of all traffic  on black friday went to Node.js servers 

  • Not a single Node server went down. Zero.

  • cpu loads hovered around 1-2%

  • Developers were so bored that day :)

Node.js - A solid pillar for your web/mobile app.

STILL NOT CONVINCED?

https://github.com/joyent/node/wiki/Projects,-Applications,-and-Companies-Using-Node

Node.js - A solid pillar for your web/mobile app.

THE DARK SIDE

Node.js - A solid pillar for your web/mobile app.

PAINFUL DEBUGGING

Node.js - A solid pillar for your web/mobile app.

PAINFUL DEBUGGING

Walmart Node.js Memory Leak

http://www.joyent.com/blog/walmart-node-js-memory-leak

Node.js - A solid pillar for your web/mobile app.

THE GIANT SPLIT

Node.js

IO.js

Node.js - A solid pillar for your web/mobile app.

There's an alternative!

Python + twisted

RUBY + EVENTMACHINE

ESB WSO2

BUT THIS IS STILL JUST A MIDDLEWARE..

VERTX JVM

Node.js - A solid pillar for your web/mobile app.

There's an alternative!

GO

Node.js - A solid pillar for your web/mobile app.

There's an alternative!

import ( "io/db", "fmt", "http" )
func main() {
    err, user := db.get('users', userId)
    err, posts := db.query('posts', 
        fmt.Concat('user_id = ',userId)
    )
    http.response := render(user, posts)
    http.send()
    http.close()
}
    

Node.js - A solid pillar for your web/mobile app.

There's an alternative!

Python + twisted

RUBY + EVENT MACHINE

ESB WSO2

BUT THIS IS STILL JUST A MIDDLEWARE..

VERTX JVM

Node.js - A solid pillar for your web/mobile app.

ESB WSO2 VS NODE.jS

  • Powerful

  • Elastic

  • Universal

  • HEAVY

  • java

  • DO WE NEED THIS?

  • Powerful

  • Elastic

  • JSON

  • LIGHTWEIGHT

  • NPM PACKAGES

  • MODERN TECHNOLOGY

  • BUT IMMATURE

Node.js - A solid pillar for your web/mobile app.

ESB WSO2 VS NODE.jS

Microsoft Azure uses NODE.js as ESB

NODE WORKS PERFECTLY WITH AMQP

MAYBE ITS TIME TO RETHINK OUR direction

BEFORE WE BUILD A...

SWARM ESB

Node.js - A solid pillar for your web/mobile app.

Node.js - A solid pillar for your web/mobile app.

Questions?

Thank you!

Tomasz Banasiak

Email:  tomasz.banasiak@rst.com.pl

WEB:     http://banasiak.pro

Node.js as a solid pillar of your web/mobile app

By Tomasz Banasiak

Node.js as a solid pillar of your web/mobile app

  • 1,104