seattle strongloop node.js workshop
TRANSCRIPT
Shubhra Kar | Products & Education twitter:@shubhrakar
{“Node.js”: “APIs @hyperscale”}
About me
u J2EE and SOA architect
u Performance architect
u Node, mBaaS & APIs
These guys sent me !
Bert Belder
Ben Noordhuis
Node/io core
Raymond Feng
Ritchie Martori
LoopBack & Express core
Sam Roberts
Miroslav Bajtos
Ryan Graham
Buzzwords
Websites
Smart Mobiles
Online Catalog
Social Media
Kiosks
Computers
Physical Store
Gaming Consoles
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
Multi Threaded Server
Node.js Server
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 !
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
Nodies are not just Silicon Valley hipsters !
Getting Started Tips for
Converts
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();
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
No I am serious
JVM != V8 Bytecode = JIT compilation Package library = module JaxRS = remoting EJB = microservice Controller = router Java Model Class = JSON Model
Big Picture?
Evolution is inevitable !
Thick
Web SaaS Mobile IoT
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
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
Pattern 1: Convention
HTTP Server Library REST HTTP Library
Simple MVC Structure
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)
Router in
Express
StrongLoop Confiden.al and Proprietary Informa.on – © 2015
Router in
Restify
StrongLoop Confiden.al and Proprietary Informa.on – © 2015
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)
Router in
hapi
StrongLoop Confiden.al and Proprietary Informa.on – © 2015
Pattern 3 : Full Stack JS à lends into pattern 4
{“..”} JSON is the new first class citizen
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
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
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
ORM in Loopback.io
Sub-pattern : Schema Discovery
Isomorphic JS in Loopback.io
JS client
JS Server
Remoting
Router & Swagger
in Loopback
StrongLoop Confiden.al and Proprietary Informa.on – © 2015
Router using Sails
mobile/web clients
social & analytics
JSON API
API XML SQL
SOAP Etc.
mBaaS
Mobile API tier
Enterprise
Pattern 5: mBaaS
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
GeoLocation in Loopback.io
Push Notification in Loopback.io
Storage Service in Loopback.io
37
Wait …what is this API Gateway thingy then ?
Synchronous API “Re-Composition” is an anti-pattern
Security & Social Logins
Why not an async API Gateway* ? Pattern 6
API Clients API Gateway API Server
API
*Releasing in a sprint !
Internet of Everything
The devices are taking over !!!
Microsecond latencies are the norm
API “Decomposition” is the game changer
{JSON} {JSON}
{JSON}
<SOAP/XML>
<TABLE>
Web SaaS Mobile loT
HTML JSON
App Server API Server
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
Now I know !!!
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
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
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 !
Mobile Backend Services
Offline Sync
Geo Location
Push Notification
Storage
SDKs, Isomorphic JS, Offline Sync & Replication
JS client
JS Server
Remoting
JS client Replication
Developer SDKs
Pub-Sub
JS client
JS Server
Remoting
JS client Replication
HTTP Proxy
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
Pub-Sub architecture
53
API Explorer (Swagger) – Iterative build and test w/o frontend
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
Runtime Capabilities
Debugging
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
Memory Leaks ?
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
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.
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.
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.
Analyze, Analyze, Analyze
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’)
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 > nextMBThreshold) { <strong>(3)</strong> heapdump.writeSnapshot() nextMBThreshold += 100 } }, 6000 * 2) <strong>(4)</strong>
Simpler – slc arc and remote profiling
CPUs smoke too !
CPU profiler : slc cpu-start/stop or just slc arc
How we fixed a real production problem
Performance 1 on 1: Don’t Block the EventLoop
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
The solution The workaround: switch fromprocess.nextTick() to setImmediate()
Deep Transaction Tracing
Real time Production Monitoring
3rd Party Metrics integration
• Arc • Graphite • Splunk • Datadog • CA APM • Sumologic
Build & Deploy
Horizontal and Vertical Scaling with Process Manager
Micro services scaling
Hyper-scale & Micro-services Deployment
First there was Node
Thank you!
JUST WIN,BABY!