This website uses cookies

Our website, platform and/or any sub domains use cookies to understand how you use our services, and to improve both your experience and our marketing relevance.

CloudwaysCDN — a powerful solution that offers superior performance and satisfied global audience for your business. Read More

12 Tips for Laravel Performance Optimization

Updated on  23rd May

7 Min Read
Reading Time: 7 minutes

Since its emergence in 2011, Laravel has become a very popular option for developing business-focused applications including information management systems (popularly known as business information management systems) and ecommerce platforms. One important aspect of this popularity is Laravel performance optimization that allows developers finetune the performance of Laravel apps.

laravel performance optimization

Why Businesses Should Focus on Laravel Performance Optimization

The structure of the framework and the associated libraries ensures that developers are able to create powerful code with minimum effort. However, the code still has room for optimization that could be used for Laravel performance tuning to ensure smooth performance after deployment.

Performance and optimization are two key factors that determine the success of every business applications. In this context, ensuring the performance of Laravel application is a crucial skill that every developer should be able to deliver to their clients. Since Laravel is often used to build business information systems, the performance of Laravel powered applications has serious implications for the success of the business. In many cases, the management information systems that provide decision making support to management layers need to be fast and high performing at all times.

In this article, I have collected several important tips of optimizing Laravel 5.5 apps performance. I believe these tips would prove to of great help to Laravel developers who are responsible for maintaining Laravel powered business apps.

For the purpose of demonstrating the ideas discussed in this article, I will use a Employee Management System in Laravel .

Prerequisites

For the purpose of this tutorial, I assume that you have a Laravel application installed on a web server. My setup is:

  • Laravel 5.5
  • PHP 7.1
  • MySQL

To make sure that that I don’t get distracted by server level issues, I decided to use Cloudways Laravel cloud hosting because it takes care of server level issues and has a great devstack right out of the box. You can try out Cloudways for free by signing for an account.

You might also like: Enhanced Cloudways Staging Environment Is Now Available for All Users

Tips to Improve Laravel Performance

  1. Config caching
  2. Routes caching
  3. Remove Unused Service
  4. Classmap optimization
  5. Optimizing the composer autoload
  6. Limit Use Of Plugins
  7. JIT Compiler
  8. Choose a faster cache and session driver
  9. Cache the queries results
  10. Use “Eager Loading” of Your Data
  11. Precompile Assets
  12. Use CDN for delivering your static assets.

Config Caching

Laravel provides an exceptionally interesting command, Artisan Cache Config that is very helpful in boosting performance. The basic usage of the command is:

Once you cache the config, the changes you make do not have any effect. If you wish to refresh the config, just run the above command once again. In order to clear the config cache, use the following command:

To further optimize the application, you could use OPcache that caches the PHP code so you don’t need to recompile it.

You might also like: How To Setup Laravel Application On Cloudways With Redis Cache

Routes Caching

Routes caching is an essential optimization feature, particularly for apps with a lot of routes and configuration. The routes cache is a simple array and helps in speeding up Laravel performance because of faster loading of the array. For this, run the following command:

Remember to run the command every time config or the routes files have been changed. Otherwise, Laravel will load old changes and from the cache. For clearing the cache, use the following command:

You might also like: An Introduction To Routing In Laravel

Remove Unused Service

In the context of Laravel performance tuning, an important tip is not to load all services through the config. While you are there, always remember to disable unused services in the config files. Add comments to these service providers.

Classmap Optimization

Even a mid level Laravel app has a number of files because Laravel has the habit of calling including multiple files for include requests. A simple trick is to declare all the files that would be included for include request and combine them in a single file. Thus, for all include requests, a single file will be called and loaded. For this, use the following command:

Composer Optimize Autoload

It is a good idea to use Composer scan the application and create one-to-one association of the classes and files in the application. Use the following command:

Limit Included Libraries

The good thing about Laravel is the huge number of libraries that could be included in a app. While this is a good thing, the downside is the high level of drag that the application experiences and the over all experience slows down.

This is why it is important to review all the libraries tata re called within the code. If you think you could do without a library, remove it from the config/app.php to speed up the Laravel app. Another important place to look is composer.json.

CloudwaysCTA

JIT Compiler

Translating PHP code to bytecode and then executing it is a resource intensive process. This is why go-between such as Zend Engine are required to execute the C subroutines. This process has to be repeated every time the app is executed. To reduce time, it is essential that this process is repeated just once. This is where the Just-in-Time (JIT) compilers prove to be very useful. For Laravel apps, the recommended JIT compiler is HHVM by Facebook.

Choose a Fast Cache and Session driver

For optimal Laravel performance tuning, the best route is to store the cache and session sections in the RAM. I believe the fastest cache and session drivers for Laravel 5 performance is Memcached.

The driver key for changing the session driver  is usually located in app/config/session.php. Likewise, the driver key for changing the cache driver  is located in app/config/cache.php

You might also like: Using Memcached With PHP

Cache Queries Results

Caching the results of the queries that are frequently run is a great way of improving Laravel 5.5 performance. For this, I recommend the remember function, that is used as follows:

Use “Eager Loading” for Data

Laravel offers a great ORM for dealing with databases. Known as Eloquent, it creates models that abstracts the database tables from the developers. Using simple structures, developers could use Eloquent to deal with all CRUD operations in PHP. When Eloquent uses eager loading, it retrieves all associated object models in response to the initial query. This adds to the response of the application. Let’s compare lazy loading and eager loading:

The lazy loading query will look like:

In contrast, eager loading query will look like:

Precompile Assets

For Laravel application tuning, developers often distribute code into separate files. While this keeps the code clean and manageable, it is does not contribute to efficient production. To help developers in this context, Laravel provides a simple command:

Use CDN for Delivering Static Assets

CloudwaysCDN

Loading static assets files from CDN server (as opposed to loading it directly from the server that hosts the files) will improve Laravel application performance.

Once a client visits a site, some portion of the information is served from the closest CloudwaysCDN area. This outcome is fundamentally quick page stack speed and an incredible affair for the client. CloudwaysCDN is a benefit based CDN. This implies you have to characterize the static resources (JS, CSS, HTML, pictures, recordings, and liveliness, and so forth.) on a specific application.

General Performance Tuning Tips for Laravel

Laravel is often used to create websites and web portals. In many cases, optimizing the performance of the website is just a matter of implementing several tweaks, such as:

  1. Laravel page speed composer package
  2. Update provider details
  3. Publish the package
  4. Register your Middleware
  5. Make route over page
  6. Create Blade File
  7. Run app

Laravel page speed composer package

Download and extract renatomarinho/laravel-page-speed package using Composer. Simply add the package name with version details in a composer.json file and run a composer update command:

Next, run the following command:

Update Provider Details

After successfully extract the package, Go to config/app.php file and append service provider and alias details with specific classes.

Publish the package

After adding provider details we need to publish the particular package to implement in our application. Following command help us to publish the package after executing the command only we can use the package

Adding middleware for web access

After publishing the package we need to add middleware details in Kernel.php file. Just copy and paste it following codes under $middlewareGroups,

View page details

In final, create blade file and include set of our code to display.

How Our Custom Stack Boost Performance

Cloudways stack

Known as ThunderStack, our custom stack is designed to boost performance of Laravel apps. The winning recipe comprises of Apache and NGINX, Varnish and Memcached. On the database side, you have the choice of either MySQL or MariaDB. In addition, you could enable Redis as session driver or addition cache.

You might also like: Laravel 5 Performance Benchmarks For PHP 7.0, 7.1 And 7.2 on Cloudways

Conclusion

Laravel performance tuning and optimization is an essential concerns for all businesses that rely upon Laravel powered tools for business processes. By implementing the above mentioned tips, Laravel developers who create and maintain business applications could ensure speed and reliability for the business.

Share your opinion in the comment section. COMMENT NOW

Pardeep Kumar

Pardeep is a PHP Community Manager at Cloudways - A Managed PHP Hosting Platform. He love to work on Open source platform , Frameworks and working on new ideas. You can email him at pardeep.kumar@cloudways.com

Create Laravel apps without the worry of server management.

Deploy your app on optimized PHP hosting servers for Laravel.

Get Our Newsletter
Be the first to get the latest updates and tutorials.

THERE’S MORE TO READ.