Why Node.js is Perfect for Building Fast Web Applications
What are We Gonna Study?
Hey there folks, hope you are doing great in your life and enjoying every bit of it!
Today we'll not discuss about any code or something like that but we'll discuss why even we should use NodeJS, basically the following topics:
What makes Node.js fast
Non-blocking I/O concept
Event-driven architecture
Single-threaded model explanation
Where Node.js performs best
Real-world companies using Node.js
Now this I would recommend you to become neutral for a little bit of time, and if you develop a biasness, then my work is fulfilled, if not then i tell in the comments why, you don't want to use it?
I am not the ambassader of NodeJS 😅
I am just making you think what actually is bad or good :)
What Makes NodeJS Fast?
The V8 engine's JIT Compilation makes it fast. (I am not going to tell what they are for that click here).
It is optimized for heavy I/O heavy operations and high concurrency, That's why it exceptionally fast for web servers.
It is single threaded, and capable of handling thousands of concurrent requests on a sinle main thread, so it is memory efficient too!
NodeJS uses LibUV library to delegate heavy system tasks (like hashing, reading, network, etc) to a thread pool that works in background.
Non-Blocking I/O Concept
This works exactly like a restaurant.
You give the order to the cashier.
Cashier takes your order, tells it to the kitchen and moves to the next order.
Then you wait until the kitchen makes your dish.
And finally, the kitchen finshes your dish and cashier servers you that.
Similarly, Here
NodeJS sends a request to the system (let's assume it's reading file).
It moves to the next task, while the request is being run in the background by the thread pool.
Whenever the request is fulfilled or rejected (whatever happened error or success), Node gets notified.
The main thread picks up that notification and and executes the callback associated with it.
You'll get a better explanation with proper diagrams flow -> Here
Event-Driven Architecture
It is a software design pattern.
Event-Driven Architecture (EDA) is a design system where the flow of program is determined by events.
This architecture have 3 major components:
Event Emitter (Producer): An object that fires an event that something has happened.
Event Listener (Subscriber): A mechanism that listens to a particular named event to occur.
Event Handler (Callback): The actual function which contains all the business logic to be executed after an event.
Key benefits:
Loose Coupling: Components communicate to each other through events, so they don't have to know each other.
High Scalability: By handling many concurrent events asynchronouly, one server can handle thousands of requests, without getting down.
Real-Time Responsiveness: Ideal for applications like chat platforms or live notifications, where instant response isn't just recommended but the bare minimum.
Single Threaded Model Explanation
You might think that "Ved, JavaScript is single threaded how does it manages so many requests and how is it memory efficient?"
See, Most of the other servers aren't single threaded, they create a new thread for every incoming request.
It is quite effective but not memory efficient, your server will ran out of memory in a 1000 concurrent request.
Meanwhile NodeJS uses a single main thread, so it obviously more memory efficient.
Now, you'll be asking then how will it manage so many requests, if it just have 1 thread.
Uhmm.... Did you forgot what i just told you in the I/O section, Read that once again and you'll understand ;)
Where NodeJS Performs Best & Real World Companies Using It
Real Time Applications: Instant response is required, NodeJS helps out of the box.
Data Streaming: NodeJS can send data in small chunks, and that is why companies like Netflix uses it.
APIs and Microservices: It is lightweight and its support for JSON makes it the natural choice for building RESTful APIs, and that is why companies like Uber and PayPal use it.
Internet of Things (IoT): They frequently send small data bursts that requries real time processing. NodeJS rocks again.
Single Page Applications (SPAs): It handles the high volume of asynchronous requests typical for modern SPAs built using React or Angular.
Famous Companies: Netflix, Twitter (X), LinkedIn, Paypal, Uber, Walmart, eBay, Groupon, NASA, Trello, Citibank, ChaiCode are some famous companies which use NodeJS.
Wrap Up!
That's it guys for today, Hope you enjoyed the blog because i enjoyed it too!
With this let's end this blog here, I'll catch you up in the next blog,
Until then keep coding and keep enjoying your life 💝

