Designing a Per‑Entity Second‑Level Cache in Doctrine ORM
A guide to enabling Doctrine ORM second‑level cache per entity, covering design, trust boundaries, checks, and failure modes.
24 Oct 2026, 06:47 UTC

Problem and Takeaway
When an application repeatedly reads the same rows (e.g., user profiles) the database becomes the bottleneck. A per‑entity second‑level cache can serve those reads directly from memory, reducing latency and load.
Requirements
- Persistent, low‑latency read access for frequently queried entities.
- Cache must sit between the DB and the application.
- Cache should be enabled only on data that is immutable or read‑only after creation.
Smallest Suitable Design
Enable the second‑level cache on a per‑entity basis using Doctrine\ORM\Mapping\ClassMetadata and a compatible cache provider such as APCu.
- Ensure Doctrine version is 3.x (2.x lacks full second‑level cache support).
- Configure a shared cache store (e.g., APCu) that all PHP workers can access.
- Mark the target entity class with @Cache or set the metadata flag programmatically.
- Leave write operations unchanged; Doctrine will bypass the cache automatically for modifications.
Trust Boundary
The cache is only safe for read‑only or immutable data. Writes must either bypass the cache or trigger explicit invalidation. If the data changes without invalidation, stale reads can appear.
Operational Checks
- Run
composer show doctrine/orm-bundleand verify the version is 3.x. - Inspect the generated ClassMetadata: the property
secondLevelCacheEnabledshould be true for the entity. - Monitor the cache provider’s hit ratio; a low ratio indicates missed reads or mis‑configuration.
Failure Modes
- Cache stampede: many concurrent reads miss the cache simultaneously, causing a temporary DB surge.
- Stale data: missed invalidation events leave old values in the cache, leading to inconsistent application state or security leaks.
- Data divergence in clustered environments when a non‑shared cache store is used.
Practical Example
#[ORM\Entity]
class User
{
// fields, getters, setters …
public function __construct()
{
// enable second‑level cache for this entity
$metadata = $this->getEntityManager()->getMetadataFactory()->getClassMetadata(self::class);
$metadata->setCacheEnabled(true);
}
}
Or using annotations:
#[ORM\Entity(cache: true)]
class Product
{
// …
}
Configure APCu as the cache provider in doctrine.yaml:
doctrine:
cache:
second_level_cache:
provider: apcu
Verification
- Check Doctrine version via
composer show doctrine/orm-bundle. - Dump the metadata for the entity:
php -r "require 'vendor/autoload.php'; $cm = $em->getMetadataFactory()->getClassMetadata(Product::class); var_dump($cm->isSecondLevelCacheEnabled());"– it should return true. - Run a benchmark that performs many reads after writes and compare the cache hit ratio reported by the APCu stats.
Limitations
- Only available in Doctrine 3.x; older versions require a custom solution.
- Mis‑configured providers (e.g., local file cache in a multi‑instance setup) cause divergent data across servers.
- Default expiration may be too long for highly write‑heavy workloads; explicit invalidation is recommended.
When to Revert the Design
If you observe frequent cache misses, high memory consumption, or data inconsistency that cannot be resolved by explicit invalidation, consider disabling the second‑level cache for the affected entities or moving to a more suitable caching layer.
Conclusion
Enabling Doctrine’s second‑level cache on a per‑entity basis provides a lightweight, low‑latency read path while preserving write consistency. By respecting the trust boundary, using a shared cache provider, and performing regular verification, you can safely introduce this feature into production workloads.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.