Last Updated: September 29, 2021
·
51.11K
· sdepablos

Improve Doctrine performance up to 100% on your Symfony2 project

Doctrine 2 has a full chapter devoted to caching but up until now we had never taken a look to it at ulabox.

The thing is that the chapter does not only speak about caching SQL results for some time - the result cache -, a type of cache we're currently not interested in, but caching metadata to improve Doctrine performance. The problem is that by default both caching the class metadata and executing the DQL transformation are done in each request, but they could be cached for the duration of your current deploy, so there's room for improvement.

The first cache you should modify is the query cache. The documentation is very clear about it:

It is highly recommended that in a production environment you cache the transformation of a DQL query to its SQL counterpart. It doesn’t make sense to do this parsing multiple times as it doesn’t change unless you alter the DQL query.

The second one is the metadata cache, that avoids having to parse class metadata coming from annotations, YAMLs or XML in each request.

The way to change these settings in Symfony 2 is simple. Just modify your app caching drivers configuration (here we're changing from the default value "array" to "apc", but you could also use "memcache", "memcached", "xcache" or "service"):

doctrine:
  orm:
    metadata_cache_driver: apc
    query_cache_driver: apc

The result? We've improved up to 100% the performance of our most DB-intensive pages.

Picture

3 Responses
Add your response

When did you perform this test? You should have some concept of date and time on your pages.

over 1 year ago ·

It's just a "Change report" of a deployment given by NewRelic. The requests per minute before and after the deployment used for the comparison are almost the same, so it's not an apples and oranges thing. In fact when you see daily and weekly aggregates you realise how much did we improve the performance with just a small change.

Talking about this issue with some friends who are also developing with SF2 they said to me that the setting is really important and it's one of the first ones they enable when developing a new site.

over 1 year ago ·

I unwittingly turned off all forms of doctrine cacheing on my Symfony2 application last weekend. After three days of dissatisfied users, frantic but fruitless optimisations, and banging of heads off desks, I stumbled across this article. I checked my config and reactivated these caches - really not expecting much. I was shocked at how dramatic and complete the improvement was - much more so than this article suggests. I went from a virtually unusable server consistently at loads upwards of 30, to one that flies with a steady load well below 1. An improvement of several 1000%. Magical. Thanks!

over 1 year ago ·