Nodejs app disconnects from mongodb randomly

Viewed 37

I have a nodejs/ExpressJS/Mongodb/Mongoose app hosted on aws elasticbeanstalk. The problem is elasticbeanstalk health degrades randomly ( no specific times ), that happens because any request that requires database interaction results the following in logs:

*1360931 upstream timed out (110: Connection timed out) while reading response header from upstream

This happens no matter how much data I try to load. it happens with the least amount of data, this can last from a minute up to 20 minutes and it works again on its own, it is completely random. And I can force it to work immediately by restarting the environment ( I connect to mongodb using connection string on app startup ).

While other requests that don't require database interaction work 100%.

The thing is while database queries aren't working , I can connect to the same database from localhost and database requests work like a charm, they even work really fast.

What is even more strange is I have 4 other identical apps with the same setup, and This situation doesn't occur with any of them, only this app faces this problem !

What is the problem here ?

1 Answers

The above error usually means that your server closed the connection due to shorter timeout , but your application is not aware about it. You may need to check your connection string and modify the timeouts maybe decrease them , example connection string:

MONGO_URI=mongodb://user:password@127.0.0.1:27017/dbname?keepAlive=true&poolSize=30&autoReconnect=true&socketTimeoutMS=360000&connectTimeoutMS=360000
Related