How to use Multithreading with "worker threads" in Node.js?

แชร์
ฝัง
  • เผยแพร่เมื่อ 23 ส.ค. 2024

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

  • @sohaibnoor6610
    @sohaibnoor6610 2 หลายเดือนก่อน +9

    I have 3.5 years of Experience as a Node js Developer. But I feel I knew nothing. Thank you so much man. This is mind blowing. Love every of it

  • @vitto_pincharrata
    @vitto_pincharrata ปีที่แล้ว +12

    An amazing oportunity to use os.cpus()!!
    + import os from "os";
    ...
    - const THREAD_COUNT = os.cpus().length;
    and reduce as well!
    - const total = thread_results.reduce((x, y) => x + y);
    Thanks for the video! :3

    • @TheLexa
      @TheLexa 2 หลายเดือนก่อน

      const { cpus } = require("os");
      const THREAD_COUNT = cpus().length;
      maybe better option

  • @guytonedhai
    @guytonedhai ปีที่แล้ว +9

    This is gold! I have been binge watching your channel. I wish I had discovered your channel much before.

  • @leeshey99
    @leeshey99 4 หลายเดือนก่อน +1

    The most intuitive video of multithreading nodejs out there

  • @Dev-Siri
    @Dev-Siri ปีที่แล้ว +3

    This is the best video I have found that teaches how to use multithreading in Node

  • @subhamshaw1726
    @subhamshaw1726 29 วันที่ผ่านมา

    Wow This is amazing. very well explained as well. I am gonna use it where ever possible. Thank you for making this video. loved it. 😍😍

  • @raviagheda5564
    @raviagheda5564 4 หลายเดือนก่อน

    Awesome content, Thanks for the creating video Multi Threading. Explanation was too smooth, and especially loved the optimization part.

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

    Google is playing a joke on me. I spent a month fiddling with the first topic, with the promise of all cycles. I just solved the problem and TH-cam immediately suggested your video...

  • @jefersoncosta2621
    @jefersoncosta2621 3 หลายเดือนก่อน

    This playlist is awesome! Thank you!

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

    Best video I have ever seen on node multithreading👍

  • @codingispower1816
    @codingispower1816 7 หลายเดือนก่อน +1

    Great video! Excited for this whole series!

  • @pabloroque8268
    @pabloroque8268 4 หลายเดือนก่อน

    Great content, i hope you to do more videos with advanced concepts and proyects with nodejs, best regards from Argentina

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

    Deym, I thought i'm on my limits for optimizing my api, Thank you so much for this

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

    An excellent tutorial on Multithreading in Node using Worker Threads. Thank you very much.
    {2024-01-12}, {2024-01-15}, {2024-01-19}

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

    Really nice guide! Thanks 🙌🏻

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

    Just finished watching and subscribed. thanks for this amazing tutorial! I actually learnt something really important for my job. So thanks again! and Please upload more tutorial like this. can we also have socket pool tutorials?

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

      Hey, happy to have you on board! And thanks for the feedback, I'll look into socket pools :)

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

    Super helpful. Thank you so much!

  • @Blossemcraft
    @Blossemcraft 6 หลายเดือนก่อน

    You solved a great problem. Thanks a lot

  • @rasulrukh2907
    @rasulrukh2907 2 หลายเดือนก่อน

    great job man!

  • @caotichcuc
    @caotichcuc 3 หลายเดือนก่อน

    God, it's super helpful. Please make more videos about APIs optimization and security for Nodejs, pleaseeeeeeeeeeeeeeeeeeeeeeeeeee

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

    Imagine combining multithreading with clustering? That would be a bomb 💣

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

    thank you for sharing this was so helpful

  • @muharief3885
    @muharief3885 ปีที่แล้ว +7

    great for single good server, bad for container. This way was just make container suffer bcose the resources already taken up by the io operation.

  • @s.mehradi
    @s.mehradi หลายเดือนก่อน

    Thanks a million! 😊

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

    Excellent explanation, thank you

  • @vicTor-ft8xv
    @vicTor-ft8xv 10 หลายเดือนก่อน +1

    Thank you brother

  • @Abdulkalam-he7wo
    @Abdulkalam-he7wo ปีที่แล้ว

    great work , explanation at its best..!

  • @parassharma7041
    @parassharma7041 3 หลายเดือนก่อน

    Great video

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

    Very well done!

  • @gregoax5474
    @gregoax5474 5 หลายเดือนก่อน

    great content!

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

    okay, understood, thanks for nice tip

  •  3 หลายเดือนก่อน

    0:19 - wait, I'm not ready yet!

  • @ahmeterdonmez9195
    @ahmeterdonmez9195 6 หลายเดือนก่อน +2

    hello brother. Can we use this to handle 2-3 different tasks at the same time? I subscribed your channel ;)

  • @Aleks-fp1kq
    @Aleks-fp1kq 5 หลายเดือนก่อน

    Hello. why do you need the number of cores? If I understand it correctly, the new loop is located in the same process but on a different thread.

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

    Question: What if I will be using "docker + kubernetes" to horizontally scale my node app and at the same time i have a file upload + file processing intensive task in my app should I still use "worker threads" to manage the intensive tasks or "docker+kubernetes" will solve the problem anyway ?
    Thank you for this amazing explanation !

  • @user-gy9sv7fp3i
    @user-gy9sv7fp3i 4 หลายเดือนก่อน

    Technically, node does not use threads for network operations. It uses different mechanism called async io. But the idea is similar - main thread is also not blocked.

  • @vaibhavdani4470
    @vaibhavdani4470 28 วันที่ผ่านมา

    Though, one thing I could not digest,
    20M loops by one thread Vs 4 threads counting 5 M loops simultaneously / parallelly :->
    1. addition of their count will be 5M + 5M + 5M + 5M = 20 M loops for sure as mathematically it is correct. But,
    As these 4 threads are working parallelly time taken by them is almost equal which is of counting only 5M loops.
    Diagrammatically,
    1 Thread ---------------- 20 M loops ---------------------> 12 Sec (let's say)
    1/4 Thread --------5M loops ------------> 3 Sec (let's say)
    2/4 Thread --------5M loops ------------> 3 Sec
    3/4 Thread --------5M loops ------------> 3 Sec
    4/4 Thread --------5M loops ------------> 3 Sec
    Here, we simply reduced our task to count form 20 M to 5 M, hence time taken describes to count 5M only not 20M.

    • @vaibhavdani4470
      @vaibhavdani4470 28 วันที่ผ่านมา

      Are they counting like
      1st Thread => 1M ---> 5M , 2nd Thread => 6M ---> 10 M , 3rd Thread => 11M ----> 15 M, 4th Thread => 16M ---> 20M .... ??? No , all threads are counting form 1 to 5M and parallelly hence time taken describes to count 5M only not 20M

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

    I have a question,
    If I cluster node js app on 8 cors and if I use thread on each computation in my code,
    So assumption is my same code is running on 8 clusters and all have threads in them mean to say the api I wrote in my single code is running on 8 different cors, does it improve performance and if it improve performance? Can you tell me pros and cons of it?

  • @alirezamosavi6185
    @alirezamosavi6185 11 หลายเดือนก่อน

    Great bro

  • @user-nw3rz4yp4u
    @user-nw3rz4yp4u 6 หลายเดือนก่อน +1

    I think while creating worker threads , new main thread is not created. Correct me if I am wrong.

  • @Black-xq9ey
    @Black-xq9ey 3 หลายเดือนก่อน

    Sir Which theme you are using? It's cool for the eyes.

  • @atmai1026
    @atmai1026 4 หลายเดือนก่อน

    I has a question that when we use Multithreading with "worker threads" or real applications of it. I have saw many Nodes courses and they just introduce about it, not talk about the real application

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

    thanks bro,

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

    Just learn my friends, and you will get where you want

  • @jsarabia_dev7371
    @jsarabia_dev7371 4 หลายเดือนก่อน

    If our app is inside of pods of kubernetes is not possible the Multithreading if has not have more 1 CPU for pod?

    • @SoftwareDeveloperDiaries
      @SoftwareDeveloperDiaries  4 หลายเดือนก่อน

      That's a great question! In a Kubernetes environment, each pod typically runs a single container, and the number of CPUs available to a pod depends on the resources allocated to it. If a pod has only one CPU allocated, then indeed, multithreading might not provide significant benefits since there's only one CPU core available for processing.

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

    awesome video
    but I have a question, can you elaborate on the difference between background jobs and worker threads. I have a situation in which I need to call db recursively then call third-party API which takes some time to respond then refactor data then filter specific data and again call third-party API and then bulk insert into db
    + I need to perform this action inside cron jobs which get triggered after every 3 hours
    so what should I need to use???? and I'm using ec2 which has only 1 core so I think I can't divide it if my solution is worker thread

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

      Thank you! :)
      Sounds like you need a queueing mechanism for such a bulky operation. Take a look at RabbitMQ or Apache Kafka and how you can integrate them with your existing API.

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

      @@SoftwareDeveloperDiaries okay 🤩 thanks again sir

  • @firoj.siddiki
    @firoj.siddiki 11 หลายเดือนก่อน

    subscribed.

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

    Thanks for this video! All Node haters - in your face! :D

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

    Hi , one question is like I have scenario in which have to do file operation many times we are using some third party library to club or combine small doc files to one big final pdf , but that has to be in particular order , there are 20 sections , each section is tree containing further sections and so on and so forth ,each section contains docx file that has to be merged, so can I divide workers to club 20/ thread-count and then finally club the result of all of them to final file, only thing is I would have to find thread count , programmatically
    do you agree? is there any other way you can recommend

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

      Maybe try worker threads? I have a video on it too. If that doesn't help, you can try asking ChatGPT, I'm pretty sure it knows everything :d

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

      @@SoftwareDeveloperDiaries thanks

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

    and what will happen when you run out of all your worker threads? in case of multiple requests to blocking endpoint by different clients

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

      The machine will have to launch a fair amount of worker threads and get noticeably slow, before it throws an error that it can't launch a new thread. But this is probably the sequence that you'd see.

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

      looks like a case that should be handled)

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

    Hi,
    I did not understand the idea of 7 threads , kindly correct me If this is wrong from what I know is UV_THREADPOOL_SIZE = 4 , by default and these are the only threads used by libuv for some of the operations! i.e except CPU intensive tasks

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

      Correct. When we refer to a "thread", we actually mean a "thread pool". With UV_THREADPOOL_SIZE you define the size of the pool, which by default consists of 4 "child threads". You can still increase the size of the pool up to 1024.

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

      @@SoftwareDeveloperDiaries is it practically possible to have 1024 threads ?

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

      my cloud setup is having 7 nodes each node has 2 vcpu , virtual CPU
      I am not sure how many threads each instance has , I printed cpus length using os module it printed 2 , so only 2 threads ? do you suggest any other way or to know about it

    • @vinaykumarkharwar177
      @vinaykumarkharwar177 11 หลายเดือนก่อน

      @@vaibhavm1986 It can't, the UV_THREADPOOL_SIZE can't be bigger than the number of total logical threads on your CPU. If you have an Octa-Core CPU each of them have 2 threads, then the logical threads at your disposal will be 8 * 2. It can't be more. To automate the code, you can use -
      const os = require("os")
      process.env.UV_THREADPOOL_SIZE = os.cpus().length
      Also, the threads used in the worker pool are not real threads. They are circumventive solution for multithreading. Possibly very close to the Co-routines of Kotlin language. Or GoRoutines from Go lang. In kotlin, you can have thousands of coroutines with multiple tasks.
      If you are looking for multithreading, you can use cluster module from node.js, which spawns many worker instances on each cpu thread, but yeah they are still memory intensive and mixing worker_thread with clustering is bad idea.

  • @chavdarajan7428
    @chavdarajan7428 4 หลายเดือนก่อน

    ACCHA KAM 👍 - HANCOCK

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

    Great video!
    but most likely you work in a company that uses containerized environment (unless you live under a rock) and if thats the case - dont bother, just put 1.25 cpu requests and add more replicas!
    its safer and much more scalable, also in containers environment you cant trust the core count since it works on CPU time

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

    What is the difference between workers and fork or child process

    • @jperusm
      @jperusm 5 หลายเดือนก่อน

      Processes take much more resources to spawn while threads are lighter and are handled by the same process

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

    Jesus loves you, man. Thanks for this incredible video.

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

    How download code example?

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

      Hey, there's none. I was hoping you could reproduce it by watching the video

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

      How hard is it to write that

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

    Hey Use cluster module instead of this much better approach

  • @Flonfl0n
    @Flonfl0n 4 หลายเดือนก่อน

    Great video, but you should have used a reduce to sum your results. I'm triggered haha

    • @SoftwareDeveloperDiaries
      @SoftwareDeveloperDiaries  4 หลายเดือนก่อน

      Check this out, it's better to use good old for loops when working with a lot of data :)
      leanylabs.com/blog/js-forEach-map-reduce-vs-for-for_of/

    • @Flonfl0n
      @Flonfl0n 4 หลายเดือนก่อน

      @SoftwareDeveloperDiaries I meant to sum the result of each thread.
      Total = thread_results[0] + thread_results[1]....

    • @SoftwareDeveloperDiaries
      @SoftwareDeveloperDiaries  4 หลายเดือนก่อน

      @@Flonfl0n oh i see ^^

    • @Flonfl0n
      @Flonfl0n 4 หลายเดือนก่อน

      @@SoftwareDeveloperDiaries hehehe