Search⌘ K
AI Features

Scaling with a Reverse Proxy

Explore how to scale Node.js applications by deploying multiple instances on different ports or machines and using a reverse proxy as a load balancer. Understand the benefits of reverse proxies over clustering, how to configure Nginx for load balancing, and how to supervise instances for high availability.

We'll cover the following...

The cluster module, although very convenient and simple to use, isn’t the only option we have to scale a Node.js web application. Traditional techniques are often preferred because they offer more control and power in highly-available production environments.

The alternative to using cluster is to start multiple standalone instances of the same application running on different ports or machines, and then use a reverse proxy (or gateway) to provide access to those instances, distributing the traffic across them. In this configuration, we don’t have a primary process distributing requests to a set of workers, but a set of distinct processes running on the same machine (using different ports) or scattered across different machines inside a network. To provide a single access point to our application, we can use a reverse proxy, a special device or service placed between the clients and the instances of our application, which takes any request and forwards it to a destination server, returning the result to the client as if it was itself the origin. In this scenario, the reverse proxy is also used as a load balancer, distributing the requests among the instances of the application.

Note: For a clear explanation of the differences between a reverse proxy and a forward proxy, we can refer to the Apache HTTP server documentation.

The illustration below shows a typical multi-process, multi-machine configuration with a reverse proxy acting as a load balancer on the front:

A typical multiprocess, multi-machine configuration with a reverse proxy acting as a load balancer
A typical multiprocess, multi-machine configuration with a reverse proxy acting as a load balancer

For a Node.js application, there are many reasons to choose this approach in place of the cluster module:

  • A reverse proxy can distribute the load across several machines, not just several processes.

  • The most popular reverse proxies on the market support sticky load balancing out of the box.

  • A reverse proxy can route a request to any available server, regardless of its programming language or platform.

  • We can choose more powerful load-balancing algorithms.

  • Many reverse proxies offer additional powerful features such as URL rewrites, caching, SSLSecure Sockets Layer termination point, security features (for example, denial-of-service protection), or even ...