Chris White Web Developer

Swoole vs Roadrunner for Laravel Octane

14 November 2021 ~4 minute read

Laravel Octane is a package from the Laravel team that can massively increase your application's performance. In a normal PHP application, each HTTP request starts from a completely blank slate. The server will spawn a new PHP process, boot up the framework components and your application code, process the HTTP request, and once the response has been sent to the user it will end the process. Rinse and repeat for every request. This may sound like a huge waste of resources, but it's actually one of the more compelling reasons to use PHP: you don't have to care about state between requests which simplifies development. Laravel Octane introduces a very different paradigm. It will boot the framework and your application code once and keep it in memory to re-use between multiple HTTP requests. This comes with large performance improvements at the detriment of the ease of development. Now you need to think about what state is maintained between requests. It's a tradeoff.

Swoole or Roadrunner?

When you use Laravel Octane you need to decide if you're going to use the Swoole or Roadrunner web server to serve your application. For somebody who has never been down this path the decision can be overwhelming - after all, there are pros and cons to both and you don't want to leave useful features on the table by choosing the wrong web server. Below is a comparison between the two based on my own research.

Swoole benefits

Swoole is actually a PHP extension that adds a lot of features to the language that can be used to increase your application's performance. Due to this, it is considered a lot more featured than Roadrunner. Some of these features include:

  • Concurrent tasks, which allow you to run multiple functions truly asynchronously.
  • Ticks, which allow you to run a function at a specified interval.
  • Octane caching, which adds a ridiculously high performance in-memory cache to your Laravel application.
  • Swoole tables, which is an in-memory data store that is shared between all Swoole processes.

Swoole is also brings higher performance than Roadrunner, as can be seen in this GitHub repository.

Swoole drawbacks

Since Swoole is a PHP extension, you need to specifically compile it into the language during your build step. Depending on how you build and deploy your application this may introduce complexity for you or require you to learn how to build your own PHP installation. Thankfully, a lot of the hard work has been taken care of already. Laravel Sail has Swoole built in to the Docker image for local development, and there are resources online showing you how to get it working in services like Laravel Forge.

Swoole also does not work with Xdebug, the most popular debugging tool in the PHP ecosystem. You can read more about this in the GitHub issue. There was an effort to create a new debugging tool that specifically works with Swoole named yasd, however it is not nearly as mature as Xdebug.

Finally, there are a lot of reports of profiling and monitoring tools such as Datadog and New Relic not working reliably with Swoole due to its use of coroutines and technically running in a CLI environment. This is a departure away from how PHP applications are traditionally deployed, and it seems a lot of these tools have not yet caught up. Here is a good example from the Datadog repository.

Sorry to interrupt...

If you're enjoying reading this post, please consider following me on Twitter.

Roadrunner benefits

Roadrunner, unlike Swoole, is not a PHP extension but simply a replacement for PHP-FPM. This means it does not need to be compiled into the language itself, making it a lot easier for most people to deploy. In most cases you can simply swap your usage of PHP-FPM directly with Roadrunner and immediately see large performance improvements.

Also unlike Swoole, Roadrunner is reported to work out-of-the-box with tools such as Xdebug. In fact, there is an official documentation page on how to configure your IDE. Profiling and monitoring tools such as Datadog and New Relic, which generally don't have good support for Swoole, are also reported to be working just fine under applications served with Roadrunner. If you need to monitor your application's health and performance metrics, this may seal the deal for you provided Roadrunner is giving you the performance your application needs.

EDIT: as Peter Fox pointed out in the comments, Roadrunner also supports plugins to extend its functionality. A lot of them are already available and maintained by the people who created Roadrunner, but you can quite easily write your own as well.

Roadrunner drawbacks

There are really only two major drawbacks to using Roadrunner instead of Swoole: it does not add features to the PHP language that would prove useful in an application that demands high performance, and it is not able to serve as high of a request rate as Swoole. Whether you absolutely need that extra bit of performance for your application that Swoole offers is up to you - you must trade it off with the lack of health and performance monitoring, and lack of Xdebug support.

Personally, since I consider health and performance metrics as vital parts of running an application in production, and Roadrunner does give a substantial performance upgrade from PHP-FPM, Roadrunner would be my choice if I was using Laravel Octane.

Made with Jigsaw and Torchlight. Hosted on Netlify.