Using Multiple Memcache Server With Doctrine In Symfony

You can use multiple memcache server for doctrine to provide high availability in your symfony application easy.

Then, let’s do it!

Define your memcache servers into your parameters.yml first.

# parameters.yml
parameters:
...
memcache.pool.data:
- { host: mc1.dc1.company.net, port: 11211 }
- { host: mc2.dc1.company.net, port: 11211 }

Then, define a service that uses Memcache class to register your memcache servers, and create another service with Doctrine Cache to register your new pool.

# services.yml
service:
cache.pool.memcache:
class: \Memcache
calls:
- [addServers, ['%memcache.pool.data%']]
cache.doctrine.pool.memcache:
class: Doctrine\Common\Cache\MemcachedCache
calls:
- [setMemcached, ['@cache.pool.memcache']]

Now we are ready to use our new cache pool with doctrine. Let’s define our pool as a result cache to doctrine.

# app/config/config_prod.yml
doctrine:
orm:
result_cache_driver:
type: service
id: cache.doctrine.pool.memcache

That’s it!

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.