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

Building scalable breakout rooms with Node.js worker threads

Building scalable breakout rooms with Node.js worker threads

NodeJS worker threads have opened doors to many exciting implementations and functionalities on the server-side. Multiplayer networked games, and live engagement and collaboration, among others, make a really good candidate for CPU intensive applications which would greatly benefit from this multi-threaded-ness of NodeJS.

In this talk, we’ll understand everything about NodeJS worker threads and see a real implementation of this concept to simulate dedicated server instances to enable various groups of players to play private instances of a live quiz simultaneously.

Srushtika Neelakantam

February 25, 2021
Tweet

More Decks by Srushtika Neelakantam

Other Decks in Programming

Transcript

  1. BUILDING SCALABLE
    BREAKOUT ROOMS
    USING NODE.JS
    WORKER THREADS
    Srushtika Neelakantam
    Developer Relations Engineer at Ably Realtime

    View Slide

  2. HELLO!
    I’M SRUSHTIKA
    DevRel
    Engineer
    @Srushtika

    View Slide

  3. Let’s see
    breakout rooms
    in action
    TIME FOR A FUN QUIZ!
    3
    @Srushtika |
    Brighton Web Dev Meetup |
    https://quiz.ably.dev/

    View Slide

  4. BREAKOUT ROOMS
    FUNCTIONALITY
    1. How does realtime messaging
    work?
    2. How to make use of Node.js worker
    threads to efficiently simulate
    dedicated server instances for each
    breakout room?
    @Srushtika |
    Brighton Web Dev Meetup |

    View Slide

  5. HTTP WebSockets
    vs.
    @Srushtika |
    Brighton Web Dev Meetup |

    View Slide

  6. Publish/Subscribe
    messaging pattern
    Feature add-ons
    ● Presence
    ● Message History
    ● Integrations
    .
    .
    .
    Infrastructure add-ons
    ● Auto reconnection
    ● Scale
    ● Guaranteed delivery
    ● Message ordering
    .
    .
    .
    P.S. This is an old gif, Ably has moved on to new branding now.
    @Srushtika |
    Brighton Web Dev Meetup |

    View Slide

  7. BREAKOUT ROOMS
    FUNCTIONALITY
    1. How does realtime messaging
    work? ✅
    2. How to make use of Node.js worker
    threads to efficiently simulate
    dedicated server instances for each
    breakout room?
    @Srushtika |
    Brighton Web Dev Meetup |

    View Slide

  8. WORKER THREADS
    IN NODE.JS
    @Srushtika |
    Brighton Web Dev Meetup |

    View Slide

  9. HOW DOES
    NODE.JS WORK?
    @Srushtika |
    Brighton Web Dev Meetup |

    View Slide

  10. KEY ELEMENTS THAT MAKE NODE.JS WORK
    The chrome v8 engine
    converts JavaScript to
    machine readable
    code
    The libuv library
    provides and
    manages the event
    loop and thread pool
    @Srushtika |
    Brighton Web Dev Meetup |

    View Slide

  11. GREAT EXPLANATION OF EVENT LOOP BY PHILIP ROBERTS (JSConfEU on
    YouTube)
    @Srushtika |
    Brighton Web Dev Meetup |

    View Slide

  12. “Node.js is a CPU bottleneck due to
    its single-threaded nature. That’s
    one of its major drawbacks”
    @Srushtika |
    Brighton Web Dev Meetup |

    View Slide

  13. WORKER THREADS
    IN NODE.JS
    @Srushtika |
    Brighton Web Dev Meetup |

    View Slide

  14. NODE.JS WORKER THREADS ARCHITECTURE
    @Srushtika |
    Brighton Web Dev Meetup |
    Image source: The NodeSource Blog

    View Slide

  15. @Srushtika |
    Brighton Web Dev Meetup |

    View Slide

  16. Let’s see some real code
    https://github.com/ably-labs/realtime-quiz-framework
    @Srushtika |
    Brighton Web Dev Meetup |

    View Slide

  17. BREAKOUT ROOMS
    FUNCTIONALITY
    1. How does realtime messaging
    work? ✅
    2. How to make use of Node.js worker
    threads to efficiently simulate
    dedicated server instances for each
    breakout room? ✅
    @Srushtika |
    Brighton Web Dev Meetup |

    View Slide

  18. CAVEATS AND
    OTHER THINGS
    - Don’t use Worker threads for parallelizing
    I/O operations, the native Node
    architecture can do that more quickly
    - Creating worker threads is not cheap, so it’s
    better to create and use a thread pool
    instead
    - If there are async methods available, it’s
    better to use them, rather than off-loading
    to your own worker thread
    @Srushtika |
    Brighton Web Dev Meetup |

    View Slide

  19. Thankyou!
    @Srushtika |
    Brighton Web Dev Meetup |
    Further reading
    - NodeJS docs: https://nodejs.org/api/worker_threads.html
    - Demo source code: https://github.com/Srushtika/realtime-quiz-framework
    - “What the heck is an event loop anyway?”: https://youtu.be/8aGhZQkoFbQ
    - “Introduction to libuv: What's a Unicorn Velociraptor?”: https://youtu.be/_c51fcXRLGw
    - Realtime concepts: https://ably.com/topics

    View Slide