Setup of Mongo ODM in PHP 7 – performance considered

In a previous topic I have described our journey in optimising a symfony app ( the-journey-of-a-symfony-api-from-150ms-to-20ms ). Some of you have asked about specific configs, so here we go:

Install mongo-odm and the adapter:

php composer.phar require alcaeus/mongo-php-adapter
php composer.phar require doctrine/mongodb-odm-bundle

Now let’s configure it:

doctrine_mongodb:
    connections:
        default:
            server: mongodb://%mongo_server%
            options:
                connect: true
                connectTimeoutMS: 300 # as stated in the original post: timeouts are very important
    default_database: %database_name%
    document_managers:
        default:
            # this is also important to reduce disk usage, you could also use other backends for caching this
            metadata_cache_driver: apc   
            retry_connect:              1
            retry_query:                1
            auto_mapping: true

To have the best control I recommend to allow slave readonly only from code. For this you can simply use:

$this->mongo->getManager()->getClassMetadata(MyDocumentType::class)->slaveOkay = true

Leave a Reply