Resolve Session Persistence Issue in Node.js App with Redis Implementation for PM2 Cluster Mode

Job ID: 37605780

Budget: $30 – $250 USD

I am looking for a skilled Node.js developer to resolve a session persistence issue in a Node.js app with Redis implementation for PM2 cluster mode. The current issue is that sessions are not being maintained between server restarts and not being shared across multiple instances in PM2 cluster mode. Additionally, sessions are not being stored in Redis properly.

Requirements:
- Strong experience with Node.js and Redis implementation
- Familiarity with PM2 cluster mode
- Expertise in troubleshooting and resolving session persistence issues

The ideal candidate should have experience in resolving similar session persistence issues and be able to provide potential solutions. The project timeframe is flexible as long as the issue is resolved accurately.

The logs indicate a few key things:
* The user/signin endpoint is called, and the sign-in process completes successfully. The user is authenticated, an access token is received and updated in the database, and the session is established as the log shows "User session established".
* However, immediately following the successful sign-in, when the /payments/approve endpoint is called, it logs "Authorization failed: No user in session". This points to a failure in persisting the user's session between the sign-in and payment approval requests.
Since the application is run using PM2 in cluster mode, as evident by the logs that show multiple instances starting up (App [demoapp-backend:X] online), it's likely that each instance of my application runs in a separate process. In a multi-process environment like this, you cannot rely on the default in-memory session store because each process has its own memory, and sessions are not shared across them.
This means that even if the user signs in on one process and has their session established, subsequent requests could be routed to a different process where their session does not exist, resulting in the "No user in session" error.
To fix this issue, you need to implement a session store that is shared across all processes. Redis is a common choice for this purpose as illustrated in previous responses. Here is what you need to do:
* Set up a Redis instance that your Node.js application can access.
* Install connect-redis and redis packages in your Node.js application.
* Configure express-session to use the Redis store.
Related categories: Node.js Express JS Redis MongoDB Docker Compose