MongoDB Connection Lost
romanscott
PROOP

6 months ago

Hello Railway team,

We are running into an issue where our connection to our MongoDB server keeps getting terminated about every 2-3 weeks, which brings down our service in production. We have set up a static IP to always maintain the connection, but we are wondering why our connection keeps dying?

The only way to resolve this issue is by restarting the container. However, we don't want to have to restart the service each time it loses connection to MongoDB.

This is affecting our users in prod, can you please advise? Here is our code for connecting to MongoDB:

await mongoose.connect(process.env.MONGODB_URI, {

serverApi: {

version: "1",

strict: true,

deprecationErrors: true,

},

connectTimeoutMS: 10000,

socketTimeoutMS: 45000,

maxPoolSize: 50,

serverSelectionTimeoutMS: 5000,

heartbeatFrequencyMS: 2000,

ssl: true,

tlsAllowInvalidCertificates: true,

tlsAllowInvalidHostnames: true,

retryWrites: true,

retryReads: true,

directConnection: false,

family: 4,

});

Thank you!

Attachments

Solved

4 Replies

Thanks for reaching out Roman, and I appreciate the detailed context and code snippet.

What you’re describing sounds like a stale or dropped MongoDB connection that isn’t being gracefully handled by your app or by Mongoose’s internal connection pooling. This happens when:

  • The MongoDB connection is interrupted (e.g., network blip, TLS renegotiation, idle timeout on the DB side).

  • Mongoose doesn’t automatically reconnect or retry on certain classes of disconnections.

  • The app doesn’t listen for connection errors and recover gracefully.

I would first try to do the following:

  1. Add connection error listeners:

    Add listeners for 'disconnected', 'reconnectFailed', or general 'error' events on the Mongoose connection so your app can log or respond (e.g., attempt a reconnection).

(I generated this for you as an example on what it would look like)

mongoose.connection.on('error', (err) => {
  console.error('Mongo connection error:', err);
});

mongoose.connection.on('disconnected', () => {
  console.warn('MongoDB disconnected! Attempting to reconnect...');
  // Optional: Trigger reconnection logic or alerting here
});
  1. Enable Mongoose autoReconnect behavior:

    Although Mongoose does try to reconnect by default, some advanced options are now passed through to the underlying MongoDB Node driver. You may try:

useUnifiedTopology: true, // implicit in newer versions, but good to confirm
autoReconnect: true, // legacy, but worth verifying behavior if using older driver
  1. Avoid tlsAllowInvalidCertificates and tlsAllowInvalidHostnames in prod:

    These should only be used in development. Invalid certs/hostnames can sometimes cause renegotiation errors or get flagged by middleboxes or proxies.

Let me know if this helps.

Best,

Angelo


Status changed to Awaiting User Response Railway 6 months ago


romanscott
PROOP

6 months ago

Hi Angelo,

Our application is now crashing because it cannot establish a connection to MongoDB Atlas. We've never had this issue before. Please advise.


Status changed to Awaiting Railway Response Railway 6 months ago


romanscott
PROOP

6 months ago

Here is the screenshot

Attachments


jake
EMPLOYEE

6 months ago

Hey Roman. We cannot guarantee a singular, static connection for multiple weeks on end. The reason being for this is we need to cycle our proxy instances for security reasons

You'll need reconnection logic at the application level to get around this. Alternatively, moving whatever workload that's querying your database onto Railway into the private network will guarantee a connection indefinitely.

Happy to help migrate if you need!


Status changed to Awaiting User Response Railway 6 months ago


Railway
BOT

4 months ago

This thread has been marked as solved automatically due to a lack of recent activity. Please re-open this thread or create a new one if you require further assistance. Thank you!

Status changed to Solved Railway 4 months ago


Loading...