Alexandre Strzelewicz’s presentation on the Process Manager 2 (PM2) open source project gave listeners on day one a new tool to assist in the management and hosting of enterprise-level Node.JS applications. This tool enables an administrator to configure load balancing and resource optimization in production environments, something offered by very few existing products available today.
The project, which was first released in June 2013, has a substantial GitHub following and an extensive list of contributors. Alex, one of the primary contributors, explained how the framework can ensure concurrent use of all of the CPUs of the underlying machine hosting the Node.JS app. This dramatically increases the processing power offered by the hosting machine. He went on to explain that one limitation of running the machine in this “cluster mode” is that the web application must be stateless, a common restriction with the increasing adaptation of web app deployments to cloud-based environments.
Stateless server mode dictates that a developer cannot reliably store client related data within the server process for longer than the duration of a web request. The issue is that when multiple processors are available, the process which handles a request from a user the first time may not be the same process that handles subsequent requests from the same user. The typical solution to this restriction is to store user-related data within a persistence layer on the server i.e. a database. Each request to the server will be accompanied by a user identifier and this identifier will then be used to retrieve that user’s information from the persistence layer, per request.
This solution is a fundamental concept in the RESTful architecture style and also provides the added bonus of loosening the coupling between the client and server – a well known good practice in most coding scenarios. However, the new web sockets protocol, which developers often use to enable bidirectional communication between client and server, is a stateful protocol out-of-the-box. This can add another degree of complexity when working under a stateless server configuration. That being said, the solution presented for introducing statelessness to standard HTTP requests can also be used by web sockets when using a command/response pattern.
Alex went on to explain other advantages of running a Node.JS application on the PM2 framework. These include the ability to configure Watch & Restart mode, this instructs the server to restart whenever it notices a change to a file located within the application.