Scaling Nodejs



presented by

ryan jarvinen / @ryanj
Open Platform Evangelist
Red Hat


  1. Cloud Basics and OpenShift
  2. Horizontally scalable application architecture
  3. Spin up a new app from source
  4. Configure OpenShift / HAProxy for scaling

Cloud Overview


  • Auto-scaling application hosting
  • Web-service architectures on-demand
  • Free as in beer (no cost, no time limit)
  • Free as in freedom (open source)
  • 512 MB RAM / 1 GB storage per gear (3 free)
  • Need more resources? (just ask!)
  • REALLY a Developer Preview (GA this summer)

Secure and Efficient

One Source to Bind Them All

OpenShift Online

free OpenShift Online accounts:

OpenShift Origin

Open cartridge format

OpenShift Cartridge

Nodejs Cartridge Updates

The big picture

Open client interface

sudo gem install rhc
rhc setup

Will automatically:

  • link your dev env with your openshift account
  • verify your ssh key configuration
  • configure your application namespace

Scaling - Two ways

Vertical - bigger, taller, more expensive

Horizontal - distributed, cheap, linear

What you need for horizontal

  1. Commodity "hardware"
  2. Easy instance creation and deletion
  3. Software built to grow sideways

OpenShift Architecture

OpenShift Scaling

Scaling Tips

  • Go Stateless, Move sessions to the back-end
  • Build with reusable, loosely coupled pieces
  • Design for retries and small pieces failing
  • Borrow the best technologies
  • Profile your data, evaluate your need for speed, durability, and read vs write performance
  • Caching is your friend (when used correctly)


Setting a min and max scale from the command line

rhc cartridge-scale nodejs-0.6 -a nodeapp --min 3 --max 5

Scaling Web UI

Application scaling settings

HAProxy Scaling Hooks

Adding and removing instances / scaling up and down

haproxy_ctld --up

haproxy_ctld --down

HAProxy on / off switches

haproxy_ctld_daemon start
haproxy_ctld_daemon stop
haproxy_ctld_daemon restart

These hooks are available on the command line within your gear, and via a REST API

HAProxy Web UI


HAProxy raw data


Demo time!

Gearing up a new app on OpenShift

rhc app create -s nodepdxbot nodejs-0.6 mongodb-2.2 --from-code=
Application Options
Namespace:   rjdemo
Cartridges:  nodejs-0.6, mongodb-2.2
Source Code:
Gear Size:   default
Scaling:     no

Creating application 'instantappstore' ... done

Waiting for your DNS name to be available ... done

Downloading the application Git repository ...
Cloning into 'instantappstore'...

Your application code is now in 'instantappstore'
instantappstore @ (uuid: 5175981a5973ca7a69000501)
Created:         1:05 PM
Gears:           1 (defaults to small)
Git URL:        
Initial Git URL:

nodejs-0.6 (Node.js 0.6)
  Gears: Located with mongodb-2.2

mongodb-2.2 (MongoDB NoSQL Database 2.2)
  Gears:          Located with nodejs-0.6
  Connection URL:
  Database Name:  instantappstore
  Password:       PTk4cCetTj2w
  Username:       admin

Application instantappstore was created.


You should now have MongoDB and a basic node.js app running on OpenShift!

Your gear is now configured with:

  • it's own git repo
  • it's own web server
  • ssh access
  • logging
  • MongoDB
  • publicly accessible DNS
  • a custom build of nodejs

Run your existing applications on OpenShift

Follow this git-based approach for porting your apps to OpenShift

Make sure that you bind to the correct IP_address and port number...

Environment Variables

var ip_addr  = process.env.OPENSHIFT_INTERNAL_IP   || '';
var port     = process.env.OPENSHIFT_INTERNAL_PORT || 8000;
var hostname = process.env.OPENSHIFT_APP_DNS       || 'localhost';

Cloud Env

config-multipaas is a config-chain wrapper that provides a consistent naming scheme for cloud server configs (from OpenShift, Heroku)

npm install --save config-multipaas

Add your own configs using the config-chain API

var cc = require('config-multipaas')
var config = cc({ additional_configs: "just like config-chain!" });

When cloud configs are not available, local development configs are returned

app.listen(config.get('PORT'), config.get('IP'), function () {
  console.log( "Listening on " + config.get('IP') + ", port " + config.get('PORT') )

Adding cartridges to existing applications

This command should make mongodb services avilable to your application if run from within your local application source folder

rhc cartridge add mongodb-2.2

generating load

For scalable applications, generate load from the command line to see automatic scaling in action:

while true ; do ab -n 10000 -c 400 ; done

Run the above command from within an openshift gear to bypass wifi issues.

Connect to one of your OpenShift apps from the command line:

rhc ssh APP_NAME

Hacking from last night

Cluster sibling awareness now available in Nodejs:

Come hang out with us:

#openshift on freenode irc

More information about scaling with OpenShift:

scale up

Thanks for following along!   --ryanj

Signup with Code - NODEPDX