Speed Up Your Laravel Projects Using Laravel Octane & Swoole Server

Cerwyn Cahyono
Geek Culture
Published in
5 min readMay 14, 2021

--

Photo by Jake Givens on Unsplash

As we already know, the Laravel framework gives us abundant features. It also has a lot of packages that we can use to build our next great apps. But, here’s a question for yourself. Do you know how’s the lifecycle of Laravel works? This thing is important to us, as a developer, because if we understand how it works, we can be more understand what’s in here and what’s missing, and maybe we can extend the functionalities further.

Laravel lifecycle is pretty complicated to explain in this short article but, let me explain in this way. Every request that comes into our app, will start to execute the public/index.php as the starting point. From this index file, the request will go through to the HTTP/Console Kernels, Service Providers, Routing, and finally send back a response to the user. You can see the detailed lifecycle in the documentation here. In the steps above, Laravel will execute, load, and compile some scripts. Later, when the response is being sent back, all of the instances will be destroyed.

PHP is an interpreted language. This means, when a PHP script runs, the interpreter parses, compiles, and executes the code over and over again on each request. This may result in wasting CPU resources and additional time. https://kinsta.com/blog/php-8/

Fortunately, Swoole comes to help. And recently, Laravel published a new official package called Laravel Octane that uses Swoole as one of the application servers. What Swoole does is storing the compiled scripts/containers into the memory, to be reused later and ready to accept the next requests. With that features, your app doesn’t need to recompile the scripts again and again.

Before we go on, what is Swoole?

High Performance Programmatic Server for PHP with Async IO, Coroutines and Fibers. https://www.swoole.co.uk

Swoole is a package for PHP built with C where it offers a lot of features such as Coroutines, web sockets, and many more. With the coroutines, we can build PHP asynchronously so it’ll be…

--

--

Cerwyn Cahyono
Geek Culture

PHP/Backend Engineer at Undercurrent Capital Pte Ltd — Data Science Enthusiast