Speed Up Your Node App Using Worker Threads!

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
i recently learned about this really cool module that exists in node called worker threads and as soon as i learned about them i was like okay i have to make a video about this because it's really cool but before i actually show you what they are and how they work i think i should probably point out the problem that they're aiming to solve so here's the problem we're all well aware of the fact that node is single threaded and so what that actually means is at any given point in time our one thread our application could only handle one task at a time and in fact that's the reason why expensive things like input output so for example talking to a database or reading contents from a file those types of operations have to happen asynchronously and the reason why they have to happen asynchronously is because if our one precious thread that we have to do our work is busy doing a long running task all other requests that are kind of coming in are gonna have to wait until this one long running task is done so instead the way that we solve this problem is we take these expensive operations like reading contents from a file or talking to a database and we offload it to somebody else we use the event loop we basically in node uses c plus compiler but the point is we use somebody else to go do the expensive operation for us which effectively allows our own thread our own one set of hands to continue doing work while that is happening simultaneously but now this is all fine and dandy when you are in fact dealing with something like input output that can be done asynchronously but what happens when your situation is different what happens if you can't do your operation asynchronously so for example let's say you've already read all the data in from the database and what you're reading from the database is a really really big data set we're talking about maybe millions of records worth of data right now you're being tasked with doing some sort of data manipulation before you then go back and send it off to the client the trouble of course is at this point the asynchronous task is done the data has already been read in your callback is fired your promise is fired whatever you're using it's over now you're back on your main thread you're back in synchronous land so what's going up happening is if your one precious thread is busy doing all this data manipulation on this massive giant data set what's going to end up happening is you're going to take your otherwise really fast node application and you're going to slow it right down to a crawl now i actually have a small little example that i spun up to illustrate this problem in action so here you can see on screen you can see i've got a small little note application it's a node express application and i'm defining two endpoints so the first endpoint is very simple it's just going to go to the root url and then what it's going to do is going to take this variable counter it's going to increment it by one and then just send it back to the client the other endpoint slash heavy this is the one that i'm going to be using to simulate the really expensive operation so here's how this one works what we're doing is we're initializing a variable uh total and we're initializing its value to zero we then do a for loop and this for loop is going to go for a crazy amount of iterations i haven't actually counted what that number is but suffice it to say it's going to take a long time for this for loop to be done now at every iteration what's going to happen is we're going to increment the value of total by 1. finally finally whenever this for loop is done which as i said will take a minute we're then going to go ahead and send the total back to the client so now let's run this app and see the problem see the ramifications of having an expensive for loop just like this in our code all right so now you can see i've got one tab open to the root url and every single time i refresh this page i pretty much almost instantaneously get back a response it works exactly as you might expect now let's throw in the wrinkle so here now in the second tab you can see that i'm going to go ahead and make the request to slash heavy now this request is going to take a long time to come back with its response but that in itself is not the issue let me show you what the issue is going to be so i'm going to hit enter you can see that we're spinning we're waiting the response is not coming back right away but crucially when i go back to the first tab this request previously came back almost instantaneously now if i go ahead and refresh the browser this is now also going to hang and it's hanging because our one precious thread is busy doing all the work that this for loop requires so now i'm going to have to wait for the heavy request to conclude before i can actually get my response back for this particular request so effectively i've taken my endpoint that used to be blazingly fast and i've slowed it right down to a crawl and that's exactly what worker threads are aiming to solve and i'm going to show you how so the first thing i'm going to do to solve my problem is i'm going to go ahead and i'm going to import worker from worker threads now this work with res module is built into node you'll have access to it from node 12 and onwards no need to install anything it's like i said it's already built in right and so then we go down to the heavy endpoint and this is where the real changes start to take shape previously the for loop was defined directly inside of the actual endpoint now instead what i'm doing is i'm actually going to go ahead and use this worker construct that i just imported to go ahead and instantiate a new worker instance and when i go and construct a new worker what i need to do is i need to pass it the name of a file or the path to a file essentially i need to tell my worker when i'm going to spawn you and i'm going to create this worker what what instructions do i have for you what work am i going to be delegating to you and so those instructions are now going to live inside of the worker.js file so let's see what those instructions are so if we go to the worker file you can see that i'm now going to be importing this thing called parent port from the same work with res module but the most important thing that's going to happen in this file is the for loop that previously lived inside of my main thread directly in my endpoint it now lives in my worker thread which means that i'm now taking my for loop and i'm going to offload it to somebody else i'm going to take the really long running work and i'm going to tell my main thread don't worry about it you don't need to do this instead i'm going to have somebody else do this for you which in turn will allow your hands to be free to service other incoming requests and then finally the last thing that has to happen is once my worker is done doing the work i need to go ahead and tell the main thread that the work is done and i can use this by using the imported parent port object that you can see up here and it exposes a method called post message so using this post message method we can go ahead and send messages between the worker thread back up to the main thread and then i can pass along whatever data that i want and in this case i'm going to be passing it the actual value that total is going to equal to after the for loop is done so then if we go back into our server or into the endpoint rather you can now see that i'm going to use the work or instance that i've created and i'm going to use the on method so the on method will allow me to go ahead and listen up for events so the event that i'm going to be listening out for is going to be the message event so effectively when the worker actually calls the post message method this message event is going to get raised and then the callback to that event is going to actually receive the incoming data now this data variable here i could have called it whatever i wanted more accurately i probably should have called a total but the point is whatever i'm actually sending from the post message method is what i will then receive right over here and then finally i can then go ahead and say reza status 200 and then go ahead and send the data back so this is how we actually use the worker but now let's actually go ahead and see the difference that it actually makes hey sorry interrupt your video really sorry but salesman caima here okay just really quickly got to tell you about this really cool thing that i now offer i just want to let you know that you can now go down to the description box below and find a link to codingwithheim.dev all you got to do is head over to that link and you're going to find that you can now schedule a one-on-one mentoring session with me so what that means is when you head on over there choose a time that makes sense for you fill in the form tell me exactly how i can help you could include topics like react javascript mock interviews career advice and more and then you and i will be in a one-on-one meeting together and i'll be there to help you solve your problems so don't delay very minimal sessions available go down to that link and go ahead and book your first session back to the video okay so now we're back in the browser i'm going to go ahead and fire off the heavy request once again and as you can see it's once again taking a long time i'm not going to get the response right away but crucially when i go back to the first tab and now i go ahead and refresh this page i immediately get the response even though the heavy request is still working if you like this video and you want to watch more videos just like this one i've got an entire playlist somewhere right over here that you can click on with a bunch of node goodness so go ahead and click on that playlist and enjoy
Info
Channel: Coding With Chaim
Views: 21,737
Rating: undefined out of 5
Keywords: worker threads, node worker threads, speed up node app, event loop, nodejs worker threads
Id: Qg68f6Ir1SA
Channel Id: undefined
Length: 6min 33sec (393 seconds)
Published: Mon Nov 15 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.