Clustering with

image/svg+xml

@MichielDeMey

Full Stack Engineer at Storied

Clustering

Process Clustering
using Node.js Cluster Module

 

Container and Server Clustering
using Kubernetes

Node.js Cluster Module

Server

> Node

Server

😴

😴

😴

> Node

> Node

> Node

> Node

Node uses only a single process (one CPU) and a mamximum of 1.5GB of RAM

Node.js
Cluster Module

Take advantage of multi-core systems.
1 master process forks n workers.


Node.js cluster module will load-balance instances of your app to available CPUs.

Can communicate with the parent using IPC.

There is no shared state between workers.

var cluster = require('cluster')
var app = require('./server')

if (cluster.isMaster) {
  var numCPUs = require('os').cpus().length

  for (var i = 0; i < numCPUs; i++) {
    cluster.fork(process.env)
  }
} else {
  app.listen(process.env.PORT)
}
var app = require('express')()

app.get('/ping', function(req, res) {
  res.send('pong!')
})

module.exports = app
var cluster = require('cluster')
var app = require('./server')

if (cluster.isMaster) {
  var numCPUs = require('os').cpus().length

  for (var i = 0; i < numCPUs; i++) {
    cluster.fork(process.env)
  }
} else {
  app.listen(process.env.PORT)
}

The Cluster module →

Fork children →

Handle requests →

var app = require('express')()

app.get('/ping', function(req, res) {
  res.send('pong!')
})

module.exports = app

Your average Express server

Export for better maintainability →

var cluster = require('cluster')
var app = require('./server')

if (cluster.isMaster) {
  var numCPUs = require('os').cpus().length

  for (var i = 0; i < numCPUs; i++) {
    cluster.fork(process.env)
  }
} else {
  app.listen(process.env.PORT)
}

What about worker crashes?

cluster.on('exit',
  function (worker, code, signal) {
    cluster.fork(process.env)
  }
)

Container Clustering

> Node

Server

> Node

> Node

> Node

>

>

>

>

>

>

>

>

>

>

>

>

>

>

>

>

Distribute load over multiple (virtual or physical) machines

Microservices

Move away from the monolithic app design.

Services talk to each other using APIs and industry standard protocols.

>

>

>

>

REST API

>

>

>

>

What about authentication?

Our services are stateless, authentication should also be stateless.

Session

Store

Service

Service

Service

Using centralized session store.
Application state = hard to scale

What about authorization?

Our services are decentralized, authorization should also be decentralized.

 

Forget about session-based authentication and let's talk about token based authentication.

>

>

>

>

/user/me

>

>

>

>

/photos

JSON Web Token

eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJkZS5tZXkubWljaGllbEBnbWFpbC5jb20iLCJpc3MiOiJhdXRoLmRlbWV5LmlvIiwicGVybWlzc2lvbnMiOlsicGluZyIsInBvbmciXX0.yj84-dz1D1HoJQjehsjHQPm7m60FtCM8pi87MaI5eUE
        

JSON Web Token

  • Holds your identity
  • No expensive DB lookups every time
  • Perfect for clustering
  • Can use symmetric/asymmetric keys
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJkZS5tZXkubWljaGllbEBnbWFpbC5jb20iLCJpc3MiOiJhdXRoLmRlbWV5LmlvIiwicGVybWlzc2lvbnMiOlsicGluZyIsInBvbmciXX0.yj84-dz1D1HoJQjehsjHQPm7m60FtCM8pi87MaI5eUE
        

Authenticate using HTTP header

Similar to oAuth specification

Kubernetes by Google

Getting started

  • Master
    • API Server
    • Scheduler
    • Controller Manager
  • Node
    • Kubelet
    • Kube-proxy

Getting started

  • Pods
    • Collection of containers with shared volumes.
    • Always on the same Node
  • Replication Controllers
    • A replication controller ensures that a specified number of pod "replicas" are running at any one time
  • Services
    • Pod are born and die. Services provide a consistent, well known endpoint for a set of pods
  • Labels
    • Key-value pairs attached to K8s resources

Demo Architecture

  • Reverse Proxy Service
  • Authentication Service
  • Ping Service

Clustering with Node.js

By Michiel De Mey

Clustering with Node.js

Let's talk about clustering in Node.js, the Node.js cluster module and Container Clustering.

  • 112