Idea to improve the API performance

I would like to know if there is a way to reduce the time of requesting routes via API using Route?

The problem is that when querying any of the routes via API, all the plugins and modules in the system are also called. We tried running tests on pure Laravel and on OctoberCMS to create API routes and still found that Laravel handles requests faster in this regard, but that’s not surprising and generally understandable.

There are a couple of ideas, but I don’t have much idea if they can be implemented:

  1. Make it possible to exclude calls to all or to specific plugins during API route calls.

  2. Isolate API or create one plugin that will be responsible only for everything related to API without calling other plugins and modules of the system.

We mostly use OctoberCMS as an admin panel to quickly create plugins and interfaces, while the frontend is usually developed in NuxtJS or NextJS as a separate application and communicates via API.

​Regarding the performance of API routes, there are several factors that can affect the response time. Internally, October CMS modules use optimized code that should follow Laravel’s standards, such as deferred providers, with minimal impact to performance. However, since plugins are essentially Laravel providers and following the same life cycle, a plugin with inefficient code runs the risk of slowing down the system. It is good idea to benchmark the system with selective plugins disabled to determine if any are causing problems.

Regarding your suggestion, it is indeed possible to improve the performance further using provider isolation for certain routes. This is achieved by configuring the web server (Apache, Nginx, etc.) to apply specific environment configuration for matching routes. The environment variables LOAD_MODULES and DISABLE_PLUGINS can be used to only load the bare minimum system requirements for responding to the request. This is something for advanced users and configuring it at the web server level is necessary for maximizing the performance gains.

For example, the following will only load the System and Tailor modules and disable the RainLab.Blog and Responsiv.Campaign plugins.

LOAD_MODULES=System,Tailor
DISABLE_PLUGINS=RainLab.Blog,Responsiv.Campaign

​To apply this in Apache, the following .htaccess rule can be used the apply these environment variables to all URLs starting with /api

RewriteCond %{REQUEST_FILENAME} ^/api/.*
SetEnv LOAD_MODULES "System,Tailor"
SetEnv DISABLE_PLUGINS "RainLab.Blog,Responsiv.Campaign"
RewriteRule ^ index.php [L]

Be sure to run the php artisan october:optimize command in the production environment along with debug mode disabled. This results in a significant performance boost since it will cache the plugin structure and route table, instead of constantly looking at the file system.

​Another thing to try is Swoole or Laravel Octane, which keeps the PHP code running in memory and negates most of these performance concerns.

2 Likes