seattle strongloop node.js workshop

81
Shubhra Kar | Products & Education twitter:@shubhrakar {“Node.js”: “APIs @hyperscale”}

Upload: jguerrero99

Post on 13-Aug-2015

190 views

Category:

Software


1 download

TRANSCRIPT

Page 1: Seattle StrongLoop Node.js Workshop

Shubhra Kar | Products & Education twitter:@shubhrakar

{“Node.js”: “APIs @hyperscale”}

Page 2: Seattle StrongLoop Node.js Workshop

About me

u  J2EE and SOA architect

u  Performance architect

u  Node, mBaaS & APIs

Page 3: Seattle StrongLoop Node.js Workshop

These guys sent me !

Bert Belder

Ben Noordhuis

Node/io core

Raymond Feng

Ritchie Martori

LoopBack & Express core

Sam Roberts

Miroslav Bajtos

Ryan Graham

Page 4: Seattle StrongLoop Node.js Workshop

Buzzwords

Websites

Smart Mobiles

Online Catalog

Social Media

Kiosks

Computers

Physical Store

Gaming Consoles

Page 5: Seattle StrongLoop Node.js Workshop

No IO bottlenecks Highly Concurrent

Very Fast JIT compiled

Node.js is emerging as the answer

Reduce infrastructure by 5X Scalable

Lightweight, JS back and front, Headless Full stack

Write and maintain 50% lesser code Simple

Suited for mobile and IoT Evented & JSON

Biggest OSS community on the planet Ecosystem

Page 6: Seattle StrongLoop Node.js Workshop

Multi Threaded Server

Node.js Server

Page 7: Seattle StrongLoop Node.js Workshop

Integration needs to be lighter and isomorphic !

REST/JSON X - Platform

Relational Database Clustered Database Security Gateway Cloud Firewall SOA Bus ( not sure on this one)

Laptop ( not requested but useful)

Connections

Tablet Phone IoT SDK API SERVER Items for ACL

Quick Example of Use

App Server Add On

App Server

Relational Database Clustered Database Security Gateway Cloud Firewall SOA Bus ( not sure on this one)

Laptop ( not requested but useful)

Connections

Tablet Phone IoT SDK API SERVER Items for ACL

Quick Example of Use

App Server Add On

App Server

X - Backend

Legacy to front edge can be fast and simple !

Page 8: Seattle StrongLoop Node.js Workshop

Proof in the Pudding

0

500

1000

1500

2000

2500

3000

1000 10000

Tran

sact

ions

/ se

cond

Back-end response (ms)

StrongLoop’s Node.js Solution vs. 5 leading competitive integration solutions Fortune 25 financial services customer evaluation in October 2014 "

leading competitor closest competitor

Page 9: Seattle StrongLoop Node.js Workshop

Nodies are not just Silicon Valley hipsters !

Page 10: Seattle StrongLoop Node.js Workshop

Getting Started Tips for

Converts

Page 11: Seattle StrongLoop Node.js Workshop

Ruby vs. Node

  RVM == NVM –  Everything is kept neatly in your user home directory. –  You can easily have multiple versions at the same time.

  GEM == NPM –  $ npm install [PACKAGE NAME] –  node_modules (global and local)

Gemfile == package.json   bundle install == $ npm install

$response = file_get_contents("http://example.com"); print_r($response); var http = require('http'); http.request({ hostname: 'example.com' }, function(res) { res.setEncoding('utf8’); res.on('data', function(chunk) { console.log(chunk); }); }).end();

Page 12: Seattle StrongLoop Node.js Workshop

Java vs. advanced node framework - Loopback   https://strongloop.com/strongblog/node-js-java-getting-started/

  Beer REST API in Java on JBoss –  Creating the pom.xml (LOC = 54) –  Creating beans.xml and setting servlet mapping (LOC=10) –  Creating DBConnection class (LOC = 35) –  Importing data into MongoDB (LOC = 1) –  Creating Beer model object (LOC = 24) –  Creating the REST service (LOC = 48) –  Bask in the Joy

Total LOC = 172, Server Install, Mongo Tools, etc… Experience = sweaty

  Beer REST API in Node on Loopback

LOC = None (boilerplate scaffold), 10 config lines, Experience = priceless

Page 13: Seattle StrongLoop Node.js Workshop

No I am serious

  JVM != V8 Bytecode = JIT compilation   Package library = module JaxRS = remoting   EJB = microservice   Controller = router   Java Model Class = JSON Model

Page 14: Seattle StrongLoop Node.js Workshop

Big Picture?

Page 15: Seattle StrongLoop Node.js Workshop

Evolution is inevitable !

Thick

Web SaaS Mobile IoT

Page 16: Seattle StrongLoop Node.js Workshop

Frameworks evolve as well !

•  Callback •  Reactor •  Observer

•  Express •  HAPI •  Restify •  Total •  Partial

•  Loopback •  Parse •  Meteor •  Feedhenry*

•  Loopback •  Sails •  Meteor •  Geddy

•  Loopback •  NodeRED •  Proprietary

KISS MVC

MBaaS ORM/ MEAN

Micro/ loT

Page 17: Seattle StrongLoop Node.js Workshop

Pattern 0: Natal patterns

Philosophy: KISS (Keep It Simple Stupid) u  small core u  small modules u  small surface area

Patterns u  reactor u  callback u  module u  observer

Page 18: Seattle StrongLoop Node.js Workshop

Pattern 1: Convention

HTTP Server Library REST HTTP Library

Simple MVC Structure

Page 19: Seattle StrongLoop Node.js Workshop

Pros and Cons

Express DIY HTTP Routing, Middleware, Templating Simple Web Apps Github Stars – 19K Commercial Support - StrongLoop CLI App Generator Export API definition with strong-remoting Connect/Express middleware extensions Vast Ecosystem and low learning curve StrongLoop DevOps tooling support Manual CRUD endpoints Manual recursive refactoring/testing No Data source Support No Client SDK No ACL (Fine Authorization)

Restify DIY Simplicity, Rest Routing Simple REST API Github Stars – 3K Commercial Support - Joyent Yeoman Generator SPDY Support Dtrace Support No export of API definition No Data source Support No Extensions of significance Manual CRUD endpoints Manual recursive refactoring/testing Limited Ecosystem No ACL (Fine Authorization)

Page 20: Seattle StrongLoop Node.js Workshop

Router in

Express

StrongLoop  Confiden.al  and  Proprietary  Informa.on  –  ©  2015  

Page 21: Seattle StrongLoop Node.js Workshop

Router in

Restify

StrongLoop  Confiden.al  and  Proprietary  Informa.on  –  ©  2015  

Page 22: Seattle StrongLoop Node.js Workshop

Pattern 2: Configuration

HTTP Server Framework

Hapi Opinionated Modularity, Security, HTTP Server control Complex Web Apps, APIs Github Stars – 4K Commercial Support - None Yeoman Generator No export of API definition Hapi Plugins – Joi, bell, bassmaster, etc Limited ecosystem as compared to Express; but does support templates with plugins Swagger support for API documentation Very limited Data source support – Mongo, Postgres, leveldb No Client SDK No ACL (Fine Authorization)

Page 23: Seattle StrongLoop Node.js Workshop

Router in

hapi

StrongLoop  Confiden.al  and  Proprietary  Informa.on  –  ©  2015  

Page 24: Seattle StrongLoop Node.js Workshop

Pattern 3 : Full Stack JS à lends into pattern 4

{“..”} JSON is the new first class citizen

Page 25: Seattle StrongLoop Node.js Workshop

Pattern 4: ORM & Isomorphic JS

u  Model Driven Development u  Any data source u  Isomorphic JS

u  Omni-Channel u  Share Models (Client/Server)

u  Loopback & Meteor u  Automatic REST API generation, routing, etc u  Common Weakness : Higher Learning Curve

Page 26: Seattle StrongLoop Node.js Workshop

Sails Opinionated Web MVC framework WebApps, API Rails familiarity, MVC Github Stars – 10 K (older) Commercial Support - None Yeoman Generator StrongLoop DevOps tooling support No export of API definition Limited data sources – MySQL, Mongo, Postgresql, memory/file No Extensions of significance Limited Ecosystem No ACL (Fine Authorization) No Client SDKs

Pros and Cons

Page 27: Seattle StrongLoop Node.js Workshop

Loopback Opinionated API framework Complex Web Apps and APIs Modularity, Scaffolding, Enterprise Connectivity Github Stars – 4 K (newer) Commercial Support - StrongLoop CLI Code Generator, Visual API composer, API explorer Export API definition Extensions like Push, File Storage, Passport, Oauth 2.0, Express Middleware Vast Connector ecosystem and extend express Auto CRUD Swagger UI and Code Generator DataSources like Mongo, mySQL, Oracle, SOAP, REST, SQLServer, Memory/File, Postgresql, Email, ATG, Couchbase, Apache Kafka, and many more Client SDK – Angular, Browser, IOS, Android, Node.js Mature ACL (Fine Authorization), API Gateway

Pros and Cons

Page 28: Seattle StrongLoop Node.js Workshop

ORM in Loopback.io

Page 29: Seattle StrongLoop Node.js Workshop

Sub-pattern : Schema Discovery

Page 30: Seattle StrongLoop Node.js Workshop

Isomorphic JS in Loopback.io

JS client

JS Server

Remoting

Page 31: Seattle StrongLoop Node.js Workshop

Router & Swagger

in Loopback

StrongLoop  Confiden.al  and  Proprietary  Informa.on  –  ©  2015  

Page 32: Seattle StrongLoop Node.js Workshop

Router  using  Sails  

Page 33: Seattle StrongLoop Node.js Workshop

mobile/web clients

social & analytics

JSON API

API XML SQL

SOAP Etc.

mBaaS

Mobile API tier

Enterprise  

Pattern 5: mBaaS

Page 34: Seattle StrongLoop Node.js Workshop

mBaaS could be on cloud or premises

u  Closed Source / Commercial

u  Open Source Key API u  Push Notification u  Geo Location u  Offline Sync u  Storage u  User Management u  Metering, Analytics u  Native SDK u  Isomorphic JS

Page 35: Seattle StrongLoop Node.js Workshop

GeoLocation in Loopback.io

Page 36: Seattle StrongLoop Node.js Workshop

Push Notification in Loopback.io

Page 37: Seattle StrongLoop Node.js Workshop

Storage Service in Loopback.io

37

Page 38: Seattle StrongLoop Node.js Workshop

Wait …what is this API Gateway thingy then ?

Page 39: Seattle StrongLoop Node.js Workshop

Synchronous API “Re-Composition” is an anti-pattern

Page 40: Seattle StrongLoop Node.js Workshop

Security & Social Logins

Why not an async API Gateway* ? Pattern 6

API Clients API Gateway API Server

API

*Releasing in a sprint !

Page 41: Seattle StrongLoop Node.js Workshop

Internet of Everything

The devices are taking over !!!

Microsecond latencies are the norm

Page 42: Seattle StrongLoop Node.js Workshop

API “Decomposition” is the game changer

{JSON} {JSON}

{JSON}

<SOAP/XML>

<TABLE>

Web SaaS Mobile loT

HTML JSON

App Server API Server

Page 43: Seattle StrongLoop Node.js Workshop

Pattern 7: Micro-services (Design Time)

Client SDK

REST Endpoints

Store Cache

API Micro Services

One URL Space

SL API PaaS REST/JSON

API Gateway

Router Remoting Model

API Orchestrator

Cloud Services

Model

Model

Connector

Connector

Connectors

Page 44: Seattle StrongLoop Node.js Workshop

Now I know !!!

Page 45: Seattle StrongLoop Node.js Workshop

StrongLoop – node.js Development to Production

Build and Deploy

Automate Lifecycle

Performance Metrics

Real-time production monitoring

Profiler Root cause

CPU & Memory API Composer Visual modeling

StrongLoop Arc

Process Manager

Scale applications"

Q2 2015

Mesh Deploy

containerized

ORM, mBaaS, Realtime

Page 46: Seattle StrongLoop Node.js Workshop
Page 47: Seattle StrongLoop Node.js Workshop

Loopback: Open Source nirvana (mBaaS, ORM, Micro)

REST API

PUSH

GEO OFF SYNC

DEVICE

USER FILE

API GATEWAY

Channel SDKs

API ENGINE

CONNECTORs

Relational Database Clustered Database Security Gateway Cloud Firewall SOA Bus ( not sure on this one)

Laptop ( not requested but useful)

Connections

Tablet Phone IoT SDK API SERVER Items for ACL

Quick Example of Use

App Server Add On

App Server

ORM

ACLs

Relational Database Clustered Database Security Gateway Cloud Firewall SOA Bus ( not sure on this one)

Laptop ( not requested but useful)

Connections

Tablet Phone IoT SDK API SERVER Items for ACL

Quick Example of Use

App Server Add On

App Server

Relational Database Clustered Database Security Gateway Cloud Firewall SOA Bus ( not sure on this one)

Laptop ( not requested but useful)

Connections

Tablet Phone IoT SDK API SERVER Items for ACL

Quick Example of Use

App Server Add On

App Server

Relational Database Clustered Database Security Gateway Cloud Firewall SOA Bus ( not sure on this one)

Laptop ( not requested but useful)

Connections

Tablet Phone IoT SDK API SERVER Items for ACL

Quick Example of Use

App Server Add On

App Server

Data and Services

Remoting

Pub-Sub

Page 48: Seattle StrongLoop Node.js Workshop

Data & Services Integration

Services

Relational Database Clustered Database Security Gateway Cloud Firewall SOA Bus ( not sure on this one)

Laptop ( not requested but useful)

Connections

Tablet Phone IoT SDK API SERVER Items for ACL

Quick Example of Use

App Server Add On

App Server

Storage

Relational Database Clustered Database Security Gateway Cloud Firewall SOA Bus ( not sure on this one)

Laptop ( not requested but useful)

Connections

Tablet Phone IoT SDK API SERVER Items for ACL

Quick Example of Use

App Server Add On

App Server

REST

Messaging

Database

NoSQL

Marquee - Discovery & Migration !

Page 49: Seattle StrongLoop Node.js Workshop

Mobile Backend Services

Offline Sync

Geo Location

Push Notification

Storage

Page 50: Seattle StrongLoop Node.js Workshop

SDKs, Isomorphic JS, Offline Sync & Replication

JS client

JS Server

Remoting

JS client Replication

Developer SDKs

Page 51: Seattle StrongLoop Node.js Workshop

Pub-Sub

JS client

JS Server

Remoting

JS client Replication

HTTP Proxy

Page 52: Seattle StrongLoop Node.js Workshop

Pub-Sub Repos

strong-pubsub (Client = require(‘strong-pubsub’)) strong-pubsub-proxy strong-pubsub-mqtt strong-pubsub-redis strong-pubsub-connection-mqtt strong-pubsub-primus (browser)

52

Page 53: Seattle StrongLoop Node.js Workshop

Pub-Sub architecture

53

Page 54: Seattle StrongLoop Node.js Workshop

API Explorer (Swagger) – Iterative build and test w/o frontend

Page 55: Seattle StrongLoop Node.js Workshop

Right interface for the right user

  Arc UI – architect view –  Widget driven –  Visual composition,

management & operations –  Marquee featured, evolving

platform

  StrongLoop Console (slc) - developer view –  Command Line Interface –  Full featured –  Greater customization

support

Page 56: Seattle StrongLoop Node.js Workshop

Runtime Capabilities

Page 57: Seattle StrongLoop Node.js Workshop

Debugging

Page 58: Seattle StrongLoop Node.js Workshop

Debugging server side clustered apps

slc debug app.js

Debugging single process

slc debug app.js http://localhost:8080/debug?port=<5858+ID-of-process>

Debugging clustered process in V11

%node debug –p <PID> connecting…ok … debug>quit %node-inspector http://127.0.0.1:8080/debug?port =5858 Or process._debugPort = 5858 + cluster.worker.id

Debugging clustered process in V10

Page 59: Seattle StrongLoop Node.js Workshop

Memory Leaks ?

Page 60: Seattle StrongLoop Node.js Workshop

How does GC work

  Concept of reachability.   Roots: Reachable or in live scope objects   Include objects referenced from anywhere in the call stack (all local variables and

parameters in the functions currently being invoked), and any global variables.   Objects are kept in memory while accessible from roots through a reference or a

chain of references.   Root objects are pointed directly from V8 or the Web browser like DOM elements   Garbage collector identifies dead memory regions/unreachable objects through a

chain of pointers from a live object; reallocates or releases them to the OS

Page 61: Seattle StrongLoop Node.js Workshop

Easy right ? Hell No !!! Pause and then Stop the World

V8 essentially:   stops program execution when

performing a garbage collection cycle.   processes only part of the object heap

in most garbage collection cycles. This minimizes the impact of stopping the application.

  Accurately keeps all objects and pointers in memory. This avoids falsely identifying objects as pointers which can result in memory leaks.

  In V8, the object heap is segmented into many parts; hence If an object is moved in a garbage collection cycle, V8 updates all pointers to the object.

Page 62: Seattle StrongLoop Node.js Workshop

Short, Full GC and some algorithms to know

V8 divides the heap into two generations: Short GC/scavenging

Objects are allocated in “new-space” (between 1 and 8 MB). Allocation in new space is very cheap; increment an allocation pointer when we want to reserve space for a new object. When the pointer reaches the end of new space, a scavenge (minor garbage collection cycle) is triggered, quickly removing dead objects from new space.

  large space overhead, since we need physical memory backing both to-space and from-space.

  Fast by design, hence using for short GC cycles. Acceptable if new space is small – a few Mbs

Full GC/mark-sweep & mark-compact Objects which have survived two minor garbage collections are promoted to “old-space.” Old-space is garbage collected in full GC (major garbage collection cycle), which is much less frequent. A full GC cycle is triggered based on a memory threshold

  To collect old space, which may contain several hundred megabytes of data, we use two closely related algorithms, Mark-sweep and Mark-compact.

Page 63: Seattle StrongLoop Node.js Workshop

New Algorithm implementation

Incremental marking & lazy sweeping   In mid-2012, Google introduced two improvements that reduced garbage

collection pauses significantly: incremental marking and lazy sweeping.   Incremental marking means being able to do a bit of marking work, then let the

mutator (JavaScript program) run a bit, then do another bit of marking work.   Short pauses in the order of 5-10 ms each as an example for marking.   Threshold based. At every alloc execution is paused to perform an incremental

marking step.

  Lazy sweep cleans up set of objects at time eventually cleaning all pages.

Page 64: Seattle StrongLoop Node.js Workshop

Analyze, Analyze, Analyze

Page 65: Seattle StrongLoop Node.js Workshop

heapdump for V8 snapshots by StrongLoop

npm install heapdump   Add to app : var heapdump = require(‘heapdump’)

  Method 1 : writeSnapshot

  Method 2 : SIGUSR2 (Unix only)

  Make sure your directory is writable

var heapdump = require('heapdump') ... heapdump.writeSnapshot()

kill –USR2 <pid>

process.chdir('/path/to/writeable/dir’)

Page 66: Seattle StrongLoop Node.js Workshop

heapdump for V8 snapshots by StrongLoop

  Programmatic heap snapshots (timer based)

  Programmatic heap snapshots (threshold based)

var heapdump = require('heapdump') ... setInterval(function () { heapdump.writeSnapshot() }, 6000 * 30) <strong>(1)</strong>

var heapdump = require('heapdump') var nextMBThreshold = 0 <strong>(1)</strong> setInterval(function () { var memMB = process.memoryUsage().rss / 1048576 <strong>(2)</strong> if (memMB &gt; nextMBThreshold) { <strong>(3)</strong> heapdump.writeSnapshot() nextMBThreshold += 100 } }, 6000 * 2) <strong>(4)</strong>

Page 67: Seattle StrongLoop Node.js Workshop

Simpler – slc arc and remote profiling

Page 68: Seattle StrongLoop Node.js Workshop

CPUs smoke too !

Page 69: Seattle StrongLoop Node.js Workshop

CPU profiler : slc cpu-start/stop or just slc arc

Page 70: Seattle StrongLoop Node.js Workshop

How we fixed a real production problem

Page 71: Seattle StrongLoop Node.js Workshop

Performance 1 on 1: Don’t Block the EventLoop

Page 72: Seattle StrongLoop Node.js Workshop

Blocked event loop in Meteor Atmosphere

  node-fibers implements co-routines. Meteor uses this to hack local thread storage allowing V8 to run multiple execution contexts each mapped to a co-routine.

FindOrAllocatePerThreadDataForThisThread() used in switching context between co-routines

  Co-routines are cooperative; the current coroutine has to yield control before another one can

run and that is what Meteor does in its process.nextTick() callback; it essentially builds concurrent (but not parallel) green threads on a round-robin scheduler

  Too many tiny tasks and not one long running one was blocking the event loop

process.nextTick() has a failsafe mechanism where it will process “x” tick callbacks before deferring the remaining ones to the next event loop tick.

  Native MongoDB driver disabled the failsafe to silence a warning message in node v0.10 about

maxtickDepth being reached

ticks parent name 2274 7.3% v8::internal::Isolate::FindOrAllocatePerThreadDataForThisThread() 1325 58.3% LazyCompile: ~<anonymous> packages/meteor.js:683 1325 100.0% LazyCompile: _tickCallback node.js:399

Page 73: Seattle StrongLoop Node.js Workshop

The solution   The workaround: switch fromprocess.nextTick() to setImmediate()

Page 74: Seattle StrongLoop Node.js Workshop

Deep Transaction Tracing

Page 75: Seattle StrongLoop Node.js Workshop

Real time Production Monitoring

Page 76: Seattle StrongLoop Node.js Workshop

3rd Party Metrics integration

•  Arc •  Graphite •  Splunk •  Datadog •  CA APM •  Sumologic

Page 77: Seattle StrongLoop Node.js Workshop

Build & Deploy

Page 78: Seattle StrongLoop Node.js Workshop

Horizontal and Vertical Scaling with Process Manager

Page 79: Seattle StrongLoop Node.js Workshop

Micro services scaling

Page 80: Seattle StrongLoop Node.js Workshop

Hyper-scale & Micro-services Deployment

Page 81: Seattle StrongLoop Node.js Workshop

First there was Node

Thank you!

JUST WIN,BABY!