Here are 14 performance tips and ideas that I use for Laravel Applications. Not all are related to Laravel but also worth reading.
1. Use artisan commands
Laravel comes with commands for boost performance.
1 2 3 4 5 6 |
php artisan config:cache php artisan route:cache // Note: Artisan Optimize was removed in laravel 5.5 this works in previous versions. php artisan optimize --force |
Commands are useful when you are using lots of routes and configuration files.
Tip: Don’t forget to clear cache after changes.
1 2 3 4 5 |
php artisan config:clear php artisan route:cache php artisan view:clear |
2. Use deployment tool to invoke all commands for You.
This is not performance tip – but this will reduce Your time and that’s matters 😉
Deployer is a simple deployment tool for PHP. If you’ve ever used Composer to manage your project dependencies, you’ll feel right at home with Deployer.
Deployer have scenarios to deploy Laravel Application it’s runs migrations, seedes, optimization commands with one command.
1 |
php deployer.phar deploy production |
There is article about implementing deployer https://deployer.org/blog/how-to-deploy-laravel
3. Minimize Use of Laravel Plugins
There are many plugins for Laravel that allow you to easily add more functionality. Remove from composer plugins that You are not using.
4. Profile queries
Install debugbar – https://github.com/barryvdh/laravel-debugbar
Debug Bar will allow you to see what queires are invoked on each pages.
5. Use “Eager Loading”
When you invoke queries via Eloquent, it uses “lazy loading” approach.
1 2 3 4 |
$books = App\Book::all(); // 20 books in table foreach($books as $book){ echo $book->author->name; } |
For every book in database eloquent will run separate SQL query (21 queries instead of 2).
1 2 3 4 5 |
$books = App\Book::with('author')->get(); foreach($books as $book){ echo $book->author->name; } |
Eager load query will run only two SQL queries.
6. Use Caching
Cache anything you can. I use model caching for every dictionary (user type, contact status etc..) loaded from database. Caching model will also automatically invalidate itself after saving/updating of Eloquent object.
Cache model – Read Article about Laravel Model Caching
I really recommend this lib with a clear conscience. It helped me a lot. Caching of model allowed me to reduce from 100 queries in one page to 6 and lower page load time to 2 second.
tip: Laravel model caching requires php 7.1
Cache full response – https://github.com/spatie/laravel-responsecache
If Your website is in most static content you can cache full response.
7. Use Redis or Database cache instead of file cache
Redis is an open source (BSD licensed), in-memory data structure store, used as a database, cacheand message broker.
https://laravel.com/docs/5.5/redis
8. Minify and combine css and javascript
I use javascript, css combine and minify. This allows to lower request from website.
Packer – https://github.com/eusonlito/laravel-Packer
Packer library will automatically combine, minify and create statis files in folder.
Example: If you include 20 different css files in layout this will combine them to single file.
9. Mod Expires
Set expires information. Caching javascript, css , images and other assets in browser memory allows to speed-up page load time.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
## EXPIRES HEADER CACHING ## <IfModule mod_expires.c> # Fonts # Add correct content-type for fonts AddType application/vnd.ms-fontobject .eot AddType application/x-font-ttf .ttf AddType application/x-font-opentype .otf AddType application/x-font-woff .woff AddType image/svg+xml .svg ExpiresActive On ExpiresByType image/jpg "access plus 1 month" ExpiresByType image/jpeg "access plus 1 month" ExpiresByType image/gif "access plus 1 month" ExpiresByType image/png "access plus 1 month" ExpiresByType text/css "access plus 1 month" ExpiresByType application/pdf "access plus 1 month" ExpiresByType text/javascript "access plus 1 month" ExpiresByType application/javascript "access plus 1 month" ExpiresByType application/x-javascript "access plus 1 month" ExpiresByType application/x-shockwave-flash "access plus 1 month" ExpiresByType image/x-icon "access plus 1 month" ExpiresByType image/ico "access plus 1 month" ExpiresByType text/css "access plus 1 month" ExpiresByType text/css "now plus 1 month" ExpiresByType application/vnd.ms-fontobject "access plus 1 month" ExpiresByType application/x-font-ttf "access plus 1 month" ExpiresByType application/x-font-opentype "access plus 1 month" ExpiresByType application/x-font-woff "access plus 1 month" ExpiresByType image/svg+xml "access plus 1 month" ExpiresByType text/html "access plus 600 seconds" ExpiresDefault "access plus 2 days" </IfModule> |
10. If You are still using php 5 – Change it to 7
Here is good article about comparison of php 5.7 and php 7.0
https://www.cloudways.com/blog/php-5-6-vs-php-7-symfony-benchmarks/
11. Optimize your mysql database
Use query caching – https://www.howtogeek.com/howto/programming/speed-up-your-web-site-with-mysql-query-caching/
12. Use New Relic
New Relic is great tool. New Relic provides deep insight into what’s happening in your Laravel application. This will allow You to find memory leaks, slow queries etc.
https://newrelic.com/php/laravel
13. Use pusher or similiar library for live messages
Before sockets, pusher, laravel echo, socket.io i used to pull ajax data in intervals for “Real life experience 😉 “. Sometimes there was hundreds of queries to database in few second..
14. Rewrite application to SPA – Single Page Application
When You use SPA on each page you will load only required data as ajax queries.
Good Example of SPA library: https://github.com/vuejs
Laravel-BAP Modular Backend Application Platform + Example CRM with 17 modules
You have additional ideas? Thoughts ? Share them in a comments.
Nice article and site! Point 6 is my fav. I will definitely visit her more often to check whats new.
March 10, 2018 at 11:06 amreally helpful tips, thank you much!
March 10, 2018 at 11:52 amThank You! I will publish new article every week.
March 10, 2018 at 3:23 pmThanks, really useful. I have tested point 6 model caching with an ecommerce site, it does an amazing job!
March 11, 2018 at 2:13 amSPA requires high end devices with a lots of RAM, also it can’t handle very well the SEO, I drop off the #14
March 11, 2018 at 7:17 pmJust a note, php artisan optimize was deprecated in L5.5 and removed in L5.6
March 17, 2018 at 11:20 amYes,I was reading about that. I updated Article. Thank You.
March 17, 2018 at 11:35 amGreat article.
March 18, 2018 at 1:50 pmFor point 5, Laravel will actually do two (or more) requests, the main and a second one for each
with
parameter.Totally love it, 2 line for big performance difference
March 18, 2018 at 2:52 pmIt.s deprecated as of Laravel 5.5, here you go https://github.com/laravel/framework/pull/20771
March 18, 2018 at 8:08 pmI fixed this in article. Thank You.
March 18, 2018 at 8:15 pmI was recently looking into implementing MySQL Caching, but found it is has been deprecated as of ver 5.7.20 (https://dev.mysql.com/doc/refman/5.7/en/query-cache.html)
March 20, 2018 at 12:49 amYet another package which helps you in caching not your database queries but your page partials giving the most performance possible.
https://github.com/imanghafoori1/laravel-widgetize
June 24, 2018 at 6:09 pmhttps://github.com/imanghafoori1/laravel-widgetize
Another laravel performance optimization tip (https://www.cloudways.com/blog/laravel-performance-optimization/ ), you can use memcached for database query performance optimization. This will further improve the performance of the app.
June 26, 2018 at 11:39 amit’s very helful, thank you
October 22, 2018 at 12:14 am