.. in production

JSFoo '16

Bangalore

.. here to talk about the things we didn't give much fork about!

  • Logging, monitoring & alerts
  • Configurations
  • Profiling and control port
  • Clustering
  • Sticky sessions
  • Conventions

I'm Jai

Engineering @ DoSelect

@dolftax

Logging

What to log?

  • log, info, warn, error, fatal, .. is an overkill

What to log?

  • log, info, warn, error, fatal, .. is an overkill
  • Every log write affects performance

What to log?

  • log, info, warn, error, fatal, .. is an overkill
  • Every log write affects performance
  • `try { } catch (e) { }` does exist

Takeaway

  • Resist the tendency to log everything    

Takeaway

  • Resist the tendency to log everything
  • The more you log, the less you can find

Takeaway

  • Resist the tendency to log everything
  • The more you log, the less you can find
  • `log`, `error`. Period.

Monitoring - The Obvious

IO? Why?

Because, libuv

Configuration

try {
  global.config = require('./dev-config')
} catch (err) {
  global.config = require('./config')
}

Key takeaway: Leave the configuration to deployer playbooks

Containers?

Take a sip of BudLight and ask yourself again

Mem leaks

Mem leaks

Memory

Time

Heapdump helps!

// To generate heap snapshot
require('heapdump')

// Run garbage collector
process.on('SIGUSR2', function () {
  gc()
})
$ kill -USR2 <pid>

Use all the cores

  • Scale vertically first                        

Use all the cores

  • Scale vertically first
  • require('cluster') // && fork workers

.. cluster - bare minimum

if (cluster.isMaster) {
  // Fork workers.
  for (var i = 0; i < numCPUs; i++) {
    cluster.fork()
  }

  cluster.on('exit', (worker, code, signal) => {
    console.log(`worker ${worker.process.pid} died`)
  })
} else {
  // Listen
}

`cluster` ain't enough

Websockets?

Problem:

Websocket handshake might arrive at different workers

Solution:

Maintain sticky session when balancing requests

Sticky session

Conventions

Questions?

 

 

Thanks much!

 

 

@dolftax

Node.js in production

By Jaipradeesh

Node.js in production

  • 1,584