These are the slides to accompany the talk I gave at JSConf Iceland 2018.
Scaling NodeJSBeyond the ordinaryAbhinav RastogiLead UI Engineer_abhinavrastogi
View Slide
150M RegisteredUsers500K Phones sold in15 seconds200M Visits over 5 daysduring annual sale event
– Andre Bondi“Scalability is the capability of a system to handle agrowing amount of work, or its potential to be enlarged toaccommodate that growth.”
Why should it concern the JScommunity?
Why should it concern the JScommunity?• JS is running on servers now• Failure is shifting to application code• Failure is better handled by devs
Types of ScalabilityxyzAdd more machinesAdd more resourcesApplication layer
Horizontal Scaling
Horizontal ScalingMachineNodeAPI
Horizontal ScalingMachineNodeAPIMachineNodeELB
Vertical Scaling
Vertical ScalingMachineNodeAPI
Vertical ScalingMachineNodeAPINode??
Application LayerOptimisations
The Optimisation CycleLoad TestFind BottleneckFix Issues
network, cpu, memory, disk
network bandwidth
1000 kb per page100 rps per machine100 machines= 10gbps
compression
const app = express()app.use(compression)
co-hosted nginx
co-hosted nginxMachineNodeNodePM2nginxAPIELB
network profiling
netstat / sslsofwatch
everything is a file in unix!
ulimit
ulimitcore file size (blocks, -c) 0file size (blocks, -f) unlimitedpending signals (-i) 32767max locked memory (kbytes, -l) 32max memory size (kbytes, -m) unlimitedopen files (-n) 1024max user processes (-u) 50
increase the limits/reduce the usage
keep-alive header
keep-alive headerObject.assign(headers, {'Connection': 'keep-alive','Keep-Alive': 'timeout=200'});
connection pooling
connection poolingconst http = require('http');fetch(url, {agent: new http.Agent({keepAlive: true,maxSockets: 24})});
ephemeral ports
tcp connection states
tcp connection statessource: http://cnp3book.info.ucl.ac.be/1st/html/transport/transport.html
cpu
cpu profiling
node --prof app.js
import crypto from 'crypto';app.get('/auth', (req, res) => {const hash = crypto.pbkdf2Sync(password, users[username].salt, 100, 512);if (users[username].hash.toString() === hash.toString()) {res.sendStatus(200);} else {res.sendStatus(401);}});
[Summary]:ticks total nonlib name79 0.2% 0.2% JavaScript36703 97.2% 99.2% C++7 0.0% 0.0% GC767 2.0% Shared libraries215 0.6% Unaccounted
[C++]:ticks total nonlib name19557 51.8% 52.9% node::crypto::PBKDF2(v8)4510 11.9% 12.2% _sha1_block_data_order3165 8.4% 8.6% _malloc_zone_malloc
$ npm install -g 0x$ 0x app.js
disk
memory
real-time monitoring
load, profile, fix, repeat!
thank you@_abhinavrastogihttp://tiny.cc/scalingnodejs