Why is nodejs easy to expand

Tram Ho

Clusters and Child Processes

Scaling Node.js Applications

Although single-threaded and non-block is pretty good. But a process in a CPU will not be enough to handle the growing workload of an application. No matter how powerful your server can be, what single-threaded support can be limited. The fact that a node runs in single-threaded does not mean that we cannot take advantage of many processes and of course, so do many servers. Using multiple processes is the only way to extend a Nodejs application. Nodejs is designed to build distributed applications with multiple nodes. This is why it is named Nodejs.

Scalability is brought into the platform. Workloads are the most common reason we expand our applications, but that’s not the only reason. We also expanded our applications to increase usability and failure tolerance. Before we talk about scaling a node, there are three things we can do to extend an application:

  1. The easiest way to extend a large application is to clone it multiple times and let each mirrored instance handle part of the workload. This does not take a long time to develop and it is highly effective.
  2. In addition, we can extend an application by separating it based on functions and services. This means that there are many different applications with different codes and sometimes even a separate database. This strategy is often associated with the term microservice, in which the microphone indicates that the services should be as small as possible, but in reality, the scale of the service is not important, but the implementation. Loose connection and high cohesion between the services. Implementing this strategy is often not easy and can lead to unexpected long-term problems, but when done correctly, the advantages are huge.
  3. The third expansion strategy is to divide the application into cases where each case is only responsible for a portion of the application’s data. Data partitioning requires a lookup step before each operation to determine which case of the application to use. For example, we may want to partition users based on their country or language. We need to look up that information first.

Successfully extending a large application will eventually implement all three strategies and nodes. js makes it easy to do so. Next, we will talk about the built-in tools available in Nodejs to implement the clone strategy.

Child Processes Events and Standard IO

We can easily generate a child process by using child_ process modules and the child process can easily communicate with each other by a messaging system. The child_ process module allows us to access Operating System functions by running any system command within a child process, controlling input stream and listening to its outout stream. We can control the arguments passed to the command and we can do whatever we want with its output. For example, we can convert the input of one command to another, because all the inputs and outputs of these commands can be displayed to us using streams.

There are four different ways we can use to create a child process in Node: Spawn, fork, exec, and execFile. Each function will have a difference that different uses:

execFile

Execute a function on a new process. Input is the name or path to a file, which can be passed as an argument and a callback. Output is when done, the callback will be called with data as buffer or an error.

spawn

Execute a function on a new process. Input is a command, can be passed arguments. Output is 1 stream I / O,

exec

Execute a function on a new process. Input is a command, can be passed arguments and callbacks. Output is when done, the callback will be called with data as buffer or an error.

fork

The child_ process.fork () method is a special case of child_ process.spawn () that is specially used to generate new Nodejs processes. Like child_ process.spawn (), a ChildProcess object is returned. The returned ChildProcess will have an integrated communication channel that allows messages to be passed between the parent process and the child process.

Refer

https://dzone.com/articles/understanding-execfile-spawn-exec-and-fork-in-node

Share the news now

Source : Viblo