Concurrency vs Parallelism in Node.js

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everyone welcome back and in this video let's just discuss an interesting aspect of node.js runtime that is concurrency versus parallelism and it's more about javascript compared to node.js how javascript works as a concurrent language with the event loop and the asynchronous model but in general what exactly concurrency is what parallelism is in computer science and how this should affect you as a developer let's go if you're new here make sure you leave a like subscribe to the channel and hit the bell icon this is free of cost and helps the channel grow all right let's start at concurrency versus parallelism now what concurrency is let's understand it in a way that let's say you are trying to do two tasks right you are trying to clean up your room and you're also trying to cook food right so in this case what you will do is if i divide your time in terms of the availability let's say this black over here denotes the time you're spending in cleaning up your roux and this red over here denotes the time you're spending and cooking food so maybe it looks something like this where for the first 10 minutes you clean up the room then for the next five minutes you cook the food maybe stir a few things maybe add a few things and for the next seven minutes again you clean up the room then for the next four minutes you cook up the food for some more time and so on so what you as a person are doing here is that you're splitting your time into different tasks you're distributing it into different tasks but at the end of the day you're trying to do a bunch of things together right so this way of execution where it seems like you you might be doing stuff parallelly but at the end of the day you are doing it in a sequential manner but you just switch the context very rapidly that is known as concurrency parallelism on the other hand means that you and you alone are actually cleaning up the room and someone else or maybe you know you from another universe is actually cooking up food for you right so this is true parallelism where for a given time slice both of the tasks are happening right in this case you can see that for a given time slice you would never see both of the tasks happening so that means this process is concurrent and this process is parallel so let's understand now why do computers even bother doing this this concurrent execution why not just do it in a sequential manner and be done with it take 17 minutes to clean up the room first and then take nine minutes let's understand it in the following way let's say you are printing a document on a computer right so you have this computer over here and you have this printer which is printing some documents right let's say it is printing 10 documents over here let's say you issue a print command to this computer and this printer starts printing one document and then two document then three document would you really want your computer to just freeze for as long as these 10 documents are not printed and not respond to any cursor movement or any keystroke or anything obviously not right you would want to keep using your printer parallely to your computer right or you just want that computer hey just keep handling that printing part i don't really care but i also want my interactivity and my user stuff back on my system now this might be a little wrong and oversimplified example because printers technically won't really need the access to the cpu all the time because they also just like really copy their documents and stuff in their own internal memory but you get the idea that if this execution over here isn't concurrent in a way then this would actually be a disaster for your user experience that's why your computer needs to switch the context all the time and see what else needs my priority at the moment now discussing this in the node.js concept now node.js is not a language which executes stuff parallely right if you have a single core system you would always be able to run a single thread of node.js right that means that if even if you're trying to do any sort of processing or anything then node.js would actually run it in a sequential manner but node.js runs stuff concurrently thanks to its event loop now how do we know that it runs the request concurrently and how does event loop come into the picture well you see that when you perform anything which is i o based on network based asynchronous right when you use these async await stuff in node.js that is the point where event loop kicks in it basically you can think of it in a way how you would expect an operating system to implement some sort of you know loop which just keeps on checking what i have to do next what i have to do next is there anything more important similarly event loop is kind of that loop where node.js would periodically cycle through everything and anything which requires its attention and in this case it's actually a bunch of queues micro task queues and promises and everything which is stored in those queues right but the broader idea over here is that node.js let's say if you send 300 requests on this node.js server then it starts the first request like this like req1 and you keep on processing this request until and unless it finds some i o event or some network event for example you might be running an await call with fetch right so the moment that happens node.js says that hey not my problem operating system handle this fetch request and it's going to move on right it's going to move on in a way where it will start processing the other request it will start processing request number two now right meanwhile this os switch performs the network request at a lower level the network request and then gets back the result sometime later then at that time node.js let's say once that result is there with us now it will resume the request number one then maybe request number one is completed but request three is arrived so it will process request number three and so on right so you get the idea although node is single threaded over here there is no parallelism like node is not executing request one and two parallely but you see that it is basically the same concurrency model and the way node is able to achieve this concurrency is by offloading a few parts on the operating system like you know this point the place where node would actually offload the point to operating system is where it faces something like a network request or some sort of asynchronous action right or not even asynchronous in a way if you can just put it in a micro task queue or you know a queue which node.js maintains then we'll automatically move it to the next tick the next cycle now the question is can you run node.js parallelly well of course you can but for that you need to run different processes of node.js right so if you have a four core computer or core system you can run four node.js processors which will definitely be four times faster than your single node.js process and would be able to handle four times more request but they would all require four different cores to run in a parallel fashion and within those four course itself node would run concurrently on each of those cores right so so as to not like you know waste any time waiting for something which it has no control over it just moves on to the next thing so yeah that's pretty much it that was a little bit of introduction into what concurrency is what parallelism is and how it works in context of node.js hopefully this was a useful introduction to you if it was let me know in the comments below what do you think about node what do you think about concurrency and parallelism and if you learn anything new that is all for this one i'm gonna see you in the next video really soon if you're still watching this video make sure you comment down in the comment section i watched this video till the end also if you're not part of code dam's discord community you are missing out a lot on events which we organize on a weekly basis to code you already know the drill make sure you like the video subscribe to the channel if you haven't already and thank you so much for watching
Info
Channel: Mehul - Codedamn
Views: 40,171
Rating: undefined out of 5
Keywords: mehul mohan, codedamn, nodejs, concurrency, parallelism, react, reactjs, java, JavaScript, learn Java in 2022, learn JavaScript for beginners, python, css, html, Ruby on rails, tailwind, learn programming, coding, code, coders, developers life, developers world, coding for beginners, coding life, web development 2022, coding projects, 100daysofcode, 100daysofjs
Id: ENc_sLdUlFE
Channel Id: undefined
Length: 7min 46sec (466 seconds)
Published: Fri Feb 04 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.