Speeding up Laravel in Simple Steps

speeding-up-laravel-in-simple-steps-2

Speeding up Laravel applications can be done fairly simple. This guide will only cover the absolute surface and won’t dig deep into specific applications.

Using Cookie for Sessions Instead of Files

Changing a config/environment variable can improve web-performance a bit. It’s very different depending on your site, but usually I’ve found that it gave a little boost. It’s quite fast to switch your session driver:

  1. Open up your .env file
  2. Find the SESSION_DRIVER
  3. Change it to cookie Like so:
SESSION_DRIVER=cookie

So, Why Does Using Cookies Instead of Files, Boost Performance?

Reading from cookies is /usually/ way faster than reading from a file.

Cookies and Assets

Another thing to think about, if you’re interested in shaving off every millisecond possible, is serving assets (images, js, styles,..) from a cookieless domain; hence serving your assets from another domain, where you’re not using cookies. Remember if you’re using Google Analytics, it may a cookie for subdomains as well.

Benchmarks

I’ll be using a local site, completely fresh Laravel instance (with a logged in user, reading from and writing to the session), and the following command to benchmark:

ab -n 1000 -c 25 "http://laravel-file-vs-cookie.dev/"

File

Concurrency Level:      25
Time taken for tests:   15.264 seconds
Complete requests:      1000
Requests per second:    65.51 [#/sec] (mean)

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.1      0       1
Processing:    80  377  45.5    366     537
Waiting:       80  377  45.5    366     537
Total:         81  377  45.4    366     537

Cookie

Concurrency Level:      25
Time taken for tests:   13.880 seconds
Complete requests:      1000
Requests per second:    72.05 [#/sec] (mean)

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.1      0       1
Processing:    77  343  26.7    342     477
Waiting:       77  343  26.7    342     477
Total:         78  343  26.7    342     478

Difference Of cause, as mentioned, results may vary. However, looking at the results above, we we’re able to server ~6.54 requests more per second.

Route and Config Cache

Another way to boost performance is using config and route caching. But please be aware, you can only cache routes if all routes point to a controller.

  1. Locate your project root in your terminal.
  2. Type in php artisan config:cache
  3. And php artisan route:cache

Benchmarks

I’ve setup another application with 10 resource controllers and did not touch anything else. I’ll be using a variation of the same command as before (pointing to one of the controllers.

ab -n 1000 -c 25 "http://laravel-route-config-cache.dev/posts"

Before

Concurrency Level:      25
Time taken for tests:   16.139 seconds
Complete requests:      1000
Requests per second:    61.96 [#/sec] (mean)

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.1      0       1
Processing:    79  399  52.1    390     581
Waiting:       79  399  52.1    390     581
Total:         80  399  52.1    390     581

After

Concurrency Level:      25
Time taken for tests:   11.931 seconds
Complete requests:      1000
Requests per second:    83.81 [#/sec] (mean)

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.1      0       1
Processing:    58  295  45.5    287     531
Waiting:       58  295  45.5    287     531
Total:         59  295  45.4    287     531

Difference Remember, results may vary a lot, however again, we see a difference. This time, we were able to handle ~21,85 more requests per second, which I’d say is worth it. The reason for the large difference, is mainly that routes are expensive, they’re heavy to generate, and if they’re not cached; will generate on each request. Remember, to change your config or routes afterwards, you’ll have to use these commands:

php artisan route:clear
php artisan config:clear

Artisan Optimize

You can also use php artisan optimize which will optimize your class loader and generally just optimize your application a bit. It likely won’t provide a huge boost, but its work trying. I won’t be showing a benchmark for this, as I could not see any real difference for a clean installation of Laravel. You should experiment with this, but its unlikely to provide a negative result.

Please note that in Laravel 5.5 optimize has been made redundant, and it will be removed in future versions.

Speeding up Laravel Even Further

I will be creating more posts regarding Laravel and performance, so stay tuned, however meanwhile you could checkout this video course: Performant Laravel, by Chris Fidao, which covers these tips, and much more.

Show Comments