Speed Up Laravel on Top of Swoole


May 4th, 2018


Swoole is a production-grade async programming framework for PHP. It is a PHP extension written in pure C language, which enables PHP developers to write high-performance, scalable, concurrent TCP, UDP, Unix socket, HTTP, WebSocket services in PHP without too much knowledge of the non-blocking I/O programming and low-level Linux kernel. You can think of Swoole as something like NodeJS but for PHP, with higher performance.

Why run Laravel on Swoole?

The image below illustrates the lifecycle in PHP. As you can see, when you run php script every time, PHP needs to initialize modules and launch Zend Engine for your running environment. And your PHP script needs to be compiled to OpCodes for Zend Engine’s execution.

However, this lifecycle needs to go over and over again in each request. Because the environment created for a single request will be immediately destroyed after the request process is done.

In other words, in traditional PHP lifecycle, it wastes a bunch of time building and destroying resources for your script execution. And imagine in frameworks like Laravel, how many files does it need to load for one request? There’s a lot of I/O consumption for loading files as well.

PHP Lifecycle

So what if we have a built-in server on top of Swoole, and all the scripts can be kept in memory after the first load? This is why we try to run Laravel on Swoole. Swoole can be a powerful performance booster and Laravel provides the elegant structure and code usages. That’s a perfect combination!


Here are the main features of swooletw/laravel-swoole:

  • Run Laravel/Lumen application on top of Swoole.
  • Outstanding performance boosting up to 30x.
  • Sandbox mode to isolate app container.
  • Support running WebSocket server in Laravel.
  • Support Socket.io protocol.
  • Support Swoole table for cross-process data sharing.

Require this package with Composer:

1$ composer require swooletw/laravel-swoole

This package relies on Swoole extension. Make sure you’ve installed Swoole before you use this package. Using this command to install it quickly:

1pecl install swoole

After installing the extension, you will need to edit php.ini and add an extension=swoole.so line before you use it.

1php -i | grep php.ini # check the php.ini file location
2sudo echo "extension=swoole.so" >> php.ini # add the extension=swoole.so to the end of php.ini
3php -m | grep swoole # check if the swoole extension has been enabled

Visit the official website for more information.

Notice: Swoole currently only supports Linux and OSX. Windows servers are not able to use Swoole yet.

Then, add the service provider:

If you are using Laravel, add the service provider to the provider’s array in config/app.php:

2 'providers' => [
3 SwooleTW\Http\LaravelServiceProvider::class,
4 ],

If you are using Lumen, append the following code to bootstrap/app.php:


It supports package auto discovery. If you’re running Laravel 5.5, you can skip this step.

Up and Running

Now, you can run the following command to start Swoole HTTP server.

1$ php artisan swoole:http start

Then you can see the following message:

1Starting swoole http server...
2Swoole http server started: <>

Now you can access your Laravel application on


Test with clean Lumen 5.5, using MacBook Air 13, 2015.
Benchmarking Tool: wrk

1wrk -t4 -c100 http://your.app

Nginx with FPM

1Running 10s test @ http://lumen.app:9999
2 4 threads and 100 connections
3 Thread Stats Avg Stdev Max +/- Stdev
4 Latency 1.14s 191.03ms 1.40s 90.31%
5 Req/Sec 22.65 10.65 50.00 65.31%
6 815 requests in 10.07s, 223.65KB read
7Requests/sec: 80.93
8Transfer/sec: 22.21KB

Swoole HTTP Server

1Running 10s test @
2 4 threads and 100 connections
3 Thread Stats Avg Stdev Max +/- Stdev
4 Latency 11.58ms 4.74ms 68.73ms 81.63%
5 Req/Sec 2.19k 357.43 2.90k 69.50%
6 87879 requests in 10.08s, 15.67MB read
7Requests/sec: 8717.00
8Transfer/sec: 1.55MB

Learn More

Check out the official package at Github Repo and Official Docs for more information.

Filed in:

Albert Chen

I am a full-time developer focusing on back-end development in Taiwan. Currently I'm using Laravel or Lumen as my major frameworks in most of my projects, also trying hard to develop more useful packages for Laravel.

My website: https://albert-chen.com