Which cache backend makes sense for Doctrine ORM on a low-traffic PHP app: APCu or Redis?
0 reputation · 04 Oct 2022, 00:46 UTC
0 reputation · 04 Oct 2022, 00:46 UTC
I'm running a small, low-traffic PHP application on Doctrine ORM (2.x/3.x line) and want to cut database and CPU costs. I've already set associations to lazy fetch and pre-generated proxies with auto_generate_proxy_classes disabled in production, which removed most of the obvious overhead.
The remaining decision is the metadata and query cache backend. The documentation supports adapters such as APCu and Redis, but the trade-offs for a tiny workload aren't clear to me. APCu is per-process, so with several PHP-FPM workers each worker keeps its own cache, affecting hit rates and invalidation. Redis is shared and consistent across workers, but adds an extra service to deploy, secure, and monitor — which itself has a cost that may exceed what a low-traffic app saves.
I also want to avoid making things worse: I've read that lazy loading can produce N+1 query patterns if associations are touched inside loops, so I plan to verify query counts with the SQL logger in staging before and after any change.
For a workload with only a handful of requests per minute:
33275 reputation · 04 Oct 2022, 05:45 UTC
For a low‑traffic Doctrine ORM application (a handful of requests per minute) APCu is the appropriate cache backend; adding Redis rarely yields a measurable benefit and introduces operational overhead.
Because the metadata set is tiny and rarely changes, each worker’s APCu cache warms up after the first few requests and then serves nearly all subsequent metadata look‑ups from memory. The per‑worker duplication does not significantly lower the overall hit rate at low request volume, while Redis would add latency from network round‑trips and require extra monitoring, security patches, and memory management.
sudo apt-get install php-apcu or via PECL).apc.shm_size is large enough for Doctrine’s metadata (e.g., 64M).$config->setMetadataCacheImpl(new \Doctrine\Common\Cache\ApcuCache());
$config->setQueryCacheImpl(new \Doctrine\Common\Cache\ApcuCache());
apc_cache_info('user')['hit_rate']; a sustained rate above 90 % confirms the cache is effective.If you run many PHP‑FPM workers (e.g., >20) and notice low APCu hit rates or frequent cache misses after code changes, let me know the worker count; that could shift the recommendation toward a shared Redis cache.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.