Upgrade to Pro — share decks privately, control downloads, hide ads and more …

NodeJS_API_Meetup_LA_SanDiego

Avatar for ShubhraKar ShubhraKar
October 31, 2014

 NodeJS_API_Meetup_LA_SanDiego

Workshop: Develop, Deploy, Monitor and Scale REST APIs Built in Node.js. Framework used is Loopback.io from StrongLoop.

Avatar for ShubhraKar

ShubhraKar

October 31, 2014

Other Decks in Technology

Transcript

  1. Develop, Monitor and Scale REST APIs in Node.js Shubhra Kar

    | Director – Products twitter:@shubhrakar mail:[email protected]
  2. APIs are powering this change 4 SaaS Mobile IoT SOA

    Web App Server Database Datacube API Server HTML JSON
  3. Distinctly unique Approaches to solve the same problem Let’s just

    RESTify our current SOA to solve this Architect Developer
  4. What backend is being used for APIs ? 7 Node

    is FAST Node is perfect for APIs Node powers full-stack JS …and highly concurrent!
  5. What is Node.js? •  Asynchronous, event-driven, non-blocking I/O platform that

    is perfect for real- time, data-intensive applications. •  Single thread of execution •  Built on Chrome’s JavaScript runtime engine, V8 •  Uses Libuv, a high performance evented I/O library that works on Linux and Windows (faster than Libev) while there are still events to process: e = get the next event if there is a callback associated with e: call the callback
  6. Behind the Curtain Node Bindings C/C++ (socket, http, etc.) V8

    Libuv Libeio (thread pool) Libev (event loop) IOCP (IO completion ports) Node Standard Library (JavaScript) epoll/kqueue/poll (event ports) Linux Kernel Windows Kernel Yes, it’s C back there
  7. Full Stack JavaScript is powering mobility Angular Bootstrap Ionic Cordova

    A Compelling Option for Enterprise Mobility ü  Faster time to market vs. 100% native development ü  Reuse skills of existing web developer staff ü  Open Source stack / no vendor lock-in ü  For Enterprise apps, performance is comparable to native apps ü  Near “write once, run anywhere” UI: tablets, phones, smart TVs, smart watches, cars, etc. Gartner predicts by 2016 more than 50% of the apps deployed will be Hybrid apps. LoopBack API Server Node.js
  8. But is the backend really being written in JS ?

    •  Node.js active contributors (>65K) •  Very large and active developer community •  Growing / most modules (100K > Java, Ruby, PHP…)
  9. Pros Great starting point Low learning curve Nearly a standard

    for Node.js web middleware Fully customizable Targeted at browser, hence templating and rendering OOB Cons All endpoints need to be created manually Developers may end up creating their own non-optimized libraries Every end point needs to be tested Refactoring is painful, update everything everywhere No standards or prescription, DIY Express
  10. Pros Very stable and actively developed. Automatic DTrace support for

    all handlers on enabled platforms Built in throttling SPDY support Borrows from express but without unnecessary templating/rendering Strict API with full control over HTTP interactions Visibility into latency and app characteristics Cons All endpoints need to be created manually Developers may end up creating their own non-optimized libraries Every end point needs to be tested Refactoring is painful, update everything everywhere No standards or prescription, DIY Less documentation Restify
  11. Pros Supported by Walmart labs. Less known but getting momentum

    Very granular control over request handling Detailed API reference with support for document generation Significantly more functionality for building web servers Pseudo prescriptive with configuration centric approach Caching, Authentication and input validation available Plugin based architecture for scaling Cons Good construction blocks but left to your own devices to figure out design patterns Binds you to specific modules and tool chains (catbox, joi, tv, boom, good, travelogue, yar) All endpoints need to be created manually Developers may end up creating their own non-optimized libraries Every end point needs to be tested Refactoring is painful, update everything everywhere 24
  12. Pros Very quick RESTful API development Convention over configuration (extends

    express) Built in models ready to use (ORM) RPC support, Web and Mobile SDKs Enterprise data connectors (10+ out of box) and Data Juggler Fully configurable when needed Extensive documentation Commercial support and full time team working on project Mobile Services (Push, Geo, Storage, Sync, Offline, File, Device, User) Authentication, validation and Authorization DevOps tools packaged for debugging, monitoring, profiling, scaling and production support Cons Higher learning curve
  13. The emergence of the API economy Compilers AppServer PaaS mBaaS

    API Server Speed of Development Cost, Control, Data
  14. REST API PUSH GEO OFF SYNC DEVICE USER FILE Relational

    Database Clustered Database Security Gateway Cloud Laptop ( not requested but useful) Connections Tablet Phone IoT SDK API SERVER Quick Example of Use App S Add 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 API GATEWAY Channel SDKs ORM API ENGINE C O N N E C T O R s A P I S D K s So some developers wrote an Open source API server REST API atabase Security Gateway Cloud Firewall SOA Bus ( not sure on this one) Phone IoT SDK API SERVER Items for ACL Quick Example of Use App Server Add On App Server API Explorer Debugging, Provisioning, Scaling, Monitoring
  15. These guys sent me ! Bert Belder Ben Noordhuis Node

    Core Raymond Feng Ritchie Martori LoopBack / Express Core Sam Roberts Miroslav Bajtos Ryan Graham
  16. Loopback Open-source Node.js Framework $ slc loopback _-----_ | |

    .--------------------------. |--(o)--| | Let's create a LoopBack | `---------´ | application! | ( _´U`_ ) '--------------------------' /___A___\ | ~ | __'.___.'__ ´ ` |° ´ Y ` [?] Enter a directory name where to create the project: (.) u  Middleware API Engine u  ORM u  Aggregation & Mashups u  API Explorer (Swagger) u  Fine Grained Access Control (ACLs) u  Data Replication u  IsoMorphic JS and Client SDKs.
  17. Open Source Studio u  Visual ORM u  Discovery u  Migration

    u  DEV - API Design & Composition u  OPS – Provisioning, Scaling & Monitoring
  18. Open Source mBaaS (mobile Backend as a Service) u  Push

    Notification u  GeoLocation u  Offline Sync u  3rd Party APIs u  Device / User Tracking u  Cloud / File Storage
  19. Open Source API Gateway u  Security (AAA) u  Mediation and

    Transformation u  Infrastructure QoS (pagination, throttling, caching, delivery guarantee, firewall) u  Analytics and health monitoring u  Aggregation and Microservices Routing u  Virtualization and Reverse Proxy
  20. Connectors u  Connection Management u  RDBMS, NoSQL, Services, BigData, Proprietary

    u  Model Discovery u  Data Replication u  State Management u  Storage Open Source Enterprise Relational Database Clustered Database Security Gateway Cloud Firewall SOA Bus ( not sure on this o 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
  21. The boss does have valid questions L I recommend Node.js

    for our API strategy Architect The Boss Node is nerdy like systems programming !...Where are tools ? Do we have an IDE ?
  22. Node.js APIs actually have a life cycle 49 Compose Deploy

    / Scale Monitor Secure / Manage API Studio Controller StrongOps API Gateway Development Production Reliable API Solutions powered by Node.js
  23. Let’s start by debugging •  Chrome debugger •  Debug Remotely

    on Production •  Breakpoints & uncaught exceptions •  Source Maps •  Unit test integration •  Display/edit variables •  Pre-loaded breaks Known as Node-Inspector / StrongLoop-Debug
  24. 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
  25. But is it enough to convince him ? Plus I

    can now diagnose CPU hotspots and Memory Bottlenecks Architect The Boss Node is fast you said !...what bottlenecks and leaks ?
  26. How to diagnose CPU hotspots upto line of code ?

    Enter StrongLoop OSS - Node Profiler slc runctl cpu-start/stop PID >> CPU Proc files
  27. How about a fancy chart for my boss ? Call

    Stack Path and function CPU Cycle times
  28. heapdump for V8 snapshots " 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’)
  29. Heap Snapshot strategies "   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>
  30. We are getting there… See we have great Dev Tools

    Architect The Boss Not at 3:00 a.m. in the night !….Show me its Production ready
  31. On-Premises / 3rd Party monitoring ? 63 •  Graphite • 

    Splunk •  Datadog •  Introscope •  Others
  32. And On-Demand Dynamic Instrumentation Agent App Monkey Patching Agent App

    Dynamic Instrument •  Live Edit •  Line Level Instrumentation •  Any package, any framework, any code •  Custom logic •  Counters, Gauges and Timers •  HA rollback
  33. Almost there … Look I got monitoring Architect The Boss

    Good…but before I promote you, show me some scale
  34. Vertical and pseudo-horizontal scaling •  OSS Controller •  Cluster Mgmt.

    •  Hot Deploy •  Rolling Restart •  Cluster State Mgmt. •  Process Mgmt. •  Deploy
  35. MESH – All things distributed •  Distributed Deploy •  Cross

    MC state •  HA •  Docker Containers •  Auto Scaling •  Process Mgmt.
  36. StrongLoop OSS API Server "   Express "   Loopback

    API Framework "   API Gateway "   Controller (Mesh) "   Strong-agent – Monitoring "   API Explorer (Swagger) "   Mobile SDKs "   Optimized node modules "   Connectors to enterprise systems