5

I have an website which is set up to use a MongoDB instance on compose.io. Everything is working ok, except for that my connection to compose.io and mongoDB takes about 1.2 seconds to connect. In my opinion this is quite slow, especially when several queries are fired on the database.

What would be viable tactics to increase the connection speed, or other tactics like caching to speed up the web app? Or would a setup with mongoDB installed on the server be a better solution?

I am using mongoclient on php to connect to mongoDB on compose.io. I use the following code to connect:

new MongoClient('mongodb://username:password@candidate.6.mongolayer.com:10554,candidate.5.mongolayer.com:10690/database?replicaSet=set-xxxxxxxx');
Patrick Steenks
  • 612
  • 1
  • 11
  • 24
  • 1
    It would seem that your biggest problem is that you are trying to "connect" every time you want to issue a query. Connecting to databases "is slow" as in inherrent process. That is why we "re-use" the database connection in our applications to avoid that overhead. Show some code so we can point out exactly what you are doing wrong if you don't understand the basic principle described here. – Blakes Seven Sep 12 '15 at 13:18
  • Yes, the database connection is re-used. However, the connection will timeout after a number of operations, or time. So when I want to reconnect to the database, again it takes 1.2 seconds. But basically what you suggest is to increase the connection time-out, so that the connection will be longer in the air and can more often be re-used? – Patrick Steenks Sep 12 '15 at 17:35
  • 1
    Well at least giving the connection string gives us some idea of the client you are using. As for "timeout", there is no such setting where the database connection "hangs up" after a period of time. Problems I see are that the base PHP driver no longer implements concepts such as connection pools and has limitted replicaset member discovery. You might look for adaptations on the base driver that support better things or at least implement some sort of "keepalive" check in your application. Beyond that, then it will be a provider issue you need to talk to them about. – Blakes Seven Sep 13 '15 at 02:47

0 Answers0