Currently Available: Need a skilled Software Developer for your next project?
Categories
Laravel

Why Long-Running PHP Worker Processes See Stale Config and Cached Data, and How to Fix It

A long-running PHP worker is a PHP process that stays alive while it handles multiple jobs or requests. Queue workers and application servers such as Laravel Octane reuse that process across tasks. Because the worker keeps the framework and its service objects in memory between tasks, a running worker can keep using old values after you deploy new configuration or code.

That stale value might live in the worker’s memory, the framework’s configuration cache, PHP’s OPcache (which stores compiled PHP scripts), or a shared store such as Redis. Each layer refreshes on its own schedule. Start by finding where the stale value lives. Once you know, refresh that cache or data source and restart any worker that still holds the old value in memory. When you restart a worker, you clear its memory, but you do not clear every cache.

Find where the old value lives

Begin by identifying what is actually stale. If a configuration value is already outdated when the framework boots, inspect the framework’s configuration cache or the values it loaded at startup. If PHP behaves as though a file still contains old code, check OPcache. If a database lookup returns an old result, check the cache that stores database results.

A worker can also reuse the same service object across jobs. A service is an object that handles application work, such as sending email or calculating a price. If that object stores job-specific data in a property or a static variable and never clears it, a later job can read the earlier value. Symfony documents that Messenger workers reuse service instances and that properties can leak state between messages.

You can narrow the search by checking how many workers show the problem, though that count alone does not prove the cause. If one worker behaves differently from its peers, inspect that process’s in-memory state and startup-loaded values. If workers on multiple hosts return the same old result, inspect your shared configuration or cache storage. To match a stale result to a specific process, log the worker’s process ID, deployed release identifier, or a fingerprint derived from its configuration, then compare those logs with the stale output.

Refresh Laravel configuration before workers boot

In Laravel, set the deployment environment to its intended values, then run php artisan config:cache during deployment. That command combines your configuration files into a single cached file. Once Laravel creates this cache, it stops loading .env during requests and Artisan commands, and env() returns only environment variables set outside .env. Keep env() calls inside your configuration files and read settings everywhere else with config(), as the Laravel configuration documentation directs.

If a setting changes after Laravel builds the configuration cache, rebuild the cache. You can run php artisan config:clear to remove the cached configuration file. Laravel’s deployment documentation also mentions optimize:clear, which removes optimization-generated files along with all keys in the default cache driver. Check what your default cache driver stores before you run optimize:clear, because it can wipe application data beyond configuration.

When you rebuild the configuration cache, a running worker still holds the values it loaded at startup. After you update the cache, restart the worker so it boots with the new configuration.

Clear per-job state from reused services

When a worker reuses a service object, make sure the service clears task-specific data before the next task begins. Symfony Messenger handles this through ResetInterface. A service that holds values between messages implements the interface and clears any properties that should not carry over. Symfony’s service container documentation explains how service resets work in long-running processes. After you add a reset method, test that the service still behaves properly once it resets.

Laravel Octane boots the application once in each request worker and reuses it. Its Laravel 10.x guidance warns that an object can hold on to a request or application container it received in its constructor. Because Octane reuses that object, later requests can end up using the old request or container instance. Do not store request-specific data in static collections or long-lived service properties, and do not capture a request or container in a constructor when each request needs its own instance.

Check OPcache separately from application data

PHP’s OPcache stores compiled PHP scripts so PHP does not have to compile each file again. It only affects changed code files. It has no effect on business data held in a service property, Redis, or a database cache. The PHP OPcache manual explains that opcache.validate_timestamps turns timestamp checks on or off. When those checks are on, opcache.revalidate_freq sets how often PHP checks whether scripts have changed. When timestamp checks are off, PHP does not detect file updates on its own. In that case, you need to clear the cached script with opcache_invalidate(), reset the cache with opcache_reset(), or restart the relevant server.

Make sure you check the OPcache settings for the exact PHP process serving the affected requests. Command-line PHP, PHP-FPM, and an application server can each use different configurations. PHP-FPM is PHP’s FastCGI process manager. If you restart an FPM pool, a separate queue consumer or Octane worker keeps running. Confirm which process produces the stale result before you choose what to restart.

Restart workers gracefully and recycle them periodically

Laravel queue workers do not reboot the framework before every job. During deployment, you can run php artisan queue:restart to tell running workers to finish their current job and then exit. A process monitor such as Supervisor must then start replacement workers. Workers normally check the cache for a restart signal while they look for the next job. If Laravel disables interruption polling, workers will ignore that signal, so check your polling configuration before running the command again. See the Laravel queue documentation for details.

Symfony Messenger also uses the application cache to tell workers to stop. If you run workers across multiple hosts, configure a shared cache adapter that all hosts use so every worker sees the signal. When each deployment creates a new release directory, Symfony recommends setting cache.prefix_seed so the cache namespace (the name used to group cache entries) stays the same across releases. Its Messenger documentation also advises giving workers enough shutdown time to finish their current message.

You can also recycle workers after a chosen number of jobs or a set amount of time to limit memory growth and make them reload code and configuration periodically. Laravel offers --max-jobs and --max-time, while Symfony Messenger offers options such as --limit, --time-limit, and --memory-limit. These options will not fix a service that leaks state on every job. Pair them with a process monitor, and pick limits that fit your workload and shutdown window.

When you restart a worker, you clear the memory held by that process, but you do not clear a shared Redis or database cache. Laravel’s Octane cache works differently. Workers on one server share it, and Octane flushes it when you restart the Octane server. For any other cache backend, check its documentation or behavior directly rather than assuming a restart clears it.

What I'm building

Delegate tasks. Get software.

Give Vroni a GitHub issue, bug report, spec, or rough idea. It reads the repo, plans the change, writes code, runs checks, and works toward a review-ready pull request.

Take a look at vroni.com

Email updates

Usually a new article and a few links I found interesting.

No spam. Unsubscribe with one click.

Leave a Reply

Your email address will not be published. Required fields are marked *