Performance is a great deal in any application. Everybody wants their system to be fast. No company can boost its business without a proper system with good performance. We have seen many companies that are left behind by competitors due to lack of good performance of their application. Thus, to compete in the market, we need a system that has great performance.
Laravel is a most popular PHP framework in the recent years. It has been continuously adding new features since it’s the first release. It is used to build small applications to large enterprise applications. Laravel is fast but we can make it faster with some tips and tricks in the production.
Below are the steps we can follow to optimize our laravel application in production.
Route Caching
Caching is a nice feature in laravel that helps to boost the performance. Caching routes help to dramatically decrease the amount of time to register all the application’s routes. We can generate a route cache, we have to run the following artisan command:
php artisan route:cache
After we run the command, all our routes will be cached. So, when the users request certain routes, the routes will be loaded from cache. But, when new routes are added, they will not hit unless cached routes are cleared. Thus, it is best to cache the routes only on the production. We can clear cached routes using artisan command:
php artisan route:clear
Note: If you have a problem with the application cache, you can Clear route, view, config and application cache data in Laravel 5 following the steps mentioned in the article.
Config Caching
Similar to route cache, we have feature for config cache. We can cache our config files into a single file which will be loaded quickly upon request by the framework. This artisan command should also be executed on the production because, during the development stage, we need to add and change various configuration files. To cache configuration files, run the following command:
php artisan config:cache
Eager Loading of Data
Another method we can implement is to reduce the queries with the database. This is really important to optimize the performance of the application. The developers should really follow this query with their database. When we have multiple relationships, we must implement eager loading.
What is the problem with eloquent relationships is the N+1 query problem? It is much faster to issue 1 query which returns 100 results than to issue 100 queries which each return 1 result. For example, we have a hotels
table and rooms
table with relationship. In this case, we can execute in a normal way like this:
$hotels = App\Hotel::all(); foreach ($hotels as $hotel) { echo $hotel->room->name; }
But, using eager loading, we can reduce the database queries as belwo:
$hotels = App\Hotel::with('room')->get(); foreach ($hotels as $hotel) { echo $hotel->room->name; }
Right Cache & Session Driver
By default, laravel uses file cache driver. For optimal performance, you can use the Memcached driver or Redis driver. These are more advanced and faster compared to file cache driver. To see the configurations setting, you can read the official documentation . You can find the configuration about cache in config/cache.php
Similar to Cache driver, you can choose the fastest driver for the session. Laravel supports file, cookie, database, Memcached and Redis. You can find the configuration about session in config/session.php
.
Queues
Queue helps in the asynchronous process by putting heavy tasks on the stack and dispatching these task after sometimes. We can use the queue for the various heavy task such as sending emails. Using queue for such task will drastically increase the performance of the system. The main principle behind this is that the tasks are executed asynchronously. You can also read the implementation of How to use Queue in Laravel 5 in this article.
Database Caching
Another point that helps to boost the performance of the application is database caching. With database caching, we can display the repeated database query results from the cache. This will help to reduce the overload query to database making the query faster. When we execute the query for the first time, after we receive results, we can cache the results. Then, we can show the results from the cache when requested later. For this, we need to pass the time which indicates the time the results should be cached. For example:
$value = Cache::remember('hotels', $minutes, function () { return DB::table('hotels')->get(); });
Similarly, if we can also put the query results in cache forever. This is especially useful when we know that the results don’t change forever. For example, list of countries.
$value = Cache::rememberForever('countries', function() { return DB::table('countries')->get(); });
Assets bundling
Laravel Mix provides a fluent API for defining Webpack build steps for your Laravel application using several common CSS and JavaScript pre-processors. Laravel Mix helps us to combine multiple stylesheets and scripts into single file. The below code will combile two stylesheet i.e. normalize.css
and videojs.css
into single stylesheet all.css
.
mix.styles([ 'public/css/vendor/normalize.css', 'public/css/vendor/videojs.css' ], 'public/css/all.css');
Similarly, we can combine scripts into single file as like below:
mix.scripts([ 'public/js/admin.js', 'public/js/dashboard.js' ], 'public/js/all.js');
Latest PHP Version
The latest version of PHP is 7.2 which is considered to be a revolutionary release. There are various reasons for this. The main reason is that it is super fast than previous PHP versions. You upgrade your server PHP versions to latest to boost the performance of your application. Besides, speed, there are various new features which are explained in details in the article New Features In PHP 7.
Comment Unused Service
As you open the config/app.php file, you will see there are a lot of service providers which loads at the beginning of the application. You may not need all the providers. In this case, you may comment the unused providers to make your application load faster. But, you need to be careful that it doesn’t affect your main application.
Conclusion
One of the key factors that directly affect the success of the business is the performance of the application. Laravel applications are fast but we can even boost the performance with the help of the above-mentioned methods. You can even try and test on your local development. If you have any questions or feedback, please comment below.