php artisan about and optimize: What Laravel Actually Caches

7 min read

php artisan about is an operational snapshot: environment, cache state, drivers, providers and more. Read it before guessing what a server has loaded. php artisan optimize builds Laravel’s safe production caches; it does not optimize your SQL or application design.

config:cache means runtime application code must call config(), never env(). route:cache fails on duplicate route names and on closures that capture non-serializable values. event:cache speeds listener discovery but must be rebuilt after changing listeners. view:cache compiles Blade templates. Deploy atomically, rebuild caches using the release’s code and configuration, then restart long-lived workers.

In development, clear a cache to diagnose a stale result; do not add optimize:clear to every deploy out of habit. Caches are generated artifacts with a lifecycle, not magic.

Start with evidence, not a cache-clearing reflex

about is useful because it shows the application as Laravel sees it, rather than as a shell session assumes it is configured. Run it inside the same image, release directory and user context that serves traffic:

bash
php artisan about
php artisan about --only=environment
php artisan about --only=cache

The exact sections vary between Laravel versions and installed packages, but the useful questions are stable:

  • Which environment and application URL is this process using?
  • Is configuration cached, and was the cache built from this release?
  • Are routes and events cached?
  • Which cache, session and queue drivers are actually configured?
  • Is the queue worker consuming the same release that answered this command?

That last question catches a common incident. A deployment updates a symlink or container image, web requests use the new code, but a long-running worker still has old configuration and classes in memory. about does not prove the worker was restarted; it gives the operator a concrete baseline to compare with the worker's process, release SHA and logs.

What each cache changes

Laravel's optimize commands are deliberately conservative. They cache metadata Laravel can safely derive from a release. They do not cache Eloquent results, compile arbitrary PHP, add database indexes or repair a slow N+1 query.

Configuration cache

config:cache merges the files under config/ into one generated PHP file. It avoids loading many configuration files for every request, but it changes the valid source of configuration at runtime:

php
// config/services.php: correct place for environment access
return [
    'payments' => [
        'endpoint' => env('PAYMENTS_ENDPOINT'),
        'token' => env('PAYMENTS_TOKEN'),
    ],
];

// Application code: read the resolved configuration value.
$endpoint = config('services.payments.endpoint');

This is not just style. With a cached configuration, Laravel may not load .env for the request at all. An env('PAYMENTS_TOKEN') call in a controller can therefore return null in production while seeming fine locally. Put defaults, casts and environment access in configuration files; keep application code on the config() side of that boundary.

Do not cache configuration when a process deliberately changes environment values without a restart and expects PHP to see them. That is rare and is usually a sign that deployment configuration needs a more explicit lifecycle. For ordinary production releases, configuration caching is the right default.

Route cache

route:cache serializes the route collection. It speeds route registration, which matters in applications with many routes or packages, but every route must be serializable. Modern Laravel can serialize plain closure routes, so a simple closure does not block the cache. What does block it: two routes sharing a name, and a closure that captures something unserializable through use, such as a resource, a PDO handle or an object holding one:

php
// Serializable, but the captured object is not: route:cache fails.
$client = new SomeClientHoldingAResource();
Route::get('/health', fn (): array => ['ok' => $client->ping()]);

// Compatible and easier to test independently.
Route::get('/health', HealthController::class)->name('health');

Prefer a controller or invokable action for routes intended for a cached production application: it is testable and cannot capture state by accident. Never wait for deployment to discover that the release cannot build its cache; run php artisan route:cache in CI against the release candidate.

Event and view caches

event:cache records event/listener discovery. It is useful when listener discovery scans a sizeable codebase, but it must be rebuilt whenever listeners, subscribers or event discovery configuration changes. If an event appears to stop reaching a new listener immediately after deployment, compare the release SHA and event cache before changing the listener code.

view:cache compiles known Blade views ahead of the first request. It removes first-hit compilation cost but does not turn dynamic views into static pages. If a deployment ships new Blade templates while an old release directory still owns the compiled-view path, it can serve confusing output. Keep compiled views inside the release or a release-aware writable path and clear them only as part of a controlled release transition.

A safe release sequence

Treat generated caches as artifacts of a specific code revision and specific configuration. The exact commands differ for containers, Envoy, Deployer or a CI runner, but the ordering should resemble this:

bash
# Run in the new, not-yet-live release.
php artisan migrate --force
php artisan optimize

# Switch traffic atomically using the platform's release mechanism.
# Then ask long-lived processes to finish current work and reload code.
php artisan horizon:terminate

# Verify the release rather than assuming the command succeeded.
php artisan about --only=cache
php artisan route:list --name=health

The migration line belongs only when its migration is safe for the currently running code. For a schema change that is not backwards compatible, use an expand-contract rollout instead: deploy compatible code, backfill, switch reads, then remove the old schema later. Cache correctness cannot rescue an unsafe database rollout.

horizon:terminate asks Horizon workers to finish their current job and restart under the supervisor. A non-Horizon worker needs the corresponding safe restart mechanism for its process manager. Do not use kill -9 as a routine deploy step: interrupted jobs can be retried, partially write data or leave an external provider request in an ambiguous state.

Common failure modes

"The environment value changed, but the app still uses the old one"

First check whether configuration is cached. If it is, update the deployment secret or environment, build configuration cache in the new release, and restart long-lived processes. Clearing only an application cache store such as Redis will not change config.php or the memory of an existing worker.

"The new route returns 404 only in production"

Confirm the deployed SHA, then inspect route cache and run route:list in the same runtime. The cause may be an old image, a cache built before the route was added, a route group condition that differs by environment, or a missing domain constraint. Do not begin by deleting every cache: that destroys evidence and can briefly make the service slower under load.

"Clearing caches fixed it"

That is a symptom, not a diagnosis. Record which cache was stale, why it was shared across releases or not rebuilt, and which deployment step should own it. Otherwise the next release repeats the incident and the team accumulates an unsafe cargo-cult command such as optimize:clear && optimize on live traffic.

When optimize is not the answer

Do not reach for these commands to fix slow database queries, expensive HTTP calls, unbounded queue throughput or memory leaks. Profile the request, inspect query plans, measure queue wait time, or investigate the worker respectively. Laravel's metadata caches are a small startup optimization and a deployment concern; they are not a general performance strategy.

The operational habit worth keeping is simple: generate caches from the exact release and configuration that will serve traffic, restart processes that retain memory, and verify the live runtime afterward. Once those ownership boundaries are explicit, optimize becomes boring—in production, that is exactly what you want.

Related articles

Dominik Jasiński

Dominik Jasiński

Laravel & PHP developer · freelance and B2B subcontracting

Existing system support

Need help with a live application?

I help companies improve live systems, clean up delivery workflows, and ship new features without adding avoidable complexity.

For software houses & agencies
Comments (0)
Sign in to leave a comment

You need to be signed in to add a comment.

Login