2

I am running a symfony application on 4 heroku dynos and I want to use apc_cache.

  • How do I make sure that the apc_cache is cleared on all dynos during deployment?

I found a trick online for clearing apc_cache during deployment that entails making a request to a (temporarily) publically available php file that clears the apc_cache. I do not understand how this would work reliably with multiple dynos, since any single request is always routed to only one of the dynos. Is this an issue at all, since my server monitoring tools indicate that the application is temporarily scaled down to one dyno during deployment, to be scaled back up when deployment is complete.

Maybe the following question should be a question on its own, but I am planning to migrate the application to Amazon EC2 in the near future and deploy automatically using Codeship, Docker and AWS Elastic Beanstalk (EB). Is there perhaps a more straightforward solution for the problem using Amazon EC2 instances in an autoscaling group?

  • 1
    In Elastic Beanstalk (amazon) you can write deployment yml files which can run bash scripts, modify files and and anything what you want. So after deploy you can restart fpm or just make a request to localhost to your hidden file which clears apc. – Karol Wojciechowski Nov 18 '15 at 12:24
  • i wonder if i could use a bundle like [this one](https://github.com/Smart-Core/AcceleratorCacheBundle) and be able to run a composer post-install-cmd that works on any platform with multiple instances. Would not know where to begin testing that, though. – Jesse van Muijden Nov 18 '15 at 12:41
  • As of PHP 5.5, PHP comes with OPcache built-in. For older versions, the most widely used byte code cache is probably APC – Wojtek Dmyszewicz Nov 18 '15 at 12:50
  • I found an opcache tweak for production: http://stackoverflow.com/questions/23382615/apc-apcu-opcache-performance-poor – Wojtek Dmyszewicz Nov 18 '15 at 13:27

1 Answers1

0

Caching the Autoloader with APC (Found in Symfony docs)

Symfony comes with a class - ApcClassLoader - that does exactly this. To use it, just adapt your front controller file. If you're using the Standard Distribution, this code should already be available as comments in this file:

// app.php
// ...

$loader = require_once __DIR__.'/../app/bootstrap.php.cache';

// Use APC for autoloading to improve performance
// Change 'sf2' by the prefix you want in order
// to prevent key conflict with another application
/*
$loader = new ApcClassLoader('sf2', $loader);
$loader->register(true);
*/

// ...

NOTE: When using the APC autoloader, if you add new classes, they will be found automatically and everything will work the same as before (i.e. no reason to "clear" the cache). However, if you change the location of a particular namespace or prefix, you'll need to flush your APC cache. Otherwise, the autoloader will still be looking at the old location for all classes inside that namespace.

You could use this bundle https://github.com/ornicar/ApcBundle or https://github.com/Smart-Core/AcceleratorCacheBundle and add the commands to your post-install-cmd

Clear all APC cache (opcode+user):

  $ php app/console apc:clear

or if you use the Capifony:

after "deploy", "symfony:clear_accelerator_cache"
after "deploy:rollback:cleanup", "symfony:clear_accelerator_cache"

Edited :

However, APC's object cache stores information on the local instance only, so once you have multiple dynos (instances), they won't share the cache.

For that, you could consider Memcached

Wojtek Dmyszewicz
  • 4,188
  • 5
  • 30
  • 42
  • the documentation of [https://github.com/Smart-Core/AcceleratorCacheBundle](https://github.com/Smart-Core/AcceleratorCacheBundle) states that I need to specify a hostname. I cannot use something like http://example.com there, because that will route the request to apc-*.php via the load balancer to only one of the dynos, right? – Jesse van Muijden Nov 18 '15 at 14:17