Cache Lumen Routes to Reduce Request Bootstrap Overhead
Enable route caching in Laravel Lumen to skip re-parsing routes on every request, then regenerate the cache after route changes.
29 Sept 2025, 13:09 UTC

Desired outcome
Reduce the time Lumen spends bootstrapping the router on each production request by replacing repeated route registration with a compiled route cache.
Before you begin
- A Lumen application installed and reachable over HTTP.
- Command-line access to the project root.
- Write permission for the framework's cache storage directory; the exact path varies by Lumen release, so verify it for your installed version.
- Route definitions that can be serialized. Controller string references are safe. Closures that capture non-serializable dependencies will cause cache generation to fail.
Version assumption: this guide describes established Lumen route-caching behavior. Command output and cache file locations can differ between Lumen releases; check your installed version before applying the steps below.
Procedure
1. Audit routes for cache compatibility
Open your routes file—commonly routes/web.php—and prefer controller strings over closures:
// Cache-friendly
$router->get('/users', 'UserController@index');
// Risky: captures a non-serializable dependency
$router->get('/report', function () use ($reportGenerator) {
return $reportGenerator->run();
});
If a closure binds to the container, request scope, or another non-serializable object, php artisan route:cache will fail during unserialization.
2. Generate the cache on staging first
Run the command from the project root. Do not run it during active development unless you intend to freeze routes until the cache is rebuilt.
cd /var/www/lumen-app
php artisan route:cache
Required permissions: the user executing the command needs write access to Lumen's cache storage directory. Risk: if any route definition cannot be serialized, the command fails and no cache is written.
Expected check: the command prints a success message, and a compiled route file appears in the framework's cache storage directory. Do not assume a specific filename; verify the path for your Lumen version.
3. Verify that the cache is active
- Request a known route and confirm it responds.
- Add a temporary route to
routes/web.phpand request it. The new route should not respond while the cache is active. - Run
php artisan route:cacheagain. The temporary route should now respond.
This confirms the router is loading the compiled file and ignoring live route-file changes until the cache is refreshed.
4. Deploy and regenerate after route changes
Run php artisan route:cache as part of your production deployment, and again whenever routes are added, removed, or modified. Cached routes do not update automatically.
Expected checks
| Check | Expected result |
|---|---|
php artisan route:list | Lists registered routes without errors. |
| Cached route file exists | Compiled route file present in Lumen's cache storage directory for your version. |
| Route added after caching | Not active until route:cache is run again. |
| Production request timing | Router bootstrap overhead lower than with dynamic registration under identical load; measure before and after to confirm. |
Recovery: revert to dynamic routes
Because route:cache changes application state, the supported rollback is to clear the cache:
cd /var/www/lumen-app
php artisan route:clear
This removes the compiled route file and restores dynamic route registration from routes/web.php. Use it if route matching fails after caching or if you need to debug route files during an incident.
Limitations
- Route caching optimizes route matching and registration only. It does not cache middleware execution, controller logic, or database queries.
- Avoid
route:cacheduring active development; new routes are ignored until the cache is rebuilt. - Closures bound to the container or request scope may not unserialize correctly from the cache.
- Cache file paths and command availability differ between Lumen and Laravel framework releases.
Practical verification
After deployment, compare request timing or router profiling before and after caching under identical load. If you do not see a measurable difference, the bottleneck is likely elsewhere in the stack. Confirm the compiled file exists and that route changes remain inactive until you rerun php artisan route:cache.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.