14 Laravel performance tips and ideas

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.

Commands are useful when you are using lots of routes and configuration files.

Tip: Don’t forget to clear cache after changes.

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.

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.

For every book in database eloquent will run separate SQL query (21 queries instead of 2).

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.

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.

SUBSCRIBE TO LARAVEL NEWSLETTER
Loading

16 Comments
  • Jackson says:

    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 am
  • Hasan says:

    really helpful tips, thank you much!

    March 10, 2018 at 11:52 am
  • laravel-bap says:

    Thank You! I will publish new article every week.

    March 10, 2018 at 3:23 pm
  • Franky says:

    Thanks, really useful. I have tested point 6 model caching with an ecommerce site, it does an amazing job!

    March 11, 2018 at 2:13 am
  • Radu says:

    SPA 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 pm
  • Just a note, php artisan optimize was deprecated in L5.5 and removed in L5.6

    March 17, 2018 at 11:20 am
    • laravel-bap says:

      Yes,I was reading about that. I updated Article. Thank You.

      March 17, 2018 at 11:35 am
  • Philippe Pérusse says:

    Great article.
    For point 5, Laravel will actually do two (or more) requests, the main and a second one for each with parameter.

    March 18, 2018 at 1:50 pm
  • Muhammad Ibnuh says:

    Totally love it, 2 line for big performance difference

    March 18, 2018 at 2:52 pm
  • Liviu says:

    It.s deprecated as of Laravel 5.5, here you go https://github.com/laravel/framework/pull/20771

    March 18, 2018 at 8:08 pm
    • laravel-bap says:

      I fixed this in article. Thank You.

      March 18, 2018 at 8:15 pm
  • William says:

    I 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 am
  • Iman says:

    Yet 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
    https://github.com/imanghafoori1/laravel-widgetize

    June 24, 2018 at 6:09 pm
  • Oliver Russell says:

    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 am
  • amra says:

    it’s very helful, thank you

    October 22, 2018 at 12:14 am
Leave A Comment
*
*

264 Shares
Share262
Tweet
Pin2
Share
%d bloggers like this: