Boosting NestJS Performance with PM2 Multiple Instances

Currently, I have a NestJS application that I typically deploy using PM2 in cluster mode with multiple instances running.

One thing to note is that NestJS utilizes the Singleton pattern for its services. This is crucial for some of my features, as it allows me to access service properties globally and maintain their values.

My concern is whether or not using PM2 cluster mode will cause any issues. Can NestJS share this Singleton pattern across instances?

Note: I am hesitant to introduce additional complexity to the project, such as installing Redis to manage global data storage.

Answer №1

When working in a clustered environment using PM2, each instance within the cluster operates as its own separate process, meaning they do not directly share memory. NestJS services act as singletons within a single process, making it challenging to share data between instances.

If you are depending on a singleton service within a NestJS instance and utilizing PM2 cluster mode, you may encounter issues as each instance functions independently, without changes reflecting across others.

To overcome this obstacle without introducing added complexity from external tools, you can consider implementing in-memory caching within your NestJS application. For instance, leveraging a caching library such as memory-cache or utilizing the built-in CacheModule from NestJS enables sharing of data between requests within the same instance.

Please let me know if this information is helpful, I can also provide some code examples for further clarification.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Load jQuery script after Ajax call has loaded the filter

Currently, I have multiple jQuery scripts running on one page. This specific page includes a product filter that operates using ajax. You can view the product filter at Below is one of the scripts I am utilizing: var $s = jQuery.noConflict(); $s('bo ...

Variables in Angular DI become undefined when a method is called through a passed function reference

Utilizing Angular, I have a class with several Dependency Injection (DI) variables. During normal execution of a method within the class, everything functions as expected and I can access the injected instances of the DI variables. Now, my goal is to crea ...

How Can I Build a Dynamic Field Form Builder in Angular 4?

While working with dynamic JSON data, I needed to create fields dynamically. For instance, if my JSON array contains 3 values, I would generate 3 input checkboxes dynamically as shown below: <ng-template ngFor let-numberOfRow [ngForOf]="numberOfRows"&g ...

The variable 'FC' has been defined, however it is not being utilized. This issue has been flagged by eslint's no-unused

After running eslint, I encountered a warning stating that X is defined but never used for every type imported from react or react-native. For example, this warning appeared for FC and ViewProps (refer to the image below). Below is my .eslintrc.js configu ...

What is the proper way to define props for a component to match those of an "img" element in TypeScript using Vue?

Is there a way to implement type inference for a component that passes all its props to an img element? I attempted the following methods: defineProps<ImgHTMLAttributes>() // and defineProps<HTMLImageElement>() However, both resulted in the e ...

Capturing user input in HTML and passing it to JavaScript

I have a good grasp on how to implement input in HTML to execute JavaScript functions that are defined in the same directory as the HTML file. For example: <input type="button" value="Submit" onclick="someFunc(500)" > When the button is clicked, th ...

Error: Fetch function does not exist

My goal is to periodically update my HTML page by fetching data from my Express API. Unfortunately, I'm facing challenges when it comes to integrating this functionality into Pug. Both fetch and XMLHttpRequest are resulting in the same error message: ...

Tips for capturing everything in NextJs with getStaticPaths

My current challenge involves utilizing getStaticProps and getStaticPaths to dynamically generate pages at build time. This is my first experience working with CatchAll routes, so I embarked on a search for solutions. Unfortunately, none of the results al ...

Create an HTML button on the homepage that directs users to the "about" page

I've been struggling to make a button in my Ionic app navigate to a different page upon clicking. Despite being new to Ionic, I've spent hours trying to solve this issue. Below is the HTML code in home.page.html: <ion-header> &l ...

Angular ng-bind is incorrectly displaying the value as 'object' instead of the intended value

I am working on an Angular app where I am retrieving data from a service in the following manner: angular.module('jsonService', ['ngResource']) .factory('MyService',function($http) { return { getItems: ...

Troubleshooting Variable Issues in PHP and JavaScript

On my PHP page, I have a while loop where I am retrieving the following... print $divLeft.strip_tags($row->twitterUser)."?size=normal\"/><br \/>".$row->twitterUser.$divRight."<a href='javascript:void(0);' id=&apos ...

Prisma allows for establishing one-to-many relationships with itself, enabling complex data connections

I am in the process of developing a simple app similar to Tinder using Prisma. In this app, users can swipe left or right to like or dislike other users. I want to be able to retrieve matches (users who also like me) and candidates (all users except myself ...

Error: Unable to locate package @babel/preset-vue version 7.1.0

I am currently working on a simple website using Ruby on Rails and Vue.js, but I am running into issues when trying to start the local server. After executing npm run dev in the terminal, I encountered 2 errors: This dependency was not found: * /Users/mu ...

Retrieving data from the array properties in a JSON object

I am facing a challenge with an array of elements, each element is quite complex as it contains nested arrays as properties. My goal is to extract specific attributes from these elements, but I have been struggling to achieve this using the forEach functio ...

Retrieve coordinates, specifically latitude and longitude, by utilizing AJAX and JSONP

Trying to obtain latitude and longitude using AJAX JSONP. Here is the code snippet: <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js"></script> <script> $(document).ready(function(){ var ...

Check to see if the cursor is positioned on the newly created raphael element without requiring a new mouse

After creating a new element using Raphael.js, I have noticed that hover events do not trigger on Chrome (42.0.2311.90) or Internet Explorer (11.0.8) when the element is under an idle cursor. However, Firefox (31.0, 32.0.2, and 37.0.2) works as intended. ...

The cube is visible, but the skybox is nowhere to be found

Having some trouble getting a skybox to appear along with a rotating cube in my project. The rotating cube is visible, but the skybox isn't showing up. Running the project on a local web server Tried copying and pasting from a tutorial that worked f ...

How can a JavaScript code be used to navigate to a different HTML file within the same directory that is stored within a folder?

Here is the sample HTML code: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewpor ...

When using firebase.firestore(), the data displayed is completely different and does not match the content of my actual database documents

I am trying to fetch data from firebase firestore using the following code: let db = firebase.firestore(); let locations = db.collection("location"); console.log("locations", locations); However, the logged data is coming back strange and not showing the ...

`'download as' feature using JavaScript and PHP`

I have implemented a JavaScript function on my website that, with the help of a PHP function, creates a file locally on the server in the same directory as the website files. The file name is only known to these JavaScript and PHP functions. Now, I am look ...