lnug: having your node.js cake and eating it too

Download LNUG: Having Your Node.js Cake and Eating It Too

Post on 22-Jan-2018

515 views

Category:

Software

5 download

Embed Size (px)

TRANSCRIPT

  • Copyright 2017 M/Gateway Developments Ltd

    Having Your Node.js Cake and Eating It Too

    Rob TweedM/Gateway Developments Ltd

    Twitter: @rtweed

    http://qewdjs.com

    mailto:rtweed@mgateway.com

  • Copyright 2017 M/Gateway Developments Ltd

    A bit of background

    Royal Marsden Hospital: 1980s Touche Ross Management Consultants

    (now Deloitte): early 1990s NHS-wide Networking Project

    Independent consultant & software developer since 1993 Specialising in Web and associated

    technologies, particularly in healthcare

  • Copyright 2017 M/Gateway Developments Ltd

    A bit of background

  • Copyright 2017 M/Gateway Developments Ltd

    A bit of background

    Node.js: Since August 2010 (v0.2 had just come out!) Gave one of the talks at LNUG inaugural

    meeting

  • Copyright 2017 M/Gateway Developments Ltd

    And So, 6 Years Later

  • Copyright 2017 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!

  • Copyright 2017 M/Gateway Developments Ltd

    Node.js Architecture 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

  • Copyright 2017 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++)

  • Copyright 2017 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

  • Copyright 2017 M/Gateway Developments Ltd

    Asynchronous Syntax in Node.js

    Node.js is unique in that you have no option but to use Asynchronous logic

  • Copyright 2017 M/Gateway Developments Ltd

    Drawbacks of Node.js

    In every other language, zealots will encourage you to use it for everything!

    But even Node's greatest exponents will tell you not to use it for certain things

  • Copyright 2017 M/Gateway Developments Ltd

    Drawbacks of Node.js

    Don't use Node.js if you have: CPU-intensive logic Complex database manipulation

    Particularly relational database handling

  • Copyright 2017 M/Gateway Developments Ltd

    Drawbacks of Node.js

    Don't use Node.js if you have: CPU-intensive logic

    Tie up the CPU and everyone else using the process will be blocked

    Complex database manipulation Particularly relational database handling

  • Copyright 2017 M/Gateway Developments Ltd

    Drawbacks of Node.js

    Don't use Node.js if you have: CPU-intensive logic Complex database manipulation

    Particularly relational database handling Due to the limitations of asynchronous logic, eg you can't

    chain functions Limits creation of very high-level database

    abstractions

  • Copyright 2017 M/Gateway Developments Ltd

    Main criticism of newbies to Node.js

    Asynchronous logic

  • Copyright 2017 M/Gateway Developments Ltd

    Async is the New Sync?

    Node.js version 8 now supports Async/Await which greatly improves the syntax needed to handle asynchronous logic Avoids "callback hell" Very like synchronous logic

  • Copyright 2017 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

  • Copyright 2017 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

  • Copyright 2017 M/Gateway Developments Ltd

    The down-sides

    Heterogeneous environment Multiple skill-sets Multiple moving parts

    Node.jsRabbitMQ

    Rails

    Java

    Database

  • Copyright 2017 M/Gateway Developments Ltd

    What does Ryan Dahl Think?

  • Copyright 2017 M/Gateway Developments Ltd

    What does Ryan Dahl Think?

    "..within a single process we could handle many requests by being completely asynchronous. I believed strongly inthis idea at the time, but over the past couple of years, I think thats probably not the be-all and end-all idea for programming."

  • Copyright 2017 M/Gateway Developments Ltd

    What does Ryan Dahl Think?

    "..when I first started hearing about Go, which was around 2012, they had really easy to use abstractions, that make "blocking I/O", because its all in green threads at the interface between Go and the operating system.

    I think it is actually all non-blocking I/O."

  • Copyright 2017 M/Gateway Developments Ltd

    What does Ryan Dahl Think?

    "I think Node is not the best system to build a massive server web. I would definitely use Go for that.

    And honestly, thats basically the reason why I left Node. It was the realization that: oh, actually, this is not the bestserver side system ever."

  • Copyright 2017 M/Gateway Developments Ltd

    What do I think?

    I want to have my Node.js Cake and Eat it!

  • Copyright 2017 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

    And I want to be able to use Node.js for all situations

  • Copyright 2017 M/Gateway Developments Ltd

    The Problem is Concurrency

    All concurrent users in Node.js share the same process

  • Copyright 2017 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

  • Copyright 2017 M/Gateway Developments Ltd

    AWS Lambda

    "Function As A Service" AKA "Serverless"

  • Copyright 2017 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)

  • Copyright 2017 M/Gateway Developments Ltd

    AWS Lambda

    The first technology they supported was Node.js

  • Copyright 2017 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

  • Copyright 2017 M/Gateway Developments Ltd

    Lambda Node.js Examples

    AWS examples show use of asynchronous APIs

    Node.js users of Lambda use asynchronous APIs and Async/Await

  • Copyright 2017 M/Gateway Developments Ltd

    Lambda Node.js Examples

    AWS examples show use of asynchronous APIs

    Node.js users of Lambda use asynchronous APIs and Async/Await

    But unless your Lambda function really needs to be asynchronous, why use asynchronous logic?

  • Copyright 2017 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

  • Copyright 2017 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 because that's what you do, right?

  • Copyright 2017 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 because that's what you do, right? Partly because almost no synchronous APIs

    exist, particularly for: Database integration Web/REST service access

  • Copyright 2017 M/Gateway Developments Ltd

    Such APIs are possible

  • Copyright 2017 M/Gateway Developments Ltd

    Such APIs are possible

  • Copyright 2017 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?

  • Copyright 2017 M/Gateway Developments Ltd

  • Copyright 2017 M/Gateway Developments Ltd

    What Is QEWD?

    Essentially it's a multi-purpose Node.js-based run-time Platform

  • Copyright 2017 M/Gateway Developments Ltd