Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

vedoj's avatar
Level 1

Does too many middlewares registered in Kernel.php slow down your app?

In my app I have a lot of middlewares.

But, on average, a link will use about 4 - 5 middlewares - maybe even less.

Homepage only 1 middleware for some basic logs.

The questions is whether having too many custom middlewars registered in Kernel.php slow down the website or only the middleware relevant for a specific route matter and are executed?

Example:

Route::middleware('logBasicStuffForEveryPage')->group(function () {
    Route::get('page1', [Page1Controller::class, 'index'])->middleware(['a','b']);
    Route::get('page2', [Page2Controller::class, 'index'])->middleware(['a','b','c','d','e','f','g','h','i','j','k','l','m']);
});

When a person accesses page1 will the server work less as when he accesses page2?

Will accessing both pages make the server work the same because either way the server alread yloaded all 20 middlewares from Kernel.php into the memory and the CPU had to process everything? Given that a...m middlewares are all registered in Kernel.php in $routeMiddleware array, of course

0 likes
14 replies
vedoj's avatar
Level 1

@vincent15000 I am not sure you get me. I want to know whether I could have e.g. 100 (don't focus on this example number 100 I just want to stress that the number is more than a few e.g 2 or 3) various middlewares and register them all in Kernel.php - because they could be useful in the longterm aso, aso (in Zizek voice ;) without slowing down my website just by being there.

Or even other formulation of this question:

Does the number of middlewares in the $routeMiddleware in Kernel.php matter if you don't use any of them and they stay there e.g. for the future use or because the God said you so ;) ?

1 like
vincent15000's avatar

@vedoj I had understood what you asked. Once again, you can test it with the Laravel debug bar.

Via visiting a route, only the assigned route middlewares are used and not the other route middlewares.

By using the Laravel debugbar, you would have seen which middlewares are used among all route middlewares. ;)

vedoj's avatar
Level 1

@vincent15000 So, does that mean I can have even 100 middlewares registered in Kernel.php but non assigned in web.php , and the website will have the same speed as when there were no custom 100 middlewars in Kernel.php?

I am just learning the right startegies o do things in Laravel.

It would be great if I could have more middlewares "at the ready" in Kernel.php , so when the need arives I can use them.

As I said I will only a small portion of them but it would be cool to have an arsenal of predone middlewares to be use when needed.

However, if the act of registration of those middleware classes will slowdow the website I will have to try to find a different way.

vedoj's avatar
Level 1

I don't know if only the middlewares actualy set for a page the user is hitting get executed and evaluated, or all middlewares are being loaded for every page all the time.

1 like
martinbean's avatar

@vedoj Instead of asking vague questions, you should actually profile the middleware you’re applying to a route.

All code has a cost. More code = more cost. But what that cost is depends on what each individual middleware is actually doing. It could add nanoseconds to a request. It could add full seconds if it’s doing something intensive like a large database query or an API call.

So response time isn’t based on the number of middleware you have; it’s based on what those middleware actually do inside their handle methods. So the answer is: It Depends™.

2 likes
vedoj's avatar
Level 1

@martinbean does registering of those middlewares in Kernel.php and not using them triggers those DB calls in the code?

1 like
martinbean's avatar

@vedoj As I mentioned, it completely depends what you do in your middleware as to what kind of impact it will have on response times.

1 like
jlrdw's avatar

Usually adding all kinds of middleware is totally unnecessary, as far as security is concerned let Authentication equal a login is required and authorization will determine what the logged in user can or cannot do.

1 like
vedoj's avatar
Level 1

@jlrdw Does adding middlewares in he array $routeMiddleware in Kernel.php trigger the code (DB calls, computation, etc.) in those middleware classes? Or only when those middlewares are triggered via a route that has them assigned to?

1 like
jlrdw's avatar

@vedoj the actual class would be "triggered" as you say when called. But classes are registered each request cycle.

I suggest reading about the laravel request cycle in the documentation.

1 like
jlrdw's avatar

Also, since this post is answered can you close the thread.

1 like

Please or to participate in this conversation.