Some case study about Laravel Octane experience on building API.

Background
Each client has their own expectations and requirements regarding the performance of their system, making it critical for the team to deliver APIs that are highly responsive and performant. We recognizes that the performance of the system is of utmost importance to customers as it directly impacts the user experience. When APIs are slow and unresponsive, it can lead to frustration and dissatisfaction among users. Therefore, the team is motivated to ensure that their APIs are as responsive as possible to meet customer expectations.
However, we has observed that as APIs become more complex which experiencing slower response times even when the database is already optimized and impacting the performance of the API. To overcome these challenges,we may need to take a more holistic approach to API development and consider factors beyond just database optimization. By taking a comprehensive approach to API development, we can ensure that their APIs are highly performant and meet the expectations of their customers.
Challenge:
The challenge we have even though we had already implemented various optimization techniques such as caching, database optimization and code optimization is performance and response time of the API still have not met the expectation. We think that the response time should be and must be lower than what we have. Hence, we knew we needed a more advanced solution to meet API performance requirements.
Solution
Here is where Laravel Octane comes very handy. We decided to implement Laravel Octane which is a high-performance application server for Laravel. Laravel Octane works as memory optimization by preloading the whole application bootstrap and dependencies into memory to reduce application startup time.
Laravel Octane supercharges your application’s performance by serving your application using high-powered application servers, including Open Swoole, Swoole, and RoadRunner. Octane boots your application once, keeps it in memory, and then feeds it requests at supersonic speeds.
We choose Swoole as our application server. Swoole is a PHP extension, which provides us cool features such as coroutines, fibers, web-sockets, caching, and etc. Installation of the extension is needed to make it work. There are many features in Octane but we only utilize concurrent task because it use many cases in our application.
We started to restructure the API using the Octane ecosystem. Installation is quite simple and easy to use at the code level. For the infrastructure, it requires extra setup. It is advised to use an Octane application behind a traditional web server such as a Nginx or Apache because it allows the web server to serve the static assets such as images and stylesheets as well as manage SSL certificate termination.
After structuring, we conducted a simple test to measure the application’s performance using Postman. We observed a significant improvement in performance especially for the API that required database queries. We also noticed that the application was more responsive and the server was able to handle more concurrent requests faster.
Notes
When preparing an existing application for Octane or starting a new one, there are certain factors to be mindful of. One key consideration is that Octane stores the framework in memory across all workers, which means that service providers for all of your applications will only be registered and booted once.
Example:
Here we want to show the some code restructure we have made:-
$result1 = app(ITAppClient::class)->getTotalComplaintBillNotReceived($params);
$result2 = app(ITAppClient::class)->getTotalComplaintChangeOfRouteType($params);Above example shows we want to get 2 different queries and store it in some variables. It shows that in order to have $result2, we need to wait $result1 to finish first. The recorded finish response time is about 800ms to 900ms.
Here is where octane came handy. Octane have ability to make concurrent task thats retrieve all the result simultaneously. Below example are after we implemented it.
[$result1, $result2] = \Octane::concurrently([
fn () => app(ITAppClient::class)->getTotalComplaintBillNotReceived($params),
fn () => app(ITAppClient::class)->getTotalComplaintChangeOfRouteType($params),
]);After restructure the code, the recorded finish response time we got is 90ms to 150ms. The varies of the time we got is depending on how fast the database / integration response.
Result
The implementation of Octane resulted in a significant improvement in the application’s response time, reducing it by approximately 60%. This means that the application is now able to handle a much higher volume of requests in a shorter amount of time, which can greatly improve the user experience.
In addition to the reduced response time, the implementation of Octane also increased the server’s capacity to handle concurrent requests by a factor of 10. This means that the server can now handle a much higher volume of requests simultaneously which is especially important for applications with high traffic volumes.
The positive impact of the implementation of Octane was significant enough that the team decided to make it a permanent part of their application stack for building APIs. This suggests that the team found the benefits of Octane to be substantial and worth continuing to use over the long term. Overall, the results of implementing Octane seem to have been highly beneficial for the team and the performance of their application.

Pros and Cons
Every architecture have some pros and cons in any way. Here are some of the pros and cons of using Laravel Octane for building APIs:
Pros:
- Faster response times and improved API performance
- The ability to handle more requests with less overhead
- Pre-warming the Laravel application in memory which eliminates the time it takes to bootstrap the application for every request
- The ability to run the application in a multi-threaded environment which allows the application to handle multiple requests simultaneously
- Easier horizontal scaling of the application
Cons:
- Requires additional setup and configuration compared to Laravel
- Limited support for some Laravel features
- Some third-party packages may not be compatible with Octane
- Swoole does not work with Xdebug
- Memory leaks if not carefully used
Conclusion:
Laravel Octane is a better choice than Laravel for building high-traffic APIs. It provides faster response times and improved API performance during peak traffic hours along with the ability to handle more requests with less overhead. Although it requires additional setup and configuration compared to Laravel, the benefits of using Octane for building APIs outweigh the cons. By migrating to Laravel Octane, we were able to improve its API performance which led to an increase in user satisfaction.
References
Found this helpful?
If this article saved you time or solved a problem, consider supporting — it helps keep the writing going.
Originally published on Medium.
View on Medium