Lumen vs Laravel performance in 2018

I am currently rewriting my side project (Eyewitness.io) to create a free version for all Laravel developers to use (more info on that will be released in the next couple of weeks).

To handle the thousands of connections per minute from hundreds of servers that are monitored, I currently use Lumen to power the API, while using Laravel to power the frontend website.

I’ve previously written about possibly combining Laravel and Lumen into one project (you can read about it here). But I’m not convinced that is the right answer. I still find myself fighting Lumen for some simple things I want to do.

Jason McCreary (creator of Laravel Shift) wrote an article last year titled “Lumen is dead. Long Live Lumen”, which I resonate with some of the points he mentioned.

Note — this is not criticism of Lumen; it is an excellent tool and has its place. But once your API grows, I find myself constantly reaching for features of Laravel that are simply not available to Lumen.

So that got me thinking; just how much faster is Lumen vs Laravel now in 2018? Why mention 2018? Because Lumen was introduced in early 2015, and many of the Lumen benchmarks I can find are from around that time.

However much has changed since 2015; Redis is faster. OPcache is standard. PHP7.2 is insanely fast compared to PHP5.6. The Laravel core itself is faster.

The other problem is many of the benchmarks I found are simple “hello world” demos, with poor out of the box configuration. So I wanted to create a slightly more realistic scenario:

  • Laravel has config:cache and route:cache while Lumen does not. So it makes sense to include this in the test using Redis.
  • Lumen should have facades and eloquent enabled — because I use them in all my projects to improve on development time (and I think this would apply to most people).
  • Realistically most applications will involve some form of database access — so we need to include that in our tests.
  • Laravel now has web routes with sessions, and api stateless routes. So we need to include both in the tests when comparing against Lumen (which is only stateless).
  • OPcache is standard now — so lets make sure that is running.
  • Most benchmarks I’ve found were run locally. I want to see how real websites perform on the internet.

One important point; the purpose of this test is simply to determine how much faster Lumen is compared to Laravel. I am not interested in the actual numbers themselves — I just want to know how they are relative to each other.

I created duplicate Laravel and Lumen applications, each with three routes;

  • a “hello world” static route with no database access.
  • a “show” route that gets a specific user and all their posts (5 records).
  • a “post/delete” route that creates a new user post, and then deletes it (to keep DB size the same during the tests).

I’ve created a gist demonstrating the exact steps to recreate my tests here, along with the GitHub repos for each: https://gist.github.com/laurencei/d46893f833d5ddd439eccff025a0b63a

I ran an Apache Benchmark test against each route for Laravel (web), Laravel (api) and Lumen. I then re-ran those tests for a total of 5 rounds (as I found the tests results fluctuate by as much as 10% each round) and then averaged the results.

Here is what I got running on the DigitalOcean 2GB server (as per my gist):

So Lumen definitely supports more requests per second. No real surprise there.

I also decided to run the sites through Blackfire.io and see what it showed:

So what does this mean? According to Blackfire.io Lumen is 290% faster than Laravel (web) for static pages, but we already knew that. The reason largely boils down to having sessions enabled in Laravel (web) and not in Lumen; which is evident when you compare Lumen is (only) 75% faster than Laravel (api).

What is most interesting is when you compare stateless Lumen database access to stateless Laravel database access on the internet itself, the speed benefit of Lumen is only about 25–30% faster than Laravel (api).

In other words — the moment your application starts to touch a database, you lose most of the speed benefit Lumen offers.

Further — if your application has large databases and more complex SQL queries — then it is likely the difference in performance will be even less, as the database becomes the bottleneck compared to the underlying framework. SQL queries taking more than just a few milliseconds to run will completely wipe out the speed benefit given by Lumen.

Now — some people might read this post and say “omg — 30% speed bonus, I’m switching to Lumen”. But my personal opinion is the opposite; it is simply not enough of a difference justify it for most projects — the reality is you’ll never notice the difference and neither will your API users. Remember; Lumen is stateless, so you’ll generally only be using this for an API and not for displaying a website to user.

I prefer the development speed offered by Laravel with the full set of tools, and all the packages in the ecosystem. Even just a few hours saved of development time offsets the cost of an extra server for a year.

Focusing on writing good clean testable code and looking for better optimisations (such as Vanish, Database Caching, Queues, Websockets etc) are likely to yield significantly better results in the long term. And having my API and website in one project will save considerable development time compared to developing them separately.

But that will not be true for everyone and each project/application will have different needs. You need to weigh up the pros and cons for yourself and run tests specific to your setup and scenario.

Chris Fidao recently released a great Scaling Laravel course to help you grow Laravel when your SaaS becomes the next Uber/Facebook, so you’ll always be able to scale Laravel if you do ever need it.

I would love people to try similar tests and report their findings. Or if you notice anything wrong with my test methodology please let me know. I’ve created a reddit post here where you can post your results or discuss further: https://www.reddit.com/r/laravel/comments/7nl003/lumen_vs_laravel_performance_in_2018/

Run a Laravel application? Eyewitness.io is a monitoring and application analytics solution built specifically for Laravel. Know how your Laravel applications are actually performing. Monitor uptime, queues, cron schedules, email, logs, security, SSL, DNS and much more. Super simple installation — be up and running in just 90 seconds…