Advertisement

QEWD.js: Have your Node.js Cake and Eat It Too

Rob Tweed
IT Consultant, Developer & Director/Founder at M/Gateway Developments Ltd
May. 31, 2017
Advertisement

More Related Content

Slideshows for you(20)

Similar to QEWD.js: Have your Node.js Cake and Eat It Too(20)

Advertisement
Advertisement

QEWD.js: Have your Node.js Cake and Eat It Too

  1. Copyright © 2014 M/Gateway Developments Ltd QEWD.js Have Your Node.js Cake and Eat It Too Rob Tweed M/Gateway Developments Ltd Twitter: @rtweed http://www.mgateway.com
  2. Copyright © 2014 M/Gateway Developments Ltd
  3. Copyright © 2014 M/Gateway Developments Ltd QEWD.js • Just another Node.js framework? • What's wrong with the ones already available and used by thousands of developers? – Express.js – Koa – Meteor – Hapi – Sails, etc etc
  4. Copyright © 2014 M/Gateway Developments Ltd Two reasons • Achieving the best interface to Cache & GT.M from JavaScript – JavaScript as a first-class language and direct alternative to Cache ObjectScript / Mumps – Integrating with legacy Cache ObjectScript / Mumps code that was inherently synchronous
  5. Copyright © 2014 M/Gateway Developments Ltd Two reasons • Achieving the best interface to Cache & GT.M from JavaScript – JavaScript as a first-class language and direct alternative to Cache ObjectScript / Mumps – Integrating with legacy Cache ObjectScript / Mumps code that was inherently synchronous – I've been working with Node.js & Cache/Mumps since 2011 and have tried everything you can imagine to figure this out!
  6. Copyright © 2014 M/Gateway Developments Ltd Two reasons • Achieving the best interface to Cache & GT.M from JavaScript – JavaScript as a first-class language and direct alternative to Cache ObjectScript / Mumps – Integrating with legacy Cache ObjectScript / Mumps code that was inherently synchronous • Addressing some of the deficiencies in Node.js that are a direct consequence of its architecture
  7. Copyright © 2014 M/Gateway Developments Ltd Node.js Architecture • Everything executes in a single process – You might have 1000's of users doing stuff concurrently • They're all sharing the same process!
  8. Copyright © 2014 M/Gateway Developments Ltd By Design • Ryan Dahl, the inventor of Node.js, wanted to create a highly-scalable, high- performance architecture for network programs – Web servers – Chat servers • Showed that the threaded architecture of Apache had performance limits and heavy memory usage
  9. Copyright © 2014 M/Gateway Developments Ltd Node.js Architecture • Everything executes in a single process • Non-blocking I/O – No user activity must block the process, or everyone grinds to a halt • Event-driven, asynchronous logic – Fire off the request, but don't wait for the results • Carry on with the next task – When results come back, handle them at the next opportunity
  10. Copyright © 2014 M/Gateway Developments Ltd Node.js = server-side JavaScript • Ryan Dahl wasn't actually a big JavaScript fan • However, he realised that it was a convenient language for such an environment, as it's designed for exactly the same kind of way of working in the browser • Google's V8 JavaScript engine was Open Sourced, so he used it to provide the language for Node.js – (which is actually mostly written in C++)
  11. Copyright © 2014 M/Gateway Developments Ltd Asynchronous Syntax in Other Languages? • Node.js isn't unique in supporting asynchronous logic and non-blocking I/O • Available also in most other modern languages – Java – Python, etc • Allows efficient use of resources when making parallel requests for resources – Files, external web services etc
  12. Copyright © 2014 M/Gateway Developments Ltd Asynchronous Syntax in Node.js • Node.js is unique in that you have no option but to use Asynchronous logic
  13. Copyright © 2014 M/Gateway Developments Ltd Node.js Popularity • Launched in 2009 • Now hugely popular and continuing to grow in popularity – Server-side of web / mobile applications • One reason is that just one language – JavaScript – is needed for all parts of an application – Single skill-set
  14. Copyright © 2014 M/Gateway Developments Ltd Drawbacks of Node.js • Even its greatest exponents will tell you not to use Node.js for certain things • In every other language, zealots will encourage you to use it for everything!
  15. Copyright © 2014 M/Gateway Developments Ltd Drawbacks of Node.js • Even its greatest exponents will tell you not to use Node.js for certain things – CPU-intensive logic – Complex database manipulation • Particularly relational database handling
  16. Copyright © 2014 M/Gateway Developments Ltd Drawbacks of Node.js • Even its greatest exponents will tell you not to use Node.js for certain things – CPU-intensive logic • Tie up the CPU and everyone else using the process will be blocked – Complex database manipulation • Particularly relational database handling
  17. Copyright © 2014 M/Gateway Developments Ltd Drawbacks of Node.js • Even its greatest exponents will tell you not to use Node.js for certain things – CPU-intensive logic – Complex database manipulation • Particularly relational database handling – Due to the limitations of asynchronous logic, eg you can't chain functions
  18. Copyright © 2014 M/Gateway Developments Ltd Usual Solutions • Use a third-party queue to offload CPU- intensive work to some other environment – RabbitMQ – ZeroMQ • Use another language such as Rails for database-intensive work
  19. Copyright © 2014 M/Gateway Developments Ltd The down-sides • Heterogeneous environment • Multiple skill-sets • Multiple moving parts Node.js RabbitMQ Rails Java Database
  20. Copyright © 2014 M/Gateway Developments Ltd The down-sides • Heterogeneous environment • Multiple skill-sets • Multiple moving parts Node.js RabbitMQ Rails Java DatabaseReminds you of EHMP?
  21. Copyright © 2014 M/Gateway Developments Ltd Cache/Mumps integration • Ideally using synchronous logic – Especially if you have to elegantly and easily handle locking in legacy code • Ideally abstract Cache/Mumps databases in a way that makes intuitive sense to a JavaScript developer • Asynchronous logic for complex database manipulation is painful, time-consuming and difficult to understand/maintain
  22. Copyright © 2014 M/Gateway Developments Ltd Async is the New Sync? • The Node.js community are well aware of the need to avoid "callback hell" caused by asychronous logic • Next version of Node.js will support Async/Await which greatly improves the syntax – Very like synchronous logic
  23. Copyright © 2014 M/Gateway Developments Ltd Async/Await Doesn't solve: • Node.js concurrency – Still need to avoid CPU-intensive code • High-level database abstractions – Proper chaining of functions requires synchronous logic
  24. Copyright © 2014 M/Gateway Developments Ltd I want my Node.js Cake & Eat it • I like JavaScript • I want just one language for everything – 1 skill set – 1 set of moving parts • No extra technologies, thank you – Keep it simple
  25. Copyright © 2014 M/Gateway Developments Ltd The Problem is Concurrency • All concurrent users in Node.js share the same process
  26. Copyright © 2014 M/Gateway Developments Ltd The Problem is Concurrency • All concurrent users in Node.js share the same process • As it happens, Amazon Web Services accidentally created a solution – And nobody (including AWS) seems to have realised the consequences of what they've done
  27. Copyright © 2014 M/Gateway Developments Ltd AWS Lambda • "Function As A Service" • AKA "Serverless"
  28. Copyright © 2014 M/Gateway Developments Ltd AWS Lambda • "Function As A Service" • AKA "Serverless" • You upload functions • AWS will execute them – You don't worry about how or on what physical machine(s) • You pay per invocation of your function(s)
  29. Copyright © 2014 M/Gateway Developments Ltd AWS Lambda • The first technology they supported was Node.js
  30. Copyright © 2014 M/Gateway Developments Ltd AWS Concurrency? • Your function will be invoked in a private computation container of some sort • Your function has that container all to itself for the duration of its execution • No competition with other concurrent users
  31. Copyright © 2014 M/Gateway Developments Ltd Lambda Node.js Examples • Examples show use of asynchronous APIs • Node.js users of Lambda use asynchronous APIs • They're even excited about the possibility of using Async/Await in Lambda
  32. Copyright © 2014 M/Gateway Developments Ltd Lambda Node.js Examples • Examples show use of asynchronous APIs • Node.js users of Lambda use asynchronous APIs • They're even excited about the possibility of using Async/Await in Lambda • But unless your Lambda function really needs to be asynchronous, why use asynchronous logic?
  33. Copyright © 2014 M/Gateway Developments Ltd Asynchronous Syntax in Other Languages? • Available also in most other modern languages – Java – Python, etc • Allows efficient use of resources when making parallel requests for resources – Files, external web services etc • But no programmer in those languages would use async logic if they didn't have to
  34. Copyright © 2014 M/Gateway Developments Ltd Async is the New Sync? • Why would Node.js developers use asynchronous logic in Lambda functions if they don't have to? – Partly dogma
  35. Copyright © 2014 M/Gateway Developments Ltd Async is the New Sync? • Why would Node.js developers use asynchronous logic in Lambda functions if they don't have to? – Partly dogma – Partly because almost no synchronous APIs exist, particularly for: • Database integration • Web/REST service access
  36. Copyright © 2014 M/Gateway Developments Ltd Such APIs are possible
  37. Copyright © 2014 M/Gateway Developments Ltd Such APIs are possible
  38. Copyright © 2014 M/Gateway Developments Ltd Any other way… • To have your Node.js cake and eat it? – Not everyone will want to use Lambda • Would it be possible to create a locally- available environment where my Node.js code runs in an isolated container where concurrency isn't an issue?
  39. Copyright © 2014 M/Gateway Developments Ltd
  40. Copyright © 2014 M/Gateway Developments Ltd QEWD.js Node.js Worker Process Master Node.js Process Queue Queue processor/ dispatcher qewd ewd-qoper8-gtm Cache/GT.M ewd-document-store Global Storage ewd-session Express HTTP(S) Interface Browser/RESTClient Custom Worker Module ewd-qoper8 Constructed from a number of EWD 3 modules Legacy Code WebSocket Interface
  41. Copyright © 2014 M/Gateway Developments Ltd QEWD.js Node.js Worker Process Master Node.js Process Queue Queue processor/ dispatcher qewd ewd-qoper8-gtm Cache/GT.M ewd-document-store Global Storage ewd-session Koa.js HTTP(S) Interface Browser/RESTClient Custom Worker Module ewd-qoper8 Can also use Koa.js – next generation Web Server Legacy Code WebSocket Interface
  42. Copyright © 2014 M/Gateway Developments Ltd QEWD.js Node.js Worker Process Master Node.js Process Queue Queue processor/ dispatcher qewd ewd-qoper8-redis Redis ewd-document-store Global Storage ewd-session Koa.js HTTP(S) Interface Browser/RESTClient Custom Worker Module ewd-qoper8 Can even use Redis as if it was a Global Storage DB WebSocket Interface
  43. Copyright © 2014 M/Gateway Developments Ltd QEWD's Architecture • Master Process – Handles all incoming REST requests • Via Express • Queues and dispatches requests to worker processes • Worker Processes – You define how large a pool of workers you require – Each worker handles just one request at a time (unlike Node.js Cluster)
  44. Copyright © 2014 M/Gateway Developments Ltd Master Node.js Process Queue Queue processor/ dispatcher Incoming Request REST HTTP WebSocket QEWD Architecture Every incoming request is passed from Express and placed in a queue No further processing of requests occurs in the master process
  45. Copyright © 2014 M/Gateway Developments Ltd QEWD Architecture Master Node.js Process Queue Queue processor/ dispatcher Queue dispatcher is invoked whenever a request is added to the queue
  46. Copyright © 2014 M/Gateway Developments Ltd QEWD Architecture Node.js Worker Process Master Node.js Process Queue Queue processor/ dispatcher Worker process started if none available
  47. Copyright © 2014 M/Gateway Developments Ltd QEWD Architecture Node.js Worker Process Master Node.js Process Queue Queue processor/ dispatcher qewd-ripple Handler module Redis/Cache/GT.M QEWD & application-specific Modules loaded and connected to database: Redis, Cache, GT.M
  48. Copyright © 2014 M/Gateway Developments Ltd QEWD Architecture Node.js Worker Process Master Node.js Process Queue Queue processor/ dispatcher Custom Worker Module Node.js Worker Process Custom Worker Module Request passed to worker Redis/Cache/GT.M
  49. Copyright © 2014 M/Gateway Developments Ltd QEWD Architecture Node.js Worker Process Master Node.js Process Queue Queue processor/ dispatcher Custom Worker Module Worker flagged as Unavailable Node.js Worker Process Custom Worker Module Begin processing message Redis/Cache/GT.M
  50. Copyright © 2014 M/Gateway Developments Ltd QEWD Architecture Master Node.js Process Queue Queue processor/ dispatcher Unavailable / processing Another incoming request Node.js Worker Process Custom Worker Module Node.js Worker Process Custom Worker Module Redis/Cache/GT.M
  51. Copyright © 2014 M/Gateway Developments Ltd QEWD Architecture Node.js Worker Process Master Node.js Process Queue Queue processor/ dispatcher Custom Worker Module Unavailable / processing Node.js Worker Process Custom Worker Module Node.js Worker Process Custom Worker Module Node.js Worker Process Custom Worker Module If worker pool size not exceeded, another worker is started and request passed to it Redis/Cache/GT.M Redis/Cache/GT.M
  52. Copyright © 2014 M/Gateway Developments Ltd QEWD Architecture Master Node.js Process Queue Queue processor/ dispatcher Unavailable / processing Node.js Worker Process Custom Worker Module Node.js Worker Process Custom Worker Module Redis/Cache/GT.M If entire Worker Pool is busy: Unavailable / processing Node.js Worker Process Custom Worker Module Node.js Worker Process Custom Worker Module Redis/Cache/GT.M Unavailable / processing Node.js Worker Process Custom Worker Module Node.js Worker Process Custom Worker Module Redis/Cache/GT.M
  53. Copyright © 2014 M/Gateway Developments Ltd QEWD Architecture Master Node.js Process Queue Queue processor/ dispatcher If entire Worker Pool is busy: New requests remain in queue Unavailable / processing Node.js Worker Process Custom Worker Module Node.js Worker Process Custom Worker Module Redis/Cache/GT.M Unavailable / processing Node.js Worker Process Custom Worker Module Node.js Worker Process Custom Worker Module Redis/Cache/GT.M Unavailable / processing Node.js Worker Process Custom Worker Module Node.js Worker Process Custom Worker Module Redis/Cache/GT.M
  54. Copyright © 2014 M/Gateway Developments Ltd QEWD Architecture Master Node.js Process Queue Queue processor/ dispatcher Unavailable / processing Node.js Worker Process Custom Worker Module Node.js Worker Process Custom Worker Module Redis/Cache/GT.M As soon as a worker is available again, a queued message can be passed to it Unavailable / processing Node.js Worker Process Custom Worker Module Node.js Worker Process Custom Worker Module Redis/Cache/GT.M Available Node.js Worker Process Custom Worker Module Redis/Cache/GT.M
  55. Copyright © 2014 M/Gateway Developments Ltd QEWD Architecture Master Node.js Process Queue Queue processor/ dispatcher Finished Node.js Worker Process Custom Worker Module Node.js Worker Process Custom Worker Module Node.js Worker Process Custom Worker Module Redis/Cache/GT.M A user's handler function signals completion using the function: finished(responseObject); This returns the response object to the master process
  56. Copyright © 2014 M/Gateway Developments Ltd QEWD Architecture Master Node.js Process Queue Queue processor/ dispatcher Finished Node.js Worker Process Custom Worker Module Node.js Worker Process Custom Worker Module Node.js Worker Process Custom Worker Module And the response is passed to Express which returns it to the client that sent the original request Redis/Cache/GT.M
  57. Copyright © 2014 M/Gateway Developments Ltd QEWD Architecture Master Node.js Process Queue Queue processor/ dispatcher Available Node.js Worker Process Custom Worker Module The finished() function also automatically returns the worker process back to the available pool So it can now handle the next queued request Redis/Cache/GT.M
  58. Copyright © 2014 M/Gateway Developments Ltd QEWD Architecture Master Node.js Process Queue Queue processor/ dispatcher Available Node.js Worker Process Custom Worker Module Worker processes, once started, are persistent No start-up / tear-down cost Workers will automatically close themselves down if they are inactive for more than a pre-set threshold time period Redis/Cache/GT.M
  59. Copyright © 2014 M/Gateway Developments Ltd QEWD Architecture Master Node.js Process Queue Queue processor/ dispatcher Available Node.js Worker Process Custom Worker Module Worker processes only handle a single request at a time Completely isolated run-time environment for handler functions No need for concerns about Node.js concurrency, so synchronous APIs can be used Redis/Cache/GT.M
  60. Copyright © 2014 M/Gateway Developments Ltd QEWD Architecture Master Node.js Process Queue Queue processor/ dispatcher Available Node.js Worker Process Custom Worker Module Redis/Cache/GT.M Long-running or CPU-intensive logic has no direct impact on other worker processes
  61. Copyright © 2014 M/Gateway Developments Ltd QEWD Architecture Master Node.js Process Queue Queue processor/ dispatcher Node.js concurrency is handled by the master process. 100% asynchronous logic The master process does almost nothing No CPU-intensive or long- running tasks, so very high-performance All the work happens in the isolated worker processes Multiple Concurrent Incoming requests
  62. Copyright © 2014 M/Gateway Developments Ltd Performance? • ewd-qoper8 module in isolation: – Handles the queue/master/worker architecture • Raspberry Pi 3 Model B – 4 core CPU – Readily-available commodity item – Low-cost • Easily-replicable benchmark
  63. Copyright © 2014 M/Gateway Developments Ltd Performance • ewd-qoper8 benchmark script provided: – How many workers? 3 – How many messages? 500,000 – Create a steady state of messages added to the queue as they're being processed: • Add a batch of 622 messages at a time • Wait 100ms between each batch – Messages are sent to workers which echo them straight back
  64. Copyright © 2014 M/Gateway Developments Ltd Performance • 5,800 messages/second sustained throughput • Limiting factor is master process hitting 100% CPU • Workers only 30% CPU, so plenty of capacity to do real work at this rate
  65. Copyright © 2014 M/Gateway Developments Ltd QEWD.js • For the Java, Python or .Net developer moving to Node.js, QEWD makes it a much simpler, much less weird and scary environment – Asynchronous code only needed when necessary for parallel execution: • eg accessing multiple remote web services • Quick and simple to install, configure and deploy
  66. Copyright © 2014 M/Gateway Developments Ltd Unique features of QEWD.js • Somewhat comparable to Apache Tomcat: – Application container • Can host multiple applications from one single instance of QEWD.js – Can be a mixture of websocket, HTTP and REST applications
  67. Copyright © 2014 M/Gateway Developments Ltd Unique features of QEWD.js • High-level abstraction of Redis/Cache/GT.M: – Flexible Document Database – Persistent JavaScript Objects • Built-in very high-performance Session database – Using the above databases
  68. Copyright © 2014 M/Gateway Developments Ltd QEWD.js • Front-to-back JSON • Browser communicates via JSON messages – Ajax or Web-Sockets • Even the integrated database is abstracted as persistent JSON – Cache, GT.M or Redis – Identical APIs, so apps can be effortlessly migrated between databases
  69. Copyright © 2014 M/Gateway Developments Ltd QEWD.js • Looks after all the critically important parts of a Web Application's back-end for you, eg: – Sessions – Database cacheing – Security • Using tried and tested solutions – Based on the previous-generation EWD technology
  70. Copyright © 2014 M/Gateway Developments Ltd QEWD.js • Resilient / Audit mode – When enabled, permanent record kept in the database of: • Queued requests • Processing status • Response(s) – If QEWD restarted, database is examined for queued, unprocessed requests • Automatically re-queued – Can specify retention period of database records
  71. Copyright © 2014 M/Gateway Developments Ltd QEWD.js • Supports all browser-side JavaScript frameworks • Some cool tooling and support available for React
  72. Copyright © 2014 M/Gateway Developments Ltd Ripple-OSI • Open Source project in UK & Ireland – Open Source stack for healthcare • Based on OpenEHR back-end systems – Browser-based, REST-ful UI • Orientated around the needs of clinicians • Initiative led by Dr Tony Shannon • Middle tier was originally Java-based – Now entirely replaced using QEWD • Choice of GT.M or Redis as embedded database – Used for cacheing OpenEHR data
  73. Copyright © 2014 M/Gateway Developments Ltd Federated Access to OpenEHR Browser QEWD GT.M or Redis ewd-qoper8 queue qewd-ripple Module Express OpenEHR Server AQL over HTTP(S) Worker PulseTile UI OpenEHR Server
  74. Copyright © 2014 M/Gateway Developments Ltd Few Moving Parts • Unlike EHMP, RippleOSI has almost no other moving parts apart from Node.js and QEWD.js • Simple installer script • Can even install and run on a Raspberry Pi!
  75. Copyright © 2014 M/Gateway Developments Ltd Ripple Foundation • http://ripple.foundation/
  76. Copyright © 2014 M/Gateway Developments Ltd
Advertisement