1

I am using the following code to connect to my mongoDB from PHP. Here is my extension info from my localhost, and the same is on beta and prod.version stat

I am using the following code to connect to database.

class WebAPI {
    private $mongoClient;
    function __construct() {
        $mongoClient = new MongoDB\Client("mongodb://database.local:27017");
        if(!$mongoClient){
            //TODO: Inform status site about failure
            http_response_code(500);
            die();
        }
        $statsDB = $mongoClient->stats; //selects database stats
        $pageViewsCollection = $statsDB->pageviews;
        $result = $pageViewsCollection->insertOne(PageViewInsights::getInfo());
        var_dump($result->getInsertedId());
    }
}

In SQL databases, we use connection pooling. I suspect mongodb also has one, I am not sure. I googled it, but nothing handy comes up for this new mongodb 1.1.x version.

Is the a more better way to do this?

UPDATE

As said by Luiz Eduardo de Christo here, I am extending the question for the more relevant info.

In case connection pooling is not relevant, how to deal with huge volume of connections? How much RAM each connection will take, provided, I managed to use only one connection for every HTTP request and manage all transactions using that one connection, say I have a server with 8GB RAM, Hexacore Xeon E5 @ 3.1 GHz on CentOS or Ubuntu, running mongod alone without any other server like apache, how much maximum connections can I make? How many concurrent users my server is able to handle? Is there a way to optimise this on MongoDB 3.2?

PS: No information I am able to find about database optimisation with respect to server configuration.

Community
  • 1
  • 1
Sibidharan
  • 2,717
  • 2
  • 26
  • 54

1 Answers1

1

Your code looks fine. Regarding pooling:

According to the PHP manual:

This section is no longer relevant as of the 1.3.0 release of the driver and only serves as a historical information on how the pooling used to work. "The latest versions of the driver have no concept of pooling anymore and will maintain only one connection per process, for each connection type (ReplicaSet/standalone/mongos), for each credentials combination."

http://php.net/manual/pt_BR/mongo.connecting.pools.php

Best regards, Eduardo.

deChristo
  • 1,860
  • 2
  • 17
  • 29
  • So how can we manage huge connection volumes? Any info on that? Or is this method efficient one? – Sibidharan Nov 11 '16 at 05:12
  • Maybe in this case, shouldn't you need to worry more about your infrastructure then your code (since the connections are managed by PHP) ?. All I can think of is something like using Mongodb Replicas with clusters and shards. Please, take a look at:(https://docs.mongodb.com/manual/replication/) and http://stackoverflow.com/questions/17839962/persistent-connection-or-connection-pooling-in-php54-nginx-phpfpm-mongodb https://www.quora.com/How-should-I-set-up-mongodb-cluster-to-handle-20K+-simultaneous-connections – deChristo Nov 11 '16 at 09:59
  • In that case, fine. I am updating the question, for connection management and maximum connections one can make. Please provide more data on that regard if you can. – Sibidharan Nov 11 '16 at 11:10