Slide 1

Slide 1 text

Scaling NodeJS Beyond the ordinary Abhinav Rastogi Lead UI Engineer _abhinavrastogi

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

150M Registered Users 500K Phones sold in 15 seconds 200M Visits over 5 days during annual sale event

Slide 4

Slide 4 text

– Andre Bondi “Scalability is the capability of a system to handle a growing amount of work, or its potential to be enlarged to accommodate that growth.”

Slide 5

Slide 5 text

Why should it concern the JS community?

Slide 6

Slide 6 text

Why should it concern the JS community? • JS is running on servers now • Failure is shifting to application code • Failure is better handled by devs

Slide 7

Slide 7 text

Types of Scalability x y z Add more machines Add more resources Application layer

Slide 8

Slide 8 text

Horizontal Scaling

Slide 9

Slide 9 text

Horizontal Scaling Machine Node API

Slide 10

Slide 10 text

Horizontal Scaling Machine Node API Machine Node ELB

Slide 11

Slide 11 text

Vertical Scaling

Slide 12

Slide 12 text

Vertical Scaling Machine Node API

Slide 13

Slide 13 text

Vertical Scaling Machine Node API Node ??

Slide 14

Slide 14 text

Application Layer Optimisations

Slide 15

Slide 15 text

The Optimisation Cycle Load Test Find Bottleneck Fix Issues

Slide 16

Slide 16 text

network, cpu, memory, disk

Slide 17

Slide 17 text

network bandwidth

Slide 18

Slide 18 text

network bandwidth

Slide 19

Slide 19 text

1000 kb per page 100 rps per machine 100 machines = 10gbps

Slide 20

Slide 20 text

compression

Slide 21

Slide 21 text

const app = express() app.use(compression)

Slide 22

Slide 22 text

co-hosted nginx

Slide 23

Slide 23 text

co-hosted nginx Machine Node Node PM2 nginx API ELB

Slide 24

Slide 24 text

network profiling

Slide 25

Slide 25 text

netstat / ss lsof watch

Slide 26

Slide 26 text

everything is a file in unix!

Slide 27

Slide 27 text

ulimit

Slide 28

Slide 28 text

ulimit core file size (blocks, -c) 0 file size (blocks, -f) unlimited pending signals (-i) 32767 max locked memory (kbytes, -l) 32 max memory size (kbytes, -m) unlimited open files (-n) 1024 max user processes (-u) 50

Slide 29

Slide 29 text

increase the limits / reduce the usage

Slide 30

Slide 30 text

keep-alive header

Slide 31

Slide 31 text

keep-alive header Object.assign(headers, { 'Connection': 'keep-alive', 'Keep-Alive': 'timeout=200' });

Slide 32

Slide 32 text

connection pooling

Slide 33

Slide 33 text

connection pooling const http = require('http'); fetch(url, { agent: new http.Agent({ keepAlive: true, maxSockets: 24 }) });

Slide 34

Slide 34 text

ephemeral ports

Slide 35

Slide 35 text

tcp connection states

Slide 36

Slide 36 text

tcp connection states source: http://cnp3book.info.ucl.ac.be/1st/html/transport/transport.html

Slide 37

Slide 37 text

tcp connection states

Slide 38

Slide 38 text

cpu

Slide 39

Slide 39 text

cpu profiling

Slide 40

Slide 40 text

node --prof app.js

Slide 41

Slide 41 text

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); } });

Slide 42

Slide 42 text

[Summary]: ticks total nonlib name 79 0.2% 0.2% JavaScript 36703 97.2% 99.2% C++ 7 0.0% 0.0% GC 767 2.0% Shared libraries 215 0.6% Unaccounted

Slide 43

Slide 43 text

[C++]: ticks total nonlib name 19557 51.8% 52.9% node::crypto::PBKDF2(v8) 4510 11.9% 12.2% _sha1_block_data_order 3165 8.4% 8.6% _malloc_zone_malloc

Slide 44

Slide 44 text

No content

Slide 45

Slide 45 text

$ npm install -g 0x $ 0x app.js

Slide 46

Slide 46 text

No content

Slide 47

Slide 47 text

disk

Slide 48

Slide 48 text

memory

Slide 49

Slide 49 text

real-time monitoring

Slide 50

Slide 50 text

load, profile, fix, repeat!

Slide 51

Slide 51 text

thank you @_abhinavrastogi http://tiny.cc/scalingnodejs