For a project I am using the new MongoDB driver for PHP with the library (see: https://docs.mongodb.com/php-library/master/), not the legacy one.
We are running PHP 7 as FPM.
I recently did some profiling with XDEBUG and realized that the first database query is significantly slower than the second one, for example
Session::validate -> Account::find 38ms
getProfile -> Account::find 2ms
Someone on github pointed me towards this site, so I can now answer my own question. To quote:
All versions of the driver since 1.2.0 persist the » libmongoc client object in the PHP worker process, which allows it to re-use database connections, authentication states, and topology information across multiple requests.
Versions of the PHP driver before 1.2.0 utilize PHP's Streams API for database connections, using an API within » libmongoc to designate custom handlers for socket communication; however, a new libmongoc client is created for each MongoDB\Driver\Manager. As a result, the driver persists individual database connections but not authentication state or topology information. This means that the driver needs to issue commands at the start of each request to authenticate and » discover the server topology.
Since the latest stable driver version as of writing this is 1.1.9, the authentication information are not persisted. However you can upgrade to 1.2.0 alpha releases, which can seriously speed up the process of connecting, as I discovered in some tests on my system. There are a few known bugs with the 1.2.0 alphas currently, so I will be sticking with the slower 1.1.9 for now.
This reponse will soon be outdated...