Speed Up Your Node App Using Worker Threads!

แชร์
ฝัง
  • เผยแพร่เมื่อ 6 ก.ย. 2024
  • Get 1 on 1 mentorship with me! ▶▶▶calendly.com/c...
    In this video I demonstrate how to use worker treads to unblock and speed up your node applications

ความคิดเห็น • 65

  • @bricehemery1534
    @bricehemery1534 2 ปีที่แล้ว +21

    Can't believe how clean is this explanation. Thank you sir.

  • @code2287
    @code2287 2 ปีที่แล้ว +4

    I've always known I needed to know Workers but am always so overwhelmed with how complicated it seemed thanks for making me understand this better

  • @Miketar2424
    @Miketar2424 ปีที่แล้ว +4

    This was great. After spending some time going through various tutorials, I found some that had typos in the code, others where the demo was filled with very complex js, and yours was simple and to the point.

  • @Applemann97
    @Applemann97 ปีที่แล้ว +3

    Perfect example, perfect explanation & presentation. Thanks for this video!

  • @user-lt1jqrhcp7ext321
    @user-lt1jqrhcp7ext321 2 ปีที่แล้ว +3

    Chaim, thank you so much for the video. Your explanation is clear and awesome

  • @whatskookin6429
    @whatskookin6429 ปีที่แล้ว +1

    Someone at my job gave me this exact explanation, excellent content

  • @Dipenparmar12
    @Dipenparmar12 2 ปีที่แล้ว +1

    One of the most interesting and very beautifully explained topic.
    Thank you very much.

  • @sankeths3508
    @sankeths3508 ปีที่แล้ว +1

    can we define the pool size for this ?

  • @trucheitor
    @trucheitor ปีที่แล้ว

    Well thanks, i Will try this today. Just curious about what happens when you Open several Windows causing going out of threads in your system

  • @anmolsharma8949
    @anmolsharma8949 ปีที่แล้ว

    Amazing example. very sorted and easy. Thanks and love from India

  • @mosheritterman3472
    @mosheritterman3472 2 ปีที่แล้ว +4

    Thanks for another great video! I'm not a node developer but wouldn't you be able to accomplish this by wrapping the loop in a promise?

    • @CodingWithChaim
      @CodingWithChaim  2 ปีที่แล้ว +2

      good question. I think the answer is that a promise does not make a sync operation happen async, instead it gives you a way to deal with an already async operation. At the end of the day this is just a simple for loop and it will always happen synchronously

    • @litbro8180
      @litbro8180 2 ปีที่แล้ว +1

      @@CodingWithChaim you can promisify this for loop
      Edit: I recreated the same environment you used in this video and spent 5 hours playing with worker threads. My original comment is wrong. While/for loop is still blocking even if it’s wrapped in a promise unless there is a promise within its conditional or within its block.

  • @patrickc.2680
    @patrickc.2680 2 ปีที่แล้ว +2

    This is really good, seen it here first! Thank you man, keep up the great work.

  • @MrLinuxFreak
    @MrLinuxFreak 10 หลายเดือนก่อน

    סרטון מאוד מעניין ומעשיר ידע תודה לך חיים

  • @dmaniac4
    @dmaniac4 2 ปีที่แล้ว +2

    Wonderful explanation!!!! Thank You!

  • @ayami123
    @ayami123 2 ปีที่แล้ว +1

    Question, can I pass just a function inside the worker threads,
    or does it really need to be a file ?

  • @jeetchheda8916
    @jeetchheda8916 7 หลายเดือนก่อน

    Now, can you tell me the limitations of using this worker thread ? Like how many workers can we spawn ? does it depend on the CPU ? can it be used as promise.all() ?

  • @karlostj4683
    @karlostj4683 ปีที่แล้ว

    Are there any threading concerns (race conditions, deadlocking, etc.) that can arise with Workers? For instance, is the registering of the worker.on('message'...) code guaranteed to execute before the worker calls its postMessage()?

  • @thomasnguyen1150
    @thomasnguyen1150 ปีที่แล้ว

    thank you very helpful

  • @sainathkamble387
    @sainathkamble387 ปีที่แล้ว

    Hey , I wanted to run some queries on a worker thread and passed in sequelize instnace to the thread but it failed , is there any way how we can pass sequelize instance to the thread and execute the queries on the difrent thread itself on nodejs ,it would have been much easier if it was GO

  • @hsdev
    @hsdev ปีที่แล้ว

    Thanks Chaim. This was very helpful.

  • @CafeAsp
    @CafeAsp 9 หลายเดือนก่อน

    Awesome video

  • @djneils100
    @djneils100 ปีที่แล้ว

    great video - helped my solve a problem - thanks Chaim!

  • @smnomad9276
    @smnomad9276 2 ปีที่แล้ว +1

    Great video man thanks a lot 🙏🏻💯 I run some bots written in JS, would this make them faster?

    • @litbro8180
      @litbro8180 2 ปีที่แล้ว +1

      It wouldn’t make them faster. But it would allow them to run in parallel if each of them has their own thread

  • @VikashSingh-wy3gd
    @VikashSingh-wy3gd 2 ปีที่แล้ว

    Should we always use worker thread instead of fork method of child process?

  • @Kareszrk
    @Kareszrk ปีที่แล้ว

    Thank you very much! This was exacly what I was looking for. :)

  • @reassume4826
    @reassume4826 2 ปีที่แล้ว

    Straight to the point. Great.

  • @chiragkamatkamat
    @chiragkamatkamat 2 ปีที่แล้ว

    Simple and crisp, thanks

  • @user-zt1hx3ci2v
    @user-zt1hx3ci2v 2 ปีที่แล้ว

    Thank you very much! More videos on the node! :)

  • @soulninjadev
    @soulninjadev ปีที่แล้ว

    awesome video, well presented and explained

  • @royjosefsson5001
    @royjosefsson5001 ปีที่แล้ว

    Are worker threads able to do the same requests as the main thread?
    I'm aggregating 5.2M documents on Mongodb. It's no problem aggregating on the main thread but it's taking for ever on the worker thread..?

  • @frontend_ko
    @frontend_ko ปีที่แล้ว

    okay, understood

  • @samusaw
    @samusaw 2 ปีที่แล้ว

    brilliant, very good example!

  • @arvi8843
    @arvi8843 ปีที่แล้ว

    That's a great explanation! Thank you. 💯

  • @code_lab_9994
    @code_lab_9994 ปีที่แล้ว

    Hi chaim ! It was a nice explanation, but one thing i want to know about, if we make a worker thread then after it's process , will it not be an orphan task, so have we kill that child_process or not after completion?

    • @CodingWithChaim
      @CodingWithChaim  ปีที่แล้ว +1

      great question! I actually dont know the answer off hand. My assumption would be that the worker will get cleaned up after its done the work, but I would have to verify to know for sure

    • @code_lab_9994
      @code_lab_9994 ปีที่แล้ว

      @@CodingWithChaim Thanks for answer

  • @ShimonWosner
    @ShimonWosner 2 ปีที่แล้ว

    Thanks you, very clear explanation 👍

  • @mckiernantim
    @mckiernantim ปีที่แล้ว

    Question on cleanup - when working with a singular worker instance to we want to be instantiating a new worker for every call to that url? Is it a better practice to reuse them?

    • @CodingWithChaim
      @CodingWithChaim  ปีที่แล้ว

      I would say its better to create a new worker for each request because this way we can scale. If we only had one worker that we kept re using, we would run into the same bottleneck we do as with the main thread

    • @mckiernantim
      @mckiernantim ปีที่แล้ว

      @@CodingWithChaim can't believe you got back so quick! Last question - do we need to cleanup the old worker? This reminds me of RXJS observable patterns and I know that failure to cleanup observables can add up over time.
      Should we delete these workers after the message?

    • @CodingWithChaim
      @CodingWithChaim  ปีที่แล้ว +1

      thats a great question! My hunch is that after they get used they will be garbage collected, but I would say you should verify that because I dont know that for sure

    • @mckiernantim
      @mckiernantim ปีที่แล้ว

      @@CodingWithChaim launching my "covid-project" in two weeks with some decently heavy computational work. I had a thought like 'wait, does this block my event loop?' two days later I find your excellent , succinct solution. Thanks again for the help. I'll circle back if I find anything down the road.

    • @CodingWithChaim
      @CodingWithChaim  ปีที่แล้ว

      glad you found the video, hope it helps

  • @aldenasf
    @aldenasf ปีที่แล้ว

    is there a way for the worker to run local function instead of another script?

    • @CodingWithChaim
      @CodingWithChaim  ปีที่แล้ว

      nodejs.org/api/worker_threads.html#new-workerfilename-options seems like it has to be a file

  • @gisdeveloper
    @gisdeveloper 2 ปีที่แล้ว

    Thanks you very much!

  • @kaypakaipa8559
    @kaypakaipa8559 2 ปีที่แล้ว

    If im running the exact same app not locally but on GCP Cloud Run, would it still hang? I assumed each request spun up a new instance of your app thua nullifying any thread issues from the heavy endpoint.

    • @CodingWithChaim
      @CodingWithChaim  2 ปีที่แล้ว

      Each request will not spin up a new instance of the app. I’m pretty sure that would only happen if you’re using a severless approach

  • @lucamartinsm
    @lucamartinsm 2 ปีที่แล้ว

    Great content! Thank you!!! :D

  • @rameshramesh-bd2wu
    @rameshramesh-bd2wu 2 ปีที่แล้ว +1

    Watch at .75x speed

  • @PabloGnesutta
    @PabloGnesutta 2 ปีที่แล้ว +1

    While the explanation on how to use worker's is good,the example is really really bad.
    If this is an Endpoint that is exposed to the client, there will be potentially hundreds of thousands of requests to that endpoint, so the node application will spawn that numbers of new threads with it's corresponding node instances, event loops etc, which is a nightmare in terms of performance.
    So keep that in mind..use workers for heavy duty, yes, but not if it will be requested an inmensurable number of times

    • @trucheitor
      @trucheitor ปีที่แล้ว

      So, if You have to do some chained awaits that update a database. How do you handle correctly the endpoint for anlot of users? I thought about doing nothing and let AWS spawn more tasks copies behind a las balancer. AM i right?

  • @manoj-k
    @manoj-k 2 ปีที่แล้ว

    🔥🔥🔥

  • @raymondpanjaitan9578
    @raymondpanjaitan9578 ปีที่แล้ว

    gg sir

  • @itzik975
    @itzik975 ปีที่แล้ว

    wow a jewish youtuber, thats rare, as rare as seeing a rainbow.
    not trying to be mean or anything just abit surprised, thats all, im jewish too so the more the better.
    Hello bytheway Chaim, really great english too, without any hebrew accent as well.
    thanks for the video, i'v found about worker threads through google ofc, while searching for cool things about nodejs.
    didn't quite understand how to use them so i got here, very well explain and articulated. i'll save for future reference.

  • @ImTheDot
    @ImTheDot ปีที่แล้ว

    Andrew Tate now teaches programming

  • @akmaldiraa
    @akmaldiraa ปีที่แล้ว

    it's ilegal bruh :"

  • @Pareshbpatel
    @Pareshbpatel 7 หลายเดือนก่อน

    Node Worker Threads, beautifully explained. Thanks.
    {2024-01-22}

  • @ibadshaikh2215
    @ibadshaikh2215 2 ปีที่แล้ว

    Wonderful explanation!!!! Thank You!

  • @MrCompsci2787
    @MrCompsci2787 2 ปีที่แล้ว

    Beautiful explanation, thank you.