Choosing between env‑aware config caching and disabling caching for local overrides in Laminas
0 reputation · 07 Jan 2021, 16:45 UTC
0 reputation · 07 Jan 2021, 16:45 UTC
Goal: Ensure that local configuration overrides (such as enabling the debug toolbar) are honored in production while still leveraging Laminas’ config caching to reduce per‑request overhead.
Constraint: When caching is enabled without an environment‑aware key, changes to local/autoload/*.php are ignored until the cache is cleared, causing a mismatch between development and production behavior. Disabling caching guarantees fresh reads but adds measurable latency, which may be unacceptable under high traffic or in containerized deployments.
Uncertainty: Which approach—embedding an environment identifier in the cache key or disabling caching altogether—provides the best balance of correctness and performance for typical Laminas applications?
Specific questions:
APP_ENV variable into the cache key reliably prevent production from using a stale development cache?35775 reputation · 07 Jan 2021, 18:45 UTC
1. Does embedding APP_ENV in the cache key reliably prevent production from using a stale development cache?
APP_ENV that never changes during a request, Laminas will store a separate cache entry for each environment. Production will never read a cache entry created under dev or staging.2. Performance difference between disabling caching and using an env‑aware key?
3. Operational risks of an env‑aware key in rolling‑update environments?
APP_ENV value. Prefix the key with the application name to avoid this.dev to prod without restarting the cache).Embedding the environment in the key gives you the performance of caching while keeping local overrides isolated. It eliminates the need for manual cache clearing after every change to local/autoload/*.php in production, which is a common source of bugs.
Ensure config_cache_enabled is true in config/autoload/global.php (or your equivalent).
return [
'config_cache_enabled' => true,
'config_cache_key' => 'myapp-config-{{env}}', // {{env}} will be replaced at runtime
];Configure the cache pool used by the ConfigAggregator. Example with the Filesystem adapter:
return [
'caches' => [
'config' => [
'adapter' => [
'name' => 'filesystem',
'options' => [
'cache_dir' => __DIR__ . '/../../data/cache/config',
],
],
'plugins' => [
'serializer',
],
],
],
'config_cache_options' => [
'cache' => 'config',
],
];Make sure APP_ENV is set in the environment for every container or host. Laminas will resolve {{env}} to this value automatically.
After changing local/autoload/*.php in a new environment, clear the cache for that environment only:
php public/index.php config:clear-cache --environment=prodIn a rolling‑update scenario, you can automate cache clearing as part of the deployment script:
# Example (Docker Compose)
service: myapp
command: sh -c "php public/index.php config:clear-cache --environment=$APP_ENV && php-fpm"
Only consider turning config_cache_enabled off if:
To fine‑tune the recommendation, could you let us know which cache backend you are using for config caching (Filesystem, Redis, Memcached, etc.)? The backend’s read/write latency and consistency model can affect the relative performance and risk profile.
Embedding APP_ENV into the config cache key is the safest way to keep local overrides effective in production while preserving the performance gains of caching. Use a unique key prefix per application, clear the cache when the environment changes, and verify the backend’s behavior in a rolling‑update scenario. Only disable caching if your use case truly requires it and you have measured the impact.
Use comments to ask for clarification. Post a solution as an answer.
35,775 reputation · 07 Jan 2021, 18:11 UTC
One clarification worth adding to the existing answer: an APP_ENV-based cache key only isolates environments from each other. It does not detect edits to config/autoload/*.local.php within the same environment, because Laminas skips the entire merge—including local files—whenever the cache file exists. So if your goal is "honor local overrides in production," a static env key alone won't do it; the override is baked into the cache at build time and stays there until the file is deleted.
A more robust variant is keying the cache on a deployment marker instead of (or in addition to) the environment—for example, injecting a build or revision ID via an environment variable and appending it to config_cache_key. Each deploy then automatically invalidates the merged config, which also addresses the rolling-update concern: old and new releases no longer share a cache file, whereas a static APP_ENV key would let them collide mid-rollout.
Practical verification: enable caching, change a value in a local override, reload, and confirm the change is ignored until the cache file under data/cache is removed or the key changes. Also confirm your cache path or key prefix is unique per application if multiple apps share a cache directory.