$30 off During Our Annual Pro Sale. View Details »

Architecture of scalable and resilient NodeJS apps with GraphQL and event-driven serverless

Architecture of scalable and resilient NodeJS apps with GraphQL and event-driven serverless

Presented at JSFoo 2018, Bangalore.

Shahidh K Muhammed

October 26, 2018
Tweet

More Decks by Shahidh K Muhammed

Other Decks in Technology

Transcript

  1. shahidh_k
    Architecture of scalable and resilient
    NodeJS apps with GraphQL and
    event-driven serverless
    JSFoo 2018
    1

    View Slide

  2. shahidh_k 2
    I’m Shahidh

    View Slide

  3. shahidh_k
    The Problem
    3

    View Slide

  4. shahidh_k
    Traditional architecture
    App
    Database
    API
    Payment microservice Restaurant microservice Delivery microservice
    4
    1. Scalable?
    2. Resilient?

    View Slide

  5. shahidh_k
    Moving towards scale & resilience
    5

    View Slide

  6. shahidh_k
    Async architecture
    App
    Database
    API
    Event system
    Payment processing
    Restaurant approval
    Delivery assignment
    Async microservices
    6

    View Slide

  7. shahidh_k
    Serverless
    7

    View Slide

  8. shahidh_k
    Serverless
    $ cat index.js
    exports.function = (req, res) => {
    res.json({"message": "Hello World"});
    }
    8
    $ serverless functions deploy hello_world --trigger-http
    https://serverless.functions.net/hello_world
    $ curl https://serverless.functions.net/hello_world
    {"message": "Hello World"}

    View Slide

  9. shahidh_k
    Scalable!
    ● No OPS
    ● No VMs to manage
    ● Don’t need to worry about
    scaling
    ● Just write the code
    ● Deploy
    ● Scale for “free”
    Graphic Source
    9

    View Slide

  10. shahidh_k
    Resilient?
    ● Serverless functions are
    ephemeral
    ● No knowledge of state - async
    ● Trigger them on events
    ● Add retries to the mix
    ● We have resiliency
    10

    View Slide

  11. shahidh_k
    Example: Food ordering app
    Column | Value
    -------------------+---------------------------------------
    id | a705a43d-c55b-4a8a-ac16-80679bd02404
    created_at | 2018-09-23 13:24:32.159024+00
    user_id | eca786b6-73ae-44c7-b34f-1757af554464
    is_validated | false
    is_paid | false
    is_approved | false
    is_agent_assigned | false
    11

    View Slide

  12. shahidh_k
    Order state machine with serverless
    is_validated | false
    is_paid | false
    is_approved | false
    is_agent_assigned | false
    is_validated | true
    is_paid | false
    is_approved | false
    is_agent_assigned | false
    is_validated | true
    is_paid | true
    is_approved | false
    is_agent_assigned | false
    is_validated | true
    is_paid | true
    is_approved | true
    is_agent_assigned | false
    is_validated | true
    is_paid | true
    is_approved | true
    is_agent_assigned | true
    Checks
    payment
    New order
    Validate
    order
    Restaurant
    approval
    Agent
    assignment
    Completed
    12

    View Slide

  13. shahidh_k
    Architecture
    Recap
    ● Microservices became serverless functions
    ● Serverless functions are scalable by nature
    ● Database events trigger async serverless
    functions
    ● Resiliency by event source tool instead of in
    the API code
    ● Moved specific failure handling logic to
    generic failure handling
    13

    View Slide

  14. shahidh_k
    The cost
    14
    App
    Database
    API
    Event system
    Payment processing
    Restaurant approval
    Delivery assignment
    Generic Event System
    Event system
    Async Serverless Fns
    Payment processing
    Restaurant approval
    Delivery assignment

    View Slide

  15. shahidh_k
    High level overview
    Database
    Event system
    Payment
    processing
    Restaurant
    approval
    Delivery
    assignment
    Async serverless
    functions
    A
    Database
    API layer
    Payment
    microservice
    Restaurant
    microservice
    Delivery
    microservice
    Synchronous logic Asynchronous logic
    15

    View Slide

  16. shahidh_k
    However, there’s
    still one problem
    When stuff happens asynchronously,
    how do I get that info back to the app?
    16

    View Slide

  17. shahidh_k
    GraphQL
    17

    View Slide

  18. shahidh_k
    REST vs GraphQL
    GET
    PUT
    POST
    PATCH
    DELETE
    GET
    PUT
    POST
    PATCH
    DELETE
    GET
    PUT
    POST
    PATCH
    DELETE
    18

    View Slide

  19. shahidh_k
    Changes the way we think about UI + API
    ● UI First modelling
    ● Makes API usage more natural
    ● Maps UI components to GraphQL
    queries
    ● Faster iterations - no back and
    forth b/n frontend-backend
    19

    View Slide

  20. shahidh_k
    GraphQL Subscriptions
    Consume async info from the backend
    without any worries
    It’s as if REST had a WATCH verb Datastore
    App
    20

    View Slide

  21. shahidh_k
    Let’s say I want to build...
    21

    View Slide

  22. shahidh_k
    Without GraphQL
    ● Polling from the client
    ● Websockets - custom API
    ● Community fragmentation
    ● Various patterns in different
    frameworks
    ● More work for the developer
    22

    View Slide

  23. shahidh_k
    With GraphQL
    ● Standardised spec
    ● All GraphQL Clients are compatible with
    all GraphQL Servers
    ● Client implementations available for all
    major platforms and frameworks
    ● Subscriptions: access to async info
    ● Less work for the developer
    23

    View Slide

  24. shahidh_k
    GraphQL Subscriptions
    App Datastore Process
    payment
    Validate
    order
    Agent
    assignme
    nt
    Restauran
    t approval
    New Order
    Complete
    24

    View Slide

  25. shahidh_k
    However, the cost is...
    25
    App
    Database
    API
    Event system
    Payment processing
    Restaurant approval
    Delivery assignment
    Generic Event System
    Event system
    Async Serverless Fns
    Payment processing
    Restaurant approval
    Delivery assignment
    GraphQL Server Subscriptions

    View Slide

  26. shahidh_k
    Traditional
    vs
    GraphQL + Serverless
    Architecture
    26

    View Slide

  27. shahidh_k
    Traditional architecture
    App
    Database
    API layer
    Payment microservice Restaurant microservice Delivery microservice
    27

    View Slide

  28. shahidh_k
    Moving towards scale & resilience
    App
    Database
    API
    Event system
    Payment processing
    Restaurant approval
    Delivery assignment
    Async microservices
    28

    View Slide

  29. shahidh_k
    GraphQL & Serverless architecture
    29
    App
    Database
    Payment processing
    Restaurant approval
    Delivery assignment
    Mutation/Subscriptions Async serverless functions

    View Slide

  30. shahidh_k
    Demo Time!
    30

    View Slide

  31. shahidh_k
    Food ordering app
    31
    https://serverless-demo.hasura.app/

    View Slide

  32. shahidh_k
    Food ordering app - scalable
    32
    https://serverless-demo.hasura.app/analytics-app

    View Slide

  33. shahidh_k
    Food ordering app - resilient
    33
    https://serverless-demo.hasura.app/console
    https://github.com/shahidhk/hasura-serverless

    View Slide

  34. shahidh_k
    3factor.app
    Build backends for apps that allow for
    fast iteration, while being resilient and
    highly scalable
    1. Realtime GraphQL
    2. Event-driven
    3. Async serverless
    34

    View Slide

  35. shahidh_k 35
    Thanks
    Questions?

    View Slide