Managing Multiple Threads In Node JS 🧵 | OPTIMIZING NODE JS

Поделиться
HTML-код
  • Опубликовано: 11 окт 2024
  • In this 6th video of my "Node JS Performance Optimizations" series, I show you how to handle managing multiple threads in Node JS. We achieve this by using a powerful module called Worker Pool. The video contains important info about what to consider when multithreading your Node application, as well as a live demo of how to implement thread-pool management.
    TITLE: MANAGING MULTIPLE THREADS IN NODE JS
    Node JS Performance Optimizations RUclips Playlist:
    • Managing The Event Loo...
    Subscribe to my Bleeding Code RUclips Channel here:
    / @bleedingcode
    Reference Links
    =============
    Bleeding Code - Node JS Performance Optimizations GitHub Repo
    github.com/ble...
    Worker Pool Node Module
    www.npmjs.com/...
    AutoCannon Benchmark Testing
    github.com/mco...
    Bleeding Code on Social Media
    ==========================
    bleedingcode.com
    / bleedcode
    / bleedingcode

Комментарии • 62

  • @BleedingCode
    @BleedingCode  3 года назад +3

    For those already running multithreading in their Node Apps, post a comment below letting us know what tools you use to achieve this 👍.

    • @MrTejomay
      @MrTejomay 3 года назад +1

      Hey, great content again. Are you on Linkedin?

    • @BleedingCode
      @BleedingCode  3 года назад

      @@MrTejomay Yup. John Vincent Jardin 😎

  • @Pareshbpatel
    @Pareshbpatel 9 месяцев назад +2

    An excellent introduction to managing multiple threads in Node JS using Worker Pool. Thanks.
    Subscribed!
    {2024-01-15}, {2024-01-23}

    • @BleedingCode
      @BleedingCode  7 месяцев назад

      Awesome. I'm glad you enjoyed it :)

  • @leonk7951
    @leonk7951 2 года назад +2

    i think its really powerful yet very easy to implement. thanks for your video. its really helpful

    • @BleedingCode
      @BleedingCode  2 года назад

      Hi Leon. I'm very glad it helped. Thanks for watching 🙏

  • @neljpj
    @neljpj 2 года назад +2

    I really enjoy your videos, I hope you’ll be making more!

    • @BleedingCode
      @BleedingCode  2 года назад

      Thanks Hannes. I appreciate it 👍

  • @arunkutz
    @arunkutz 3 года назад +2

    There were some CPU intensive Functions and i was looking into what could be done to speed it up in node.
    Thank you for the video... It was very helpful ... :)

    • @BleedingCode
      @BleedingCode  3 года назад +1

      You're very welcome Arun. If you have time, maybe check out the other videos in this series. Some nice gems in there 😎.

    • @arunkutz
      @arunkutz 3 года назад +1

      @@BleedingCode Yes .. Absolutely ... I have seen the playlist ... Will definitely go through it 😊😊😊😊

  • @TechPuzzle_Haven
    @TechPuzzle_Haven 3 года назад +2

    Excellent Videos. Thanks a lot. I hope to see more videos like this.

    • @BleedingCode
      @BleedingCode  3 года назад +1

      Thanks again Ashutosh. I see you're going through the playlist? 😎

    • @TechPuzzle_Haven
      @TechPuzzle_Haven 3 года назад

      Yes.. I have watched your all videos. Please make more videos on this topics.
      Thanks you

  • @MohamedWaheedAtef
    @MohamedWaheedAtef 3 года назад +3

    Great videos, I wish you the best of luck 😊, I really recommend you zoom in on the code areas and terminal areas in the video, zoom in and out when appropriate because the text is too small on smartphones

    • @BleedingCode
      @BleedingCode  3 года назад +1

      Hi Mohamed. Thank you so much for the feedback. Ok that's a good point. No one has mentioned that to date. I will make sure to keep that in mind and accommodate mobile devices for future content 👍

    • @MohamedWaheedAtef
      @MohamedWaheedAtef 3 года назад +1

      @@BleedingCode you are very welcome, best of luck 😊

  • @adigupta857
    @adigupta857 3 года назад +2

    Thanks for this amazing video ❤️❤️❤️, I was looking forward to this. I was trying to scale up my application by using worker threads, I made my own tool which use node native workers and manages them in parallel. But this implementation using workerpool was super cool , thanks 😊..... I want more insightful videos on important domains like this ❤️❤️

    • @BleedingCode
      @BleedingCode  3 года назад +2

      You are very welcome Adi. Sorry it took so long. Yes I will try my best to continue this series of videos...I do have a lot of related content I want to get going with as well 😎

    • @kaushikrishi01
      @kaushikrishi01 3 года назад +1

      Could you share the github repo of the implementation brother ?
      @Adi Gupta

  • @johnas3
    @johnas3 3 года назад +2

    Thanks for your video, you saved my day 🙏

  • @DAaaBbbIii
    @DAaaBbbIii 3 года назад +2

    John, you're back! ❤️

    • @BleedingCode
      @BleedingCode  3 года назад

      Hahaha yeah it only took like 6 months, but expect more frequent content 💪.

  • @ngneerin
    @ngneerin 2 года назад +2

    This is gold

  • @coderllama5302
    @coderllama5302 3 года назад +3

    Amazing!

  • @smnomad9276
    @smnomad9276 2 года назад +2

    Just discovered your channel and i love it 🔥 I have bots written in Javascript, can multithreading improve their speed?

    • @BleedingCode
      @BleedingCode  2 года назад

      Hi there. Thanks for the compliment :). Yes, multithreading can help in many ways...it's just important to understand that it helps with Userland code (i.e. your actual JS code), not things like TCP requests, I/O related tasks, etc.

  • @udemyaccount4082
    @udemyaccount4082 2 года назад +2

    Holy shit! Hey, I really like the way the controller.get() grabs the worker proxy from that context. It's like static, isn't it? Is Static context an idea in Node.js? Please let me know I am super curious and restless when I don't know these things. Thanks

    • @BleedingCode
      @BleedingCode  2 года назад +1

      Sorry for the late reply. I create and hold the controller in memory via a commonjs file. The controller.get() simply grabs hold of that in-memory object. But yes, I'm simulating a static object that's only initiated on load of the app 😎.

  • @himanshuuniyal2341
    @himanshuuniyal2341 2 года назад +3

    It is very helpful in increasing performance, just had a query how would you suggest creating test cases if we are using the proxy object worker functions, means at the time of running test cases init(responsible for loading workers in pool) will not be there and if we want to stub the object, it will say "trying to stub a property of null", as a proxy will be initializing at server run only.

    • @BleedingCode
      @BleedingCode  2 года назад

      Hi there. Good question, as the same issue occurs when trying to debug logic passed through to Worker Threads. My only suggestion when it comes to debugging or running Unit Tests, is to have a switch that runs the function bypassing the Worker Threads and running on a single thread.

  • @johnnyelcoste
    @johnnyelcoste 3 года назад +2

    excellent!

  • @pra9shinde
    @pra9shinde 3 года назад +3

    Bro create a udemy course for advance nodejs, include all this stuff
    You are amazing

    • @BleedingCode
      @BleedingCode  3 года назад +2

      Hi Pranav. Thanks so much for that compliment. I would love to do that in the near future, although right now I'm just trying to find the time to publish my next video 😎.

  • @radrianm5
    @radrianm5 2 года назад +2

    Why use this WorkerPool instead of just setting the UV_THREADPOOL_SIZE like in your previous video? Can we implement both?

    • @BleedingCode
      @BleedingCode  2 года назад +1

      Hi Rinaldi. The WorkerPool (aka Worker Threads) manages userland code (aka the physical JS logic that you write). The Libuv Threadpool manages OS-related tasks. Yes, you can absolutely implement both with no conflicts 👍

  • @rage128
    @rage128 2 года назад +2

    Thanks you..
    but i have some issue. İ downloaded your code exacly with .env file.
    minWorkers = 1 ... 8 there is no effect on autocannon avarage response count ?
    is there any other setting to enable pooling ?

    • @BleedingCode
      @BleedingCode  2 года назад

      Hi there. What is the specs of your machine?

  • @akshay__sood
    @akshay__sood 2 года назад +3

    in which phase of event loop does database queries get executed? I/O polling phase? also does these queries make use of libuv's thread pool?

    • @BleedingCode
      @BleedingCode  2 года назад +1

      Yes it's I/O and it gets offloaded to Libuv. It's important to understand that a db query is made up of a number of calls, some synchronous, some async.

    • @akshay__sood
      @akshay__sood 2 года назад +1

      @@BleedingCode in this chain of calls be it sync or async but these calls will get executed in libuv's thread pool right?

    • @BleedingCode
      @BleedingCode  2 года назад +1

      @@akshay__sood So, this is the thing. It's not grouped. Calls that are meant to run via libuv are transferred to libuv. If calls afterward are synchronous, they are run synchronously.

    • @akshay__sood
      @akshay__sood 2 года назад +1

      @@BleedingCode gotcha! Talk about it in a video with an example because database queries are primary part of backend engineering. You can talk about it when you revise your Node JS performance series as per your recent poll. Remember to mention my name!😁

  • @sudhir-pl6tz
    @sudhir-pl6tz 2 года назад +2

    Hi am confused between worker_threads and workerpools
    could you please explain?

    • @BleedingCode
      @BleedingCode  2 года назад +1

      Hi there. Sorry for the delayed response. Workerpools strategically use Worker threads behind the scenes to make life easier for devs. If you had to use worker threads manually, you'd have to spend a lot more time and know more about thread management.

  • @bules12
    @bules12 3 года назад +2

    I really loved your video, I'm a little new in Node and I would like to ask for help
    I'm trying to have more than 100 nodeJs programs running at the same time.
    To do that, I'm using clusters, making each worker deploying one express app server and calling it later, creating a lot of servers and calling them
    The question is:
    How could I use worker threads or Pools to do this?
    Cheers :)

    • @BleedingCode
      @BleedingCode  3 года назад

      Hi Bules12. Thank you very much for watching. So, maybe to help me understand better, what is the reasoning behind needing to spawn 100 node processes? How many physical CPUs does the VM or machine have?

    • @bules12
      @bules12 3 года назад

      @@BleedingCode thanks for your answer!
      I’m doing web scraping, so I need to do a lot of request to various products repeatedly
      I want to track a lot of products, that’s why I need 100+ node processes
      I’m using Ryzen 5 5600 (12 logical cores)

    • @BleedingCode
      @BleedingCode  3 года назад

      Hmmm, so if you're looking for concurrency, you might not have to spawn so many processes. It's actually costing you a lot of unnecessary CPU processing and memory usage. Worker Pool is definitely going to help you free up resources by using Worker Threads. I would recommend taking a look at my other videos in my "NodeJS Performance Optimization" playlist, especially the videos titled: "Managing the Event Loop Phases" and "Determining your application baseline performance". The key is to keep the primary thread free to execute incoming requests. If you can achieve this and you have the processing power on the VM, you could run many thousands of requests per second with 1 Node Application.

    • @bules12
      @bules12 3 года назад +1

      @@BleedingCode that’s truly a dream, I have a lot to study (From NodeJs)
      If I manage to do it using pools and worker threads successfully I’ll tell you
      Thanks ❤️

  • @soniablanche5672
    @soniablanche5672 Год назад +2

    It's funny because Nodejs's own crypto library runs on a different thread when it's hashing or encrypting something

  • @vaibhavm1986
    @vaibhavm1986 Год назад +1

    Hi
    I am making 5 API calls in and i can see 5 console log at same time how come is possible with 4 threads

    • @BleedingCode
      @BleedingCode  Год назад

      Hi there. Can you elaborate the issue? If you're making these calls on your local machine, the speed would be near instant, making it feel like all 5 logs occurred at the same time.

  • @grijjly6091
    @grijjly6091 Год назад +1

    how can you dare use workerpool package ? , it is fully overhead when functions and parameters sending worker via stringified ? just create your own worker init functions there, just send parameters and wait for message, this video series about optimization, this is disgusting

    • @BleedingCode
      @BleedingCode  7 месяцев назад

      Thanks for your honest feedback.

  • @MrTejomay
    @MrTejomay 3 года назад +3

    Enjoyed it. Are you on Linkedin @bleeding Code?

    • @BleedingCode
      @BleedingCode  3 года назад

      Thank you Tejomay. Yes I am - John Vincent Jardin 👍