A single threaded language means it can do only one thing at a time. Then why we use a single threaded language like NodeJS to create high traffic web applications with lots of concurrent connection.
Doesn’t it look counterintuitive?
Don’t you think a multi-thread language like Java and C++ will be more appropriate for this use case?
We can analyze this scenario with the help of a chat application server. The chat application should be designed to handle multiple concurrent connections, but complex calculation requirements in these kinds of applications are very less.
Let's analyze a multi-threaded language for this use case.
Multi-threaded language can use multiple threads to handle concurrent connections.
Each thread can handle a single connection, which allows for parallel processing of multiple requests. This can lead to a better performance on multi-core processors. This looks promising. Wait… let's analyze some practical disadvantages of this approach.
Complex code
The code required to develop this chat server will be more complex and need synchronization mechanisms to handle shared resources.
Difficult to debug
Code developed will be difficult to debug, as there are more moving parts and high potential for race conditions and other concurrency-related bugs.
High memory usage
The application will use more memory, due to the necessity to manage multiple threads.
Poor performance
Even if there are a greater number of threads the performance will be low compared to single threaded language, since they need to spend time and resources managing multiple threads.
Multiple threads justify its usage only if we want to maximize the computational efficiency, not the I/O efficiency.
The cost of thread creation simply overrides the benefits of multiple threads in a high traffic system.
A multi-threaded model is more suitable for high computational requirement applications that require parallel processing and need to take full advantage of multi-core processors.
Hope by now you may understand multithreading is not the holy grail in all scenarios.
Node.js is single threaded for a few reasons:
Node.js is single-threaded because it uses an event-driven, non-blocking I/O model, which allows it to handle multiple connections simultaneously.
This event driven approach uses an event loop and queue to efficiently manage concurrent connections with singe thread and minimal amount of memory overhead.
This makes NodeJS shine in real-time, data-intensive applications such as chat and gaming servers.
Also developing is much easier, since we don’t need to worry about managing multiple threads. It is also easy to horizontally scale a NodeJS application by running multiple instances in different server.
How Google Search Works Google Search is used to find information on the web, including websites, images, videos, and news. Google Search works by crawling the web. Googlebot, Google’s web crawler, visits websites and collects information about them. This information includes the website’s title, content, and links to other websites. Google then stores this information in its index.
Manipulate Types in TypeScript TypeScript is a really great language, it has a very powerful type system. We can combine various type operators to form complex operations and values in a maintainable way.