How create a few servers by using Node.js

How create a few servers by using Node.js

How create a few servers by using Node.js

In general, they say that a web server is a program that is responsible for organizing the response to user requests sent by their HTTP clients/agents. There are many of Web Server types

In current article we will talking about servers based on Node.js engine works under Unix-like OS only.


Creating a few servers using Node.js engine

Node.js cross-platform powerful runtime environment based on V8 engine and intended for executing server-site JavaScript (sometimes named ECMAScript). There are many articles about creating a basic HTTP server based on Node.js built-in module called HTTP. The HTTP module contains the function to create the server object (createServer()), bind it to a network address (listen()) and finally starts the HTTP server listening for connections. So, the HTTP Server object can listen now to ports on your computer and execute a special function named callback (requestListener()) each time a request to Server is made.

Some a simplest example is depicted below:

It can be run by command

Requesting by using curl command or any browser is showing the beforehand waited response:

Well, all is OK for now.

But what we should do if we need to have in run a few different servers in parallel? Does it possible to do by using only one Node.js engine?

Actually, turns out, it is possibly because Node.js allows create as many server objects as you want, by using practically even various Hypertext Transfer Protocols (HTTP, HTTPS, HTTPS with Client Authentication, etc.)

By the way, every server can have its own requestListener callback function that will processing corresponding requests and preparing responses.

Take also into consideration a several important aspects while creation of a few web servers by one Node.js engine:

advantages

  • Performance - NodeJS engine is supporting a super-fast real-time servers thanks to the single-threaded, event-driven architecture that efficiently processes a numbers of parallel requests.
  • Faster development - developers could create high-performance servers in a fraction of the time required by other languages and platforms.
  • Concurrent request handling - NodeJS server can handle thousands of concurrent connections with a very minimal overhead on a single process.
  • Easy Scalability - NodeJS servers can be scaled in vertical manner by using embedded Clustering Module.disadvantages

  • Performance - if each server is handled by a one NodeJS process, then apparently high load on one of them will affect others.
  • Logging - if several servers are handled by one NodeJS process you will need to separate them outputs somehow. Otherwise output will be chaotic.
  • Fault tolerance - if one NodeJS process handles several servers, then in case of a critical failure in one of them then all of them will crush simultaneously.

Clustering servers using Node.js

If your Web Server is starting to receive a huge amount of traffic it's much possibly may your underlying server couldn't be handle it. Typically, adding a few servers can solve this issue. But in order to share traffic to your all servers, a load balancing is required. Usually, a load balancer is sitting in front of Web Servers and routing (by using some policy) a client requests across all servers that minimizes time of requests answered and capacity utilization.

Node.js engine has a built-in module called Cluster Module to take the advantage of a multi-core processor. Using this module you can launch Node.js servers to each core of your system. Master process listening an outed available port to accept client requests and distribute across the worker-servers using round-robin routing.

Note that load balancing provides so named horizontal scalability by adding more server nodes or resources to handle increased traffic. Clustering module offers vertical scalability by adding more servers or resources onto one machine to handle increased demands. Since every server will work on separate processor core, you can have as much servers as many core has your processor.

Few servers creation simple example

The simple Node.js project that shows how to create a various servers can be found on GitHub.

Everyone can download and try it without any restriction.

Project creates 3 kind of servers simultaneously - HTTP, ordinary HTTPS and HTTPS with client authentication.

Project includes some Bash scripts as well which helps you to create self-signed certificates for HTTPS server and client authentication.

Downloaded project contains 2 main scrips:

  • server.js creates single script instance that builds 3 kind of servers in parallel.
  • cluster.js start a Node.js cluster of 2 (given in configuration config.json) server.js scripts instances.

The basic steps can be found in Project ReadMe file.

Evaluating the benchmark of server single instance vs clustering mode

Start one instance of server.js script by using the following command:

node --openssl-legacy-provider server.js

Note that NODE_OPTIONS environment variable --openssl-legacy-provider is needed only if you generated and use PFX certificate and Node.js version you use is above than 17.x.

To see the performance, we have used autocannon tools to benchmark test our server as shown below:

The above screenshot showed that the server can respond up to 103000 requests when running 500 concurrent connections during 10 seconds. The average requests/second is 10205.

Now starting the same server.js script under cluster mode with 2 worker instances.

node --openssl-legacy-provider cluster.js

The performance was evaluated in the same way, by using autocannon tools. So the result shown below:

The above screenshot showed that 2 servers under cluster can respond up to 178000 requests when running 500 concurrent connections during 10 seconds. The average requests/second is 17714.

Thus, using the cluster mode you can handle more requests, in our case in about 73.5% more.

Summary

Summarizing above mentioned, it can be say that without any doubts it is possible to creation a few servers on one Node.js engine.

This technique can be even used in production. For instance, I had some project where 3 servers on clustered Node.js were established:

  • HTTP server was used for quick checking the working state of engine (live or dead),
  • HTTPS server was public high-loaded server that services tones of users,
  • HTTPS with client authentication was used for administrative purpose (for servers remote controlling by ITOps and important statistical info getting).

But, of course, practically, this technique most commonly uses for short development that is very suitable while pilot projects, when require to check some idea or supposition.


要查看或添加评论,请登录

Simon Hunanyan的更多文章

社区洞察

其他会员也浏览了