SlideShare a Scribd company logo
PLANNING FOR THE
                                  HORIZONTAL
                                     SCALING NODE.JS APPLICATIONS




                        TheReddest           Brandon Cannaday       brandon@modulus.io

Thursday, April 4, 13
ME




                        HOSTING, DATA, STATS FOR NODE.JS

                                   modulus.io



Thursday, April 4, 13
WHEN TO SCALE?


                        1. RESPONSE TIMES
                        2. CPU USAGE
                        3. CONCURRENT CONNECTIONS




Thursday, April 4, 13
NODEFLY.COM




Thursday, April 4, 13
STARTING POINT
                           mydomain.com




                               > NODE


                              SERVER




Thursday, April 4, 13
NODE TWEAKS

                                     CONCURRENT OUTGOING
                                       CONNECTION LIMIT

                        http.globalAgent.maxSockets = Number.MAX_VALUE;




Thursday, April 4, 13
HURTLE: THE SERVER

                        LINUX CONFIGURATION
                          1. FILE-MAX
                          2. SOMAXCONN
                          3. ULIMIT



Thursday, April 4, 13
FILE-MAX

                        SYSTEM FILE DESCRIPTOR LIMIT

                        1. Run sysctl -w fs.file-max=65535
                        2. Run sysctl -p




Thursday, April 4, 13
SOMAXCONN

                             SOCKET LISTEN QUEUE LENGTH

                        1. Run sysctl -w net.core.somaxconn=65535
                        2. Run sysctl -p




Thursday, April 4, 13
ULIMIT
                        PER PROCESS FILE DESCRIPTOR LIMIT
                          1. Edit /etc/security/limits.conf
                          2. Add the following:
                          *      soft   nofile    65535
                          *      hard   nofile    65535
                          root   soft   nofile    65535
                          root   hard   nofile    65535
Thursday, April 4, 13
RUNNING SMOOTH

                            mydomain.com




                                > NODE


                               SERVER




Thursday, April 4, 13
HURTLE: THE CPU

                           BUY A BIGGER BOX



                         > NODE         > NODE


                        SERVER
                                        SERVER
                         1 CORE
                                        4 CORES

Thursday, April 4, 13
MULTICORE NODE

                           100%




                        USAGE




                                  1   2      3   4
                                      CORE


Thursday, April 4, 13
CLUSTER MODULE
                           mydomain.com




                            > NODE    > NODE



                            > NODE    > NODE



                                 SERVER




Thursday, April 4, 13
CLUSTER EXAMPLE

             var cluster = require('cluster');            The Cluster Module
             var http = require('http');
             var numCPUs = require('os').cpus().length;

             if(cluster.isMaster) {
               for(var i = 0; i < numCPUs; i++) {
                 cluster.fork();
               }
             }
             else {
               http.createServer(function(req, res) {
                 res.writeHead(200);
                 res.end('Hello World.');
               }).listen(80);
             }


Thursday, April 4, 13
CLUSTER EXAMPLE

             var cluster = require('cluster');
             var http = require('http');
             var numCPUs = require('os').cpus().length;

             if(cluster.isMaster) {
               for(var i = 0; i < numCPUs; i++) {
                 cluster.fork();                          Fork Children
               }
             }
             else {
               http.createServer(function(req, res) {
                 res.writeHead(200);
                 res.end('Hello World.');
               }).listen(80);
             }


Thursday, April 4, 13
CLUSTER EXAMPLE

             var cluster = require('cluster');
             var http = require('http');
             var numCPUs = require('os').cpus().length;

             if(cluster.isMaster) {
               for(var i = 0; i < numCPUs; i++) {
                 cluster.fork();
               }
             }
             else {
               http.createServer(function(req, res) {     Handle Requests
                 res.writeHead(200);
                 res.end('Hello World.');
               }).listen(80);
             }


Thursday, April 4, 13
CLUSTER LISTEN


                                 listen(...)

                        WORKER                 MASTER

                                 Handle




Thursday, April 4, 13
ROLLING UPDATES

                        1. UPDATE SCRIPT
                        2. WORKER -> STOP LISTENING
                        3. KILL WORKER
                        4. CALL FORK() AGAIN


Thursday, April 4, 13
CLUSTER MODULE
                           mydomain.com




                            > NODE    > NODE



                            > NODE    > NODE



                                 SERVER




Thursday, April 4, 13
HURTLE: SHARED STATE


                                          > NODE    > NODE

                        NO SHARED STATE
                                          > NODE    > NODE



                                               SERVER




Thursday, April 4, 13
INSTALL REDIS


                           > NODE    > NODE



                           > NODE    > NODE


                                REDIS


                                SERVER




Thursday, April 4, 13
EXAMPLE 1: SESSION
                                     MEMORY STORE

                          var express = require('express'),
                              app = express();


                          app.use(express.cookieParser());
                          app.use(express.session({
                            secret: 'My Cookie Signing Secret'
                          }));

                          app.get('/', function(req, res) {
                            req.session.somekey = 'some value';
                          });




Thursday, April 4, 13
EXAMPLE 1: SESSION
                                               REDIS STORE

                        var express = require('express'),
                            RedisStore = require('connect-redis')(express),
                            app = express();


                        app.use(express.cookieParser());
                        app.use(express.session({
                          store: new RedisStore({ host: 'localhost', port: 6379 }),
                          secret: 'My Cookie Signing Secret'
                        }));

                        app.get('/', function(req, res) {
                          req.session.somekey = 'some value';
                        });



Thursday, April 4, 13
EXAMPLE 2: SOCKET.IO

                        var   RedisStore = require('socket.io/lib/stores/redis')
                          ,   redis = require('socket.io/node_modules/redis')
                          ,   pub    = redis.createClient()
                          ,   sub    = redis.createClient()
                          ,   client = redis.createClient();

                        io.set('store', new RedisStore({
                          redisPub : pub
                        , redisSub : sub
                        , redisClient : client
                        }));


                        https://github.com/LearnBoost/Socket.IO/wiki/Configuring-Socket.IO


Thursday, April 4, 13
RUNNING SMOOTH
                            mydomain.com




                            > NODE    > NODE



                            > NODE    > NODE


                                 REDIS


                                 SERVER


Thursday, April 4, 13
LAST HURTLE: HORIZONTAL


                           > NODE    > NODE   > NODE    > NODE



                           > NODE    > NODE   > NODE    > NODE


                                REDIS              REDIS


                             APP SERVER A       APP SERVER B




Thursday, April 4, 13
SEPARATE REDIS

                        > NODE    > NODE            > NODE    > NODE



                        > NODE    > NODE            > NODE    > NODE


                          APP SERVER A                APP SERVER B




                                           REDIS

                                           SERVER

Thursday, April 4, 13
LOAD BALANCING

                   mydomain.com              LOAD BALANCER

                                                     SERVER



                                  > NODE    > NODE             > NODE    > NODE



                                  > NODE    > NODE             > NODE    > NODE


                                    APP SERVER A                 APP SERVER B




                                                      REDIS

                                                      SERVER



Thursday, April 4, 13
LOAD BALANCING


                         1. MANAGED
                         2. INSTALL ONE
                         3. WRITE YOUR OWN




Thursday, April 4, 13
NGINX

                        http {
                          upstream mydomain_com {
                            server host1.mydomain.com:80;
                            server host2.mydomain.com:80;
                          }                                       LOAD BALANCER

                            server {
                                                                     SERVER
                              listen 80;
                              server_name www.mydomain.com;
                              location / {
                                proxy_pass http://mydomain_com;
                              }
                            }
                        }



Thursday, April 4, 13
WRITE ONE




                        https://github.com/substack/bouncy


Thursday, April 4, 13
BOUNCY
              var bouncy = require('bouncy');                    bouncy module
              var hosts = [
                 'host1.mydomain.com',
                 'host2.mydomain.com'
              ];

              var count = 0;

              var server = bouncy(function(req, res, bounce) {

                   count++;
                   var host = hosts[count % hosts.length];

                   bounce(host, 80);

              });

              server.listen(80);


Thursday, April 4, 13
BOUNCY
              var bouncy = require('bouncy');

              var hosts = [                                      Server collection
                 'host1.mydomain.com',
                 'host2.mydomain.com'
              ];

              var count = 0;

              var server = bouncy(function(req, res, bounce) {

                   count++;
                   var host = hosts[count % hosts.length];

                   bounce(host, 80);

              });

              server.listen(80);


Thursday, April 4, 13
BOUNCY
              var bouncy = require('bouncy');

              var hosts = [
                 'host1.mydomain.com',
                 'host2.mydomain.com'
              ];

              var count = 0;

              var server = bouncy(function(req, res, bounce) {   Create server
                   count++;
                   var host = hosts[count % hosts.length];

                   bounce(host, 80);

              });

              server.listen(80);


Thursday, April 4, 13
BOUNCY
              var bouncy = require('bouncy');

              var hosts = [
                 'host1.mydomain.com',
                 'host2.mydomain.com'
              ];

              var count = 0;

              var server = bouncy(function(req, res, bounce) {

                   count++;
                   var host = hosts[count % hosts.length];

                   bounce(host, 80);                             Bounce request
              });

              server.listen(80);


Thursday, April 4, 13
AFFINITY


                        SESSION AFFINITY
                        STICKY SESSIONS
                          SEND THE SAME PERSON
                         BACK TO THE SAME SERVER



Thursday, April 4, 13
NGINX AFFINITY

                        http {
                          upstream mydomain_com {
                            sticky;
                            server host1.mydomain.com:80;
                            server host2.mydomain.com:80;
                          }

                            server {
                              listen 80;
                              server_name www.mydomain.com;
                              location / {
                                proxy_pass http://mydomain_com;
                              }
                            }
                        }



Thursday, April 4, 13
CUSTOM AFFINITY


                        req.headers['x-forwarded-for']
                        req.connection.remoteAddress




Thursday, April 4, 13
RUNNING SMOOTH

                   mydomain.com              LOAD BALANCER

                                                     SERVER



                                  > NODE    > NODE             > NODE    > NODE



                                  > NODE    > NODE             > NODE    > NODE


                                    APP SERVER A                 APP SERVER B




                                                      REDIS

                                                      SERVER



Thursday, April 4, 13
ROLLING UPDATES


                    1. REMOVE APP SERVER FROM LOAD BALANCER
                    2. UPGRADE APP SERVER
                    3. ADD BACK
                    4. REPEAT


Thursday, April 4, 13
SSL


                        TERMINATE EARLY




Thursday, April 4, 13
SSL

                                    LB               SSL                SSL TERMINATOR

                                           SERVER



                        > NODE    > NODE             > NODE    > NODE



                        > NODE    > NODE             > NODE    > NODE


                          APP SERVER A                 APP SERVER B




                                            REDIS

                                            SERVER



Thursday, April 4, 13
SSL

                        mydomain.com
                              80   443




                         LB             SSL



                               SERVER




Thursday, April 4, 13
STUD
                           EXAMPLE CONFIG FILE


                        frontend = [*]:443
                        backend = [127.0.0.1]:80
                        ssl = on
                        pem-file = "myCert.pem"



                            https://github.com/bumptech/stud

Thursday, April 4, 13
RUNNING SMOOTH W/SSL

                   mydomain.com               LB               SSL

                                                     SERVER



                                  > NODE    > NODE             > NODE    > NODE



                                  > NODE    > NODE             > NODE    > NODE


                                    APP SERVER A                 APP SERVER B




                                                      REDIS

                                                      SERVER



Thursday, April 4, 13
HUGE

                        LB            SSL            LB            SSL

                             SERVER                       SERVER




                                            REDIS

                                            SERVER




Thursday, April 4, 13
DNS


                        ROUND-ROBIN DNS
                         MULTIPLE RECORDS,
                           ONE DOMAIN




Thursday, April 4, 13
ROUND-ROBIN DNS


                        CLIENT 1   1. xxx.xxx.xxx.x
                                   2. xxx.xxx.xxx.y


                        CLIENT 2   1. xxx.xxx.xxx.y
                                   2. xxx.xxx.xxx.x


Thursday, April 4, 13
RUNNING SMOOTH

                          LB            SSL            LB            SSL

                               SERVER                       SERVER




                                              REDIS

                                              SERVER




Thursday, April 4, 13
BIG ENOUGH



                        > NODE


                        SERVER




Thursday, April 4, 13
BIG ENOUGH



                        > NODE


                        SERVER




Thursday, April 4, 13

More Related Content

What's hot

PythonFuse (PyCon4)
PythonFuse (PyCon4)PythonFuse (PyCon4)
PythonFuse (PyCon4)
Matteo Bertozzi
 
FUSE Developing Fillesystems in userspace
FUSE Developing Fillesystems in userspaceFUSE Developing Fillesystems in userspace
FUSE Developing Fillesystems in userspace
elliando dias
 
[Python] Quick book for dell switch_os10
[Python] Quick book for dell switch_os10[Python] Quick book for dell switch_os10
[Python] Quick book for dell switch_os10
Jo Hoon
 
Installing and Configuring Domino 10 on CentOS 7
Installing and Configuring Domino 10 on CentOS 7Installing and Configuring Domino 10 on CentOS 7
Installing and Configuring Domino 10 on CentOS 7
Devin Olson
 
Hadoop 2.2.0 Multi-node cluster Installation on Ubuntu
Hadoop 2.2.0 Multi-node cluster Installation on Ubuntu Hadoop 2.2.0 Multi-node cluster Installation on Ubuntu
Hadoop 2.2.0 Multi-node cluster Installation on Ubuntu
康志強 大人
 
Andresen 8 21 02
Andresen 8 21 02Andresen 8 21 02
Andresen 8 21 02
FNian
 
도커 없이 컨테이너 만들기 5편 마운트 네임스페이스와 오버레이 파일시스템
도커 없이 컨테이너 만들기 5편 마운트 네임스페이스와 오버레이 파일시스템도커 없이 컨테이너 만들기 5편 마운트 네임스페이스와 오버레이 파일시스템
도커 없이 컨테이너 만들기 5편 마운트 네임스페이스와 오버레이 파일시스템
Sam Kim
 
D space manual
D space manualD space manual
D space manual
Bibliounivbtn
 
Solaris
SolarisSolaris
Sun raysetup
Sun raysetupSun raysetup
Sun raysetup
Portal Oliveira
 
The Ring programming language version 1.10 book - Part 92 of 212
The Ring programming language version 1.10 book - Part 92 of 212The Ring programming language version 1.10 book - Part 92 of 212
The Ring programming language version 1.10 book - Part 92 of 212
Mahmoud Samir Fayed
 
Guide to clone_sles_instances
Guide to clone_sles_instancesGuide to clone_sles_instances
Guide to clone_sles_instances
Satheesh Thomas
 
Most frequently used unix commands for database administrator
Most frequently used unix commands for database administratorMost frequently used unix commands for database administrator
Most frequently used unix commands for database administrator
Dinesh jaisankar
 
SSD based storage tuning for databases
SSD based storage tuning for databasesSSD based storage tuning for databases
SSD based storage tuning for databases
Angelo Rajadurai
 
Running hadoop on ubuntu linux
Running hadoop on ubuntu linuxRunning hadoop on ubuntu linux
Running hadoop on ubuntu linux
TRCK
 
The State of Puppet
The State of PuppetThe State of Puppet
The State of Puppet
Puppet
 
Web Server Free Bsd
Web Server Free BsdWeb Server Free Bsd
Web Server Free Bsd
Kwanchai Charoennet
 
ubunturef
ubunturefubunturef
ubunturef
wensheng wei
 
Domino9on centos6
Domino9on centos6Domino9on centos6
Domino9on centos6
a8us
 

What's hot (19)

PythonFuse (PyCon4)
PythonFuse (PyCon4)PythonFuse (PyCon4)
PythonFuse (PyCon4)
 
FUSE Developing Fillesystems in userspace
FUSE Developing Fillesystems in userspaceFUSE Developing Fillesystems in userspace
FUSE Developing Fillesystems in userspace
 
[Python] Quick book for dell switch_os10
[Python] Quick book for dell switch_os10[Python] Quick book for dell switch_os10
[Python] Quick book for dell switch_os10
 
Installing and Configuring Domino 10 on CentOS 7
Installing and Configuring Domino 10 on CentOS 7Installing and Configuring Domino 10 on CentOS 7
Installing and Configuring Domino 10 on CentOS 7
 
Hadoop 2.2.0 Multi-node cluster Installation on Ubuntu
Hadoop 2.2.0 Multi-node cluster Installation on Ubuntu Hadoop 2.2.0 Multi-node cluster Installation on Ubuntu
Hadoop 2.2.0 Multi-node cluster Installation on Ubuntu
 
Andresen 8 21 02
Andresen 8 21 02Andresen 8 21 02
Andresen 8 21 02
 
도커 없이 컨테이너 만들기 5편 마운트 네임스페이스와 오버레이 파일시스템
도커 없이 컨테이너 만들기 5편 마운트 네임스페이스와 오버레이 파일시스템도커 없이 컨테이너 만들기 5편 마운트 네임스페이스와 오버레이 파일시스템
도커 없이 컨테이너 만들기 5편 마운트 네임스페이스와 오버레이 파일시스템
 
D space manual
D space manualD space manual
D space manual
 
Solaris
SolarisSolaris
Solaris
 
Sun raysetup
Sun raysetupSun raysetup
Sun raysetup
 
The Ring programming language version 1.10 book - Part 92 of 212
The Ring programming language version 1.10 book - Part 92 of 212The Ring programming language version 1.10 book - Part 92 of 212
The Ring programming language version 1.10 book - Part 92 of 212
 
Guide to clone_sles_instances
Guide to clone_sles_instancesGuide to clone_sles_instances
Guide to clone_sles_instances
 
Most frequently used unix commands for database administrator
Most frequently used unix commands for database administratorMost frequently used unix commands for database administrator
Most frequently used unix commands for database administrator
 
SSD based storage tuning for databases
SSD based storage tuning for databasesSSD based storage tuning for databases
SSD based storage tuning for databases
 
Running hadoop on ubuntu linux
Running hadoop on ubuntu linuxRunning hadoop on ubuntu linux
Running hadoop on ubuntu linux
 
The State of Puppet
The State of PuppetThe State of Puppet
The State of Puppet
 
Web Server Free Bsd
Web Server Free BsdWeb Server Free Bsd
Web Server Free Bsd
 
ubunturef
ubunturefubunturef
ubunturef
 
Domino9on centos6
Domino9on centos6Domino9on centos6
Domino9on centos6
 

Viewers also liked

Horizontally Scaling Node.js and WebSockets
Horizontally Scaling Node.js and WebSocketsHorizontally Scaling Node.js and WebSockets
Horizontally Scaling Node.js and WebSockets
James Simpson
 
A million connections and beyond - Node.js at scale
A million connections and beyond - Node.js at scaleA million connections and beyond - Node.js at scale
A million connections and beyond - Node.js at scale
Tom Croucher
 
Anatomy of a Modern Node.js Application Architecture
Anatomy of a Modern Node.js Application Architecture Anatomy of a Modern Node.js Application Architecture
Anatomy of a Modern Node.js Application Architecture
AppDynamics
 
Progressive Advancement in Web8
Progressive Advancement in Web8Progressive Advancement in Web8
Progressive Advancement in Web8
Paul Irish
 
Building and Scaling Node.js Applications
Building and Scaling Node.js ApplicationsBuilding and Scaling Node.js Applications
Building and Scaling Node.js Applications
Ohad Kravchick
 
Why i hate node js
Why i hate node jsWhy i hate node js
Why i hate node js
Samuele Reghenzi
 
An Overview of HTML5 Storage
An Overview of HTML5 StorageAn Overview of HTML5 Storage
An Overview of HTML5 Storage
Paul Irish
 
Dynamic Application Development by NodeJS ,AngularJS with OrientDB
Dynamic Application Development by NodeJS ,AngularJS with OrientDBDynamic Application Development by NodeJS ,AngularJS with OrientDB
Dynamic Application Development by NodeJS ,AngularJS with OrientDB
Apaichon Punopas
 
Building servers with Node.js
Building servers with Node.jsBuilding servers with Node.js
Building servers with Node.js
ConFoo
 
Scalability using Node.js
Scalability using Node.jsScalability using Node.js
Scalability using Node.js
ratankadam
 
Экономика продуктов и метрики (Илья Краинский, Magic Ink)
Экономика продуктов и метрики (Илья Краинский, Magic Ink)Экономика продуктов и метрики (Илья Краинский, Magic Ink)
Экономика продуктов и метрики (Илья Краинский, Magic Ink)
PCampRussia
 
Architecting large Node.js applications
Architecting large Node.js applicationsArchitecting large Node.js applications
Architecting large Node.js applications
Sergi Mansilla
 
Introduction to Node.js
Introduction to Node.jsIntroduction to Node.js
Introduction to Node.js
Vikash Singh
 
Clustering with Node.js
Clustering with Node.jsClustering with Node.js
Clustering with Node.js
Michiel De Mey
 
Best Practices You Must Apply to Secure Your APIs - Scott Morrison, SVP & Dis...
Best Practices You Must Apply to Secure Your APIs - Scott Morrison, SVP & Dis...Best Practices You Must Apply to Secure Your APIs - Scott Morrison, SVP & Dis...
Best Practices You Must Apply to Secure Your APIs - Scott Morrison, SVP & Dis...
CA API Management
 

Viewers also liked (15)

Horizontally Scaling Node.js and WebSockets
Horizontally Scaling Node.js and WebSocketsHorizontally Scaling Node.js and WebSockets
Horizontally Scaling Node.js and WebSockets
 
A million connections and beyond - Node.js at scale
A million connections and beyond - Node.js at scaleA million connections and beyond - Node.js at scale
A million connections and beyond - Node.js at scale
 
Anatomy of a Modern Node.js Application Architecture
Anatomy of a Modern Node.js Application Architecture Anatomy of a Modern Node.js Application Architecture
Anatomy of a Modern Node.js Application Architecture
 
Progressive Advancement in Web8
Progressive Advancement in Web8Progressive Advancement in Web8
Progressive Advancement in Web8
 
Building and Scaling Node.js Applications
Building and Scaling Node.js ApplicationsBuilding and Scaling Node.js Applications
Building and Scaling Node.js Applications
 
Why i hate node js
Why i hate node jsWhy i hate node js
Why i hate node js
 
An Overview of HTML5 Storage
An Overview of HTML5 StorageAn Overview of HTML5 Storage
An Overview of HTML5 Storage
 
Dynamic Application Development by NodeJS ,AngularJS with OrientDB
Dynamic Application Development by NodeJS ,AngularJS with OrientDBDynamic Application Development by NodeJS ,AngularJS with OrientDB
Dynamic Application Development by NodeJS ,AngularJS with OrientDB
 
Building servers with Node.js
Building servers with Node.jsBuilding servers with Node.js
Building servers with Node.js
 
Scalability using Node.js
Scalability using Node.jsScalability using Node.js
Scalability using Node.js
 
Экономика продуктов и метрики (Илья Краинский, Magic Ink)
Экономика продуктов и метрики (Илья Краинский, Magic Ink)Экономика продуктов и метрики (Илья Краинский, Magic Ink)
Экономика продуктов и метрики (Илья Краинский, Magic Ink)
 
Architecting large Node.js applications
Architecting large Node.js applicationsArchitecting large Node.js applications
Architecting large Node.js applications
 
Introduction to Node.js
Introduction to Node.jsIntroduction to Node.js
Introduction to Node.js
 
Clustering with Node.js
Clustering with Node.jsClustering with Node.js
Clustering with Node.js
 
Best Practices You Must Apply to Secure Your APIs - Scott Morrison, SVP & Dis...
Best Practices You Must Apply to Secure Your APIs - Scott Morrison, SVP & Dis...Best Practices You Must Apply to Secure Your APIs - Scott Morrison, SVP & Dis...
Best Practices You Must Apply to Secure Your APIs - Scott Morrison, SVP & Dis...
 

Similar to Planning for the Horizontal: Scaling Node.js Applications

Rails Intro & Tutorial
Rails Intro & TutorialRails Intro & Tutorial
Rails Intro & Tutorial
Ming-hsuan Chang
 
Node.js - A Quick Tour II
Node.js - A Quick Tour IINode.js - A Quick Tour II
Node.js - A Quick Tour II
Felix Geisendörfer
 
Put on Your Asynchronous Hat and Node
Put on Your Asynchronous Hat and NodePut on Your Asynchronous Hat and Node
Put on Your Asynchronous Hat and Node
Marc Fasel
 
Arp security by_abdimuna_sep_28
Arp security by_abdimuna_sep_28Arp security by_abdimuna_sep_28
Arp security by_abdimuna_sep_28
Abdimuna Muna
 
Building Reusable Puppet Modules
Building Reusable Puppet ModulesBuilding Reusable Puppet Modules
Building Reusable Puppet Modules
Puppet
 
Intravert atx meetup_condensed
Intravert atx meetup_condensedIntravert atx meetup_condensed
Intravert atx meetup_condensed
zznate
 
Cors
CorsCors
Cors
s4parke
 

Similar to Planning for the Horizontal: Scaling Node.js Applications (7)

Rails Intro & Tutorial
Rails Intro & TutorialRails Intro & Tutorial
Rails Intro & Tutorial
 
Node.js - A Quick Tour II
Node.js - A Quick Tour IINode.js - A Quick Tour II
Node.js - A Quick Tour II
 
Put on Your Asynchronous Hat and Node
Put on Your Asynchronous Hat and NodePut on Your Asynchronous Hat and Node
Put on Your Asynchronous Hat and Node
 
Arp security by_abdimuna_sep_28
Arp security by_abdimuna_sep_28Arp security by_abdimuna_sep_28
Arp security by_abdimuna_sep_28
 
Building Reusable Puppet Modules
Building Reusable Puppet ModulesBuilding Reusable Puppet Modules
Building Reusable Puppet Modules
 
Intravert atx meetup_condensed
Intravert atx meetup_condensedIntravert atx meetup_condensed
Intravert atx meetup_condensed
 
Cors
CorsCors
Cors
 

Recently uploaded

GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
Neo4j
 
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
SOFTTECHHUB
 
UiPath Test Automation using UiPath Test Suite series, part 5
UiPath Test Automation using UiPath Test Suite series, part 5UiPath Test Automation using UiPath Test Suite series, part 5
UiPath Test Automation using UiPath Test Suite series, part 5
DianaGray10
 
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
James Anderson
 
TrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy SurveyTrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy Survey
TrustArc
 
Building RAG with self-deployed Milvus vector database and Snowpark Container...
Building RAG with self-deployed Milvus vector database and Snowpark Container...Building RAG with self-deployed Milvus vector database and Snowpark Container...
Building RAG with self-deployed Milvus vector database and Snowpark Container...
Zilliz
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
ControlCase
 
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdfObservability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Paige Cruz
 
A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...
sonjaschweigert1
 
Generative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to ProductionGenerative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to Production
Aggregage
 
20240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 202420240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 2024
Matthew Sinclair
 
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
SOFTTECHHUB
 
20 Comprehensive Checklist of Designing and Developing a Website
20 Comprehensive Checklist of Designing and Developing a Website20 Comprehensive Checklist of Designing and Developing a Website
20 Comprehensive Checklist of Designing and Developing a Website
Pixlogix Infotech
 
Enchancing adoption of Open Source Libraries. A case study on Albumentations.AI
Enchancing adoption of Open Source Libraries. A case study on Albumentations.AIEnchancing adoption of Open Source Libraries. A case study on Albumentations.AI
Enchancing adoption of Open Source Libraries. A case study on Albumentations.AI
Vladimir Iglovikov, Ph.D.
 
Introduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - CybersecurityIntroduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - Cybersecurity
mikeeftimakis1
 
Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1
DianaGray10
 
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
Neo4j
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
Safe Software
 
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
Edge AI and Vision Alliance
 
UiPath Test Automation using UiPath Test Suite series, part 6
UiPath Test Automation using UiPath Test Suite series, part 6UiPath Test Automation using UiPath Test Suite series, part 6
UiPath Test Automation using UiPath Test Suite series, part 6
DianaGray10
 

Recently uploaded (20)

GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
 
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
 
UiPath Test Automation using UiPath Test Suite series, part 5
UiPath Test Automation using UiPath Test Suite series, part 5UiPath Test Automation using UiPath Test Suite series, part 5
UiPath Test Automation using UiPath Test Suite series, part 5
 
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
 
TrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy SurveyTrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy Survey
 
Building RAG with self-deployed Milvus vector database and Snowpark Container...
Building RAG with self-deployed Milvus vector database and Snowpark Container...Building RAG with self-deployed Milvus vector database and Snowpark Container...
Building RAG with self-deployed Milvus vector database and Snowpark Container...
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
 
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdfObservability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
 
A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...
 
Generative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to ProductionGenerative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to Production
 
20240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 202420240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 2024
 
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
 
20 Comprehensive Checklist of Designing and Developing a Website
20 Comprehensive Checklist of Designing and Developing a Website20 Comprehensive Checklist of Designing and Developing a Website
20 Comprehensive Checklist of Designing and Developing a Website
 
Enchancing adoption of Open Source Libraries. A case study on Albumentations.AI
Enchancing adoption of Open Source Libraries. A case study on Albumentations.AIEnchancing adoption of Open Source Libraries. A case study on Albumentations.AI
Enchancing adoption of Open Source Libraries. A case study on Albumentations.AI
 
Introduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - CybersecurityIntroduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - Cybersecurity
 
Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1
 
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
 
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
 
UiPath Test Automation using UiPath Test Suite series, part 6
UiPath Test Automation using UiPath Test Suite series, part 6UiPath Test Automation using UiPath Test Suite series, part 6
UiPath Test Automation using UiPath Test Suite series, part 6
 

Planning for the Horizontal: Scaling Node.js Applications

  • 1. PLANNING FOR THE HORIZONTAL SCALING NODE.JS APPLICATIONS TheReddest Brandon Cannaday brandon@modulus.io Thursday, April 4, 13
  • 2. ME HOSTING, DATA, STATS FOR NODE.JS modulus.io Thursday, April 4, 13
  • 3. WHEN TO SCALE? 1. RESPONSE TIMES 2. CPU USAGE 3. CONCURRENT CONNECTIONS Thursday, April 4, 13
  • 5. STARTING POINT mydomain.com > NODE SERVER Thursday, April 4, 13
  • 6. NODE TWEAKS CONCURRENT OUTGOING CONNECTION LIMIT http.globalAgent.maxSockets = Number.MAX_VALUE; Thursday, April 4, 13
  • 7. HURTLE: THE SERVER LINUX CONFIGURATION 1. FILE-MAX 2. SOMAXCONN 3. ULIMIT Thursday, April 4, 13
  • 8. FILE-MAX SYSTEM FILE DESCRIPTOR LIMIT 1. Run sysctl -w fs.file-max=65535 2. Run sysctl -p Thursday, April 4, 13
  • 9. SOMAXCONN SOCKET LISTEN QUEUE LENGTH 1. Run sysctl -w net.core.somaxconn=65535 2. Run sysctl -p Thursday, April 4, 13
  • 10. ULIMIT PER PROCESS FILE DESCRIPTOR LIMIT 1. Edit /etc/security/limits.conf 2. Add the following: * soft nofile 65535 * hard nofile 65535 root soft nofile 65535 root hard nofile 65535 Thursday, April 4, 13
  • 11. RUNNING SMOOTH mydomain.com > NODE SERVER Thursday, April 4, 13
  • 12. HURTLE: THE CPU BUY A BIGGER BOX > NODE > NODE SERVER SERVER 1 CORE 4 CORES Thursday, April 4, 13
  • 13. MULTICORE NODE 100% USAGE 1 2 3 4 CORE Thursday, April 4, 13
  • 14. CLUSTER MODULE mydomain.com > NODE > NODE > NODE > NODE SERVER Thursday, April 4, 13
  • 15. CLUSTER EXAMPLE var cluster = require('cluster'); The Cluster Module var http = require('http'); var numCPUs = require('os').cpus().length; if(cluster.isMaster) { for(var i = 0; i < numCPUs; i++) { cluster.fork(); } } else { http.createServer(function(req, res) { res.writeHead(200); res.end('Hello World.'); }).listen(80); } Thursday, April 4, 13
  • 16. CLUSTER EXAMPLE var cluster = require('cluster'); var http = require('http'); var numCPUs = require('os').cpus().length; if(cluster.isMaster) { for(var i = 0; i < numCPUs; i++) { cluster.fork(); Fork Children } } else { http.createServer(function(req, res) { res.writeHead(200); res.end('Hello World.'); }).listen(80); } Thursday, April 4, 13
  • 17. CLUSTER EXAMPLE var cluster = require('cluster'); var http = require('http'); var numCPUs = require('os').cpus().length; if(cluster.isMaster) { for(var i = 0; i < numCPUs; i++) { cluster.fork(); } } else { http.createServer(function(req, res) { Handle Requests res.writeHead(200); res.end('Hello World.'); }).listen(80); } Thursday, April 4, 13
  • 18. CLUSTER LISTEN listen(...) WORKER MASTER Handle Thursday, April 4, 13
  • 19. ROLLING UPDATES 1. UPDATE SCRIPT 2. WORKER -> STOP LISTENING 3. KILL WORKER 4. CALL FORK() AGAIN Thursday, April 4, 13
  • 20. CLUSTER MODULE mydomain.com > NODE > NODE > NODE > NODE SERVER Thursday, April 4, 13
  • 21. HURTLE: SHARED STATE > NODE > NODE NO SHARED STATE > NODE > NODE SERVER Thursday, April 4, 13
  • 22. INSTALL REDIS > NODE > NODE > NODE > NODE REDIS SERVER Thursday, April 4, 13
  • 23. EXAMPLE 1: SESSION MEMORY STORE var express = require('express'), app = express(); app.use(express.cookieParser()); app.use(express.session({ secret: 'My Cookie Signing Secret' })); app.get('/', function(req, res) { req.session.somekey = 'some value'; }); Thursday, April 4, 13
  • 24. EXAMPLE 1: SESSION REDIS STORE var express = require('express'), RedisStore = require('connect-redis')(express), app = express(); app.use(express.cookieParser()); app.use(express.session({ store: new RedisStore({ host: 'localhost', port: 6379 }), secret: 'My Cookie Signing Secret' })); app.get('/', function(req, res) { req.session.somekey = 'some value'; }); Thursday, April 4, 13
  • 25. EXAMPLE 2: SOCKET.IO var RedisStore = require('socket.io/lib/stores/redis') , redis = require('socket.io/node_modules/redis') , pub = redis.createClient() , sub = redis.createClient() , client = redis.createClient(); io.set('store', new RedisStore({ redisPub : pub , redisSub : sub , redisClient : client })); https://github.com/LearnBoost/Socket.IO/wiki/Configuring-Socket.IO Thursday, April 4, 13
  • 26. RUNNING SMOOTH mydomain.com > NODE > NODE > NODE > NODE REDIS SERVER Thursday, April 4, 13
  • 27. LAST HURTLE: HORIZONTAL > NODE > NODE > NODE > NODE > NODE > NODE > NODE > NODE REDIS REDIS APP SERVER A APP SERVER B Thursday, April 4, 13
  • 28. SEPARATE REDIS > NODE > NODE > NODE > NODE > NODE > NODE > NODE > NODE APP SERVER A APP SERVER B REDIS SERVER Thursday, April 4, 13
  • 29. LOAD BALANCING mydomain.com LOAD BALANCER SERVER > NODE > NODE > NODE > NODE > NODE > NODE > NODE > NODE APP SERVER A APP SERVER B REDIS SERVER Thursday, April 4, 13
  • 30. LOAD BALANCING 1. MANAGED 2. INSTALL ONE 3. WRITE YOUR OWN Thursday, April 4, 13
  • 31. NGINX http { upstream mydomain_com { server host1.mydomain.com:80; server host2.mydomain.com:80; } LOAD BALANCER server { SERVER listen 80; server_name www.mydomain.com; location / { proxy_pass http://mydomain_com; } } } Thursday, April 4, 13
  • 32. WRITE ONE https://github.com/substack/bouncy Thursday, April 4, 13
  • 33. BOUNCY var bouncy = require('bouncy'); bouncy module var hosts = [ 'host1.mydomain.com', 'host2.mydomain.com' ]; var count = 0; var server = bouncy(function(req, res, bounce) { count++; var host = hosts[count % hosts.length]; bounce(host, 80); }); server.listen(80); Thursday, April 4, 13
  • 34. BOUNCY var bouncy = require('bouncy'); var hosts = [ Server collection 'host1.mydomain.com', 'host2.mydomain.com' ]; var count = 0; var server = bouncy(function(req, res, bounce) { count++; var host = hosts[count % hosts.length]; bounce(host, 80); }); server.listen(80); Thursday, April 4, 13
  • 35. BOUNCY var bouncy = require('bouncy'); var hosts = [ 'host1.mydomain.com', 'host2.mydomain.com' ]; var count = 0; var server = bouncy(function(req, res, bounce) { Create server count++; var host = hosts[count % hosts.length]; bounce(host, 80); }); server.listen(80); Thursday, April 4, 13
  • 36. BOUNCY var bouncy = require('bouncy'); var hosts = [ 'host1.mydomain.com', 'host2.mydomain.com' ]; var count = 0; var server = bouncy(function(req, res, bounce) { count++; var host = hosts[count % hosts.length]; bounce(host, 80); Bounce request }); server.listen(80); Thursday, April 4, 13
  • 37. AFFINITY SESSION AFFINITY STICKY SESSIONS SEND THE SAME PERSON BACK TO THE SAME SERVER Thursday, April 4, 13
  • 38. NGINX AFFINITY http { upstream mydomain_com { sticky; server host1.mydomain.com:80; server host2.mydomain.com:80; } server { listen 80; server_name www.mydomain.com; location / { proxy_pass http://mydomain_com; } } } Thursday, April 4, 13
  • 39. CUSTOM AFFINITY req.headers['x-forwarded-for'] req.connection.remoteAddress Thursday, April 4, 13
  • 40. RUNNING SMOOTH mydomain.com LOAD BALANCER SERVER > NODE > NODE > NODE > NODE > NODE > NODE > NODE > NODE APP SERVER A APP SERVER B REDIS SERVER Thursday, April 4, 13
  • 41. ROLLING UPDATES 1. REMOVE APP SERVER FROM LOAD BALANCER 2. UPGRADE APP SERVER 3. ADD BACK 4. REPEAT Thursday, April 4, 13
  • 42. SSL TERMINATE EARLY Thursday, April 4, 13
  • 43. SSL LB SSL SSL TERMINATOR SERVER > NODE > NODE > NODE > NODE > NODE > NODE > NODE > NODE APP SERVER A APP SERVER B REDIS SERVER Thursday, April 4, 13
  • 44. SSL mydomain.com 80 443 LB SSL SERVER Thursday, April 4, 13
  • 45. STUD EXAMPLE CONFIG FILE frontend = [*]:443 backend = [127.0.0.1]:80 ssl = on pem-file = "myCert.pem" https://github.com/bumptech/stud Thursday, April 4, 13
  • 46. RUNNING SMOOTH W/SSL mydomain.com LB SSL SERVER > NODE > NODE > NODE > NODE > NODE > NODE > NODE > NODE APP SERVER A APP SERVER B REDIS SERVER Thursday, April 4, 13
  • 47. HUGE LB SSL LB SSL SERVER SERVER REDIS SERVER Thursday, April 4, 13
  • 48. DNS ROUND-ROBIN DNS MULTIPLE RECORDS, ONE DOMAIN Thursday, April 4, 13
  • 49. ROUND-ROBIN DNS CLIENT 1 1. xxx.xxx.xxx.x 2. xxx.xxx.xxx.y CLIENT 2 1. xxx.xxx.xxx.y 2. xxx.xxx.xxx.x Thursday, April 4, 13
  • 50. RUNNING SMOOTH LB SSL LB SSL SERVER SERVER REDIS SERVER Thursday, April 4, 13
  • 51. BIG ENOUGH > NODE SERVER Thursday, April 4, 13
  • 52. BIG ENOUGH > NODE SERVER Thursday, April 4, 13