Different Patterns in Node.js
Learn about the difference between blocking and non-blocking code.
Node.js motivation
Before Node.js, there were significant issues with popular servers, which made it difficult to create scalable applications.
For example, the Apache HTTP Server had trouble handling a high volume of concurrent requests. It would create a separate thread for each network request, which led to tremendous resources consumed during increased website traffic periods. An increase in traffic beyond the server’s capacity would cause Apache to start dropping additional requests, leading to a poor customer experience.
The Apache HTTP Server also implemented blocking code, which meant a program had to wait for a current task to finish and release its resources before proceeding with the next task.
Node.js was created to solve these problems.
Blocking code
To understand why Node.js is an excellent choice for creating scalable network applications, we must first understand blocking and non-blocking code as they relate to Node.js’ I/O operations.
I/O stands for “input/output,” which is needed whenever a Node.js application is communicating with the server it’s running on (for example, reading some data from a file on the file system) or some other server (like querying the database or reading from the network).
Blocking methods execute synchronously. Suppose an application has the following code to read a file:
Get hands-on with 1200+ tech skills courses.