Node JS
Event Loop
Node.js single Threaded language. The problem come up with single threaded language is If we write and application with servers, request and responses are in a single or a sequential flow .Request will not come one after the other ,Request may be concurrent .We will get 1000 of request at the same moment or may be within few seconds. Event loop is a design that make our execution so fast. It allows Node.js to perform non-blocking I/O operation. Event loop is a semi-infinite loop, polling and blocking on the O/S until some in a set of file descriptors are ready
The arrow that goes to left and right is time. The time represent the time that main thread spend. At the beginning main thread get a request. First main thread read a file .
Main thread doesn’t wait for that read file to be completed but it can go to the next one where it http request .After that read file send to another second yellow color bar. In there it gives the file I/O command to event loop.
After that it understand its HTTP request. Again its I/O it hand over to internal thread pool to do this HTTP request. After that it when it comes to do the DB operation again it handling to internal thread pool to do this.
First green color area represent the time that main thread is waiting for something else to be executed. If it get another request In here it will get that request. In green color bars main thread is waiting .After some time the file operation is get completed and internal event loop fires and event.
Main thread cannot be interrupted .If its working on something it will continue to work on that one. Once he finish he look in to the event queue and see whether there are completed operations .
Advantages in Node.js
- Use cases is the best advantage in Nodejs
Use cases are use for building fast and scalable network application
Example: PayPal, NETFLIX, UBER , e-bay
2.Node.js best for an application with a I/O operation .
Example: may be an aggregation server where you collect data from different sources like databases files and put them all together in a format and response back to the client.
- API application
- Chat application
3.easy extensible.
4. easy to scale both horizontally and vertically
- Horizontal scaling is having a cloning running in different places.
- Vertical scaling means the capability of the server.
5.support for unit testing.
6.Ability to use single programming language from one end of the application to the other end