Mode | Requests Handled in 10-Seconds | Requests Per Second (RPS) |
---|---|---|
Laravel with Octane | 2667 | 266 rps |
Laravel with Apache | 1210 | 121 rps |
Laravel with its in-built Server | 705 | 70 rps |
Quick Summary:
Laravel has gained a lot of attention in recent years, though it has been a charm in the eyes of product owners for small-scale web applications. However, developing a large-scale application with Laravel still seems like a hard nut to crack. In this blog post, we have covered the latest addition to the Laravel framework, Laravel Octane, and the buzz Octane to scale Laravel applications has created around the market to fill the scalability gap of Laravel experienced by product owners.
Laravel possesses a simple and decent syntax that allows developers to make their web application development easier. The only drawback which led Laravel to lack on every front is Scalability which is why Laravel is not among the top most preferred languages among developers. After the introduction of Octane, this perception shifted slightly in favor of Laravel and its community. But, before we move to our Topic of Octane to Laravel Applications. Let us first understand What Laravel Octane is and How does it work.
Laravel Octane is an open-source package introduced by Taylor Otwell in his Laracon Online Talk, with its release date on May 12, 2021. This package serves the Laravel application and uses the web server Swoole and RoadRunner to help improve performance. It is initialized with the first request, stored in memory, and does not reinitialize at subsequent requests. It possesses 3.3k Stars and 234 Forks on GitHub.
Laravel Octane offers several key features that help enhance performance, especially when running with Swoole. Below are a few such features and their overview for your consideration.
Laravel Octane optimizes request handling by pre-initializing multiple PHP workers, contrasting with the traditional approach of creating the workers per request. This setup allows the server to handle incoming requests more efficiently. You can configure the number of workers to start with and the number per CPU core. This configuration helps balance the load, ensuring that if one worker is occupied, others can continue processing new requests.
Refer to the command below to start with the specific workers:
php artisan octane:start --workers=4
For Swoole, use the Task Workers:
php artisan octane:start --workers=4 --task-workers=6
Adjust the worker settings cautiously, as optimal configurations depend on your application’s specific needs and use cases of your site.
With Laravel Octane, you can perform concurrent tasks, allowing multiple operations to run simultaneously. Task workers handle these concurrent tasks, freeing up web workers for other requests.
Refer to the command below to specify task workers:
php artisan octane:start --workers=4 --task-workers=6
This feature is only available for Swoole.
Laravel Octane supports periodic task execution after a few seconds using ticks, similar to JavaScript’s setInterval method. This allows operations to run repeatedly at specified intervals.
Refer to the command given below:
Octane::tick('simple-ticker', fn () => ray('Ticking...'))->seconds(10);
There is currently no command to stop these ticks once they have started. This feature is also only available with Swoole.
Laravel Octane provides an in-memory cache driver powered by Swoole tables, capable of handling up to 2 million operations per second. While this cache is high-speed, it’s important to remember that all cached data is lost upon server restart.
Use the below command to use the Octane cache:
Cache::store('octane')->put('framework', 'Laravel', 30);
You can also set cache intervals to refresh data periodically:
use Illuminate\Support\Str;
Cache::store('octane')->interval('random', function () {
return Str::random(10);
}, seconds: 5);
This feature is available exclusively with Swoole.
Swoole tables in Laravel Octane offer a high-performance, shared data storage solution accessible to all server workers. This significantly speeds up data retrieval and operations. However, like the Octane cache, data in these tables is lost when the server restarts.
Refer to the example configuration in octane.php:
'tables' => [
'example:1000' => [
'name' => 'string:1000',
'votes' => 'int',
],
],
The code for accessing and manipulating the table goes through the Octane::table method:
use Laravel\Octane\Facades\Octane;
Octane::table('example')->set('uuid', [
'name' => 'Nuno Maduro',
'votes' => 1000,
]);
return Octane::table('example')->get('uuid');
Swoole tables support string, int, and float column types and are available only with Swoole.
These features collectively enhance Laravel Octane’s efficiency and performance, making it a robust choice for high-demand applications.
Octane Laravel is built on top of the traditional Laravel and PHP version 8.0 and above. It is not a separate language but is an addition to the standard Laravel. Octane works on its servers via Swoole and RoadRunner, which the traditional Apache and Nginx should redirect for incoming traffic.
It is a software development kit that allows product owners to create efficient and scalable web applications. Open Swoole is a fork of the Swoole framework initiated in late 2021 concerning the internal conflicts within the developer community of swoole.
It is an open-source, high-performance, serviceable PHP application server that is effective and efficient in replacing the traditional Nginx setups delivering better performance and flexibility.
We can also mention that though Swoole and RoadRunner still use worker processes for all requests, they only serve the first request to boot the framework (dependency containers), and the remaining ones are from the framework’s bootstrapped version. This collectively makes your application perform impressively fast from your existing application.
Octane Laravel catches the bootstrapped Laravel framework in RAM with the first request. After that, all the requests use the already bootstrapped framework from RAM instead of doing the whole reading of files from the hard disk and re-bootstrapping the framework, making the Laravel application faster and improving its performance to a significant extent.
According to Taylor Otwell, the creator of Laravel, after the introduction of Apache and Octane, there is almost no requirement to choose Lumen for new projects. As we know, Lumen is a Laravel-based micro-framework intentionally devoid of a few components, making it fast. Also, the Laravel Octane performance server is a faster and better option than Lumen. To understand that better, let us look at a few benchmarks.
In this benchmark, we took in a total of three modes Laravel Octane vs Laravel Apache Web Server vs Laravel built-in server:
In this benchmarking, we used the tool wrk with the given below configuration:
The first attest is performed on the Just Hello World HTML, and the results that we have received are as follows:
Mode | Requests Handled in 10-Seconds | Requests Per Second (RPS) |
---|---|---|
Laravel with Octane | 2667 | 266 rps |
Laravel with Apache | 1210 | 121 rps |
Laravel with its in-built Server | 705 | 70 rps |
These, Laravel Octane benchmark metrics are enough to give you insight that Octane Laravel is far faster in terms of performance than the traditional Apache-Server Laravel.
Octane catches your data and objects in the memory, which contributes to the improvement in the performance of your Laravel application by upto 30%. This improvement in the performance is observed due to the eliminating of disk reads, process forks, and various other overheads that come to function whensoever the framework is booted up on every request, which allows the Laravel application to manage thousands of connections quickly, thereby contributing to the increase in traffic or the need to be highly available. Also, this is a productive method that too with a little expense.
Additionally, a minor or no change in the codebase is required under certain circumstances. But it is related to how your application is structured.
Develop high-performance Laravel applications with Bacancy!
Hire Laravel Developer and witness the building of your dream product with us. We have dedicated developers with extraordinary problem skills.
Octane can improve your web application development performance to a great extent, but that doesn’t mean that it is sufficiently capable of improving the performance of your web application and providing you with the freedom to scale.
It involves prerequisites like PHP version 8.0 and adopting the stateful programming convention. Therefore, before you finalize using Octane, the first and foremost step is to update your PHP version and install it. Though it seems a simple task, the severe hiccup with this is that numerous websites that work on Laravel are using an older version of Laravel, which does not support the Octane and hence needs to be updated with the latest Laravel version.
Also, many product owners, whether experienced or fresher, are not familiar with the stateful framework, which again requires efforts to understand it before they finalize to implement Laravel Octane within your web application to scale.
In addition to the above-given prerequisites, a few other factors must be considered to make a scalable web application. Let s look at those factors given below:
For applications using session state or a few middleware types, some prior changes to the code are required for using the Octane. Also, you should add support for Swoole coroutine-based sessions and remove middlewares that aren’t compatible with Swoole.
The rest of the changes that need to be made involve the Dependency Injection Container. You can refer to the Laravel Official Document of Laravel for more details on How to do it.
The traditional Laravel requires you to start or stop the entire Laravel application instance whensoever a change is made. However, contrary to this, with Octane, you get a convenient way to monitor your codebase automatically and restart your Octane server without any manual interventions, which allows you to make the developments easily by altering the code at a single instance or in one place instead of restarting the whole application insta\nce every time the codebase is updated.
Yes, there is always another solution to a problem. Though Laravel Octane is a great tool to resolve the issue of scaling within the traditional Laravel application, it is still not the only option; plenty of other options can contribute to your scaling needs. Let us have a look at a few of them:
The first comes horizontal scaling, a simple yet least recommended method to scale your Laravel application. The reason behind this is that though it is efficient, it becomes more and more complex and expensive as the running costs keep increasing and more difficult as you migrate to a different server. Further, it is still a limited solution as you cannot scale on demand with this setup.
For scaling your Laravel Application on AWS, you can automatically scale the applications to a limited extent for applications with the same MySQL database. These servers thereafter use a single Amazon RDS or Amazon Relational Database.
Though this seems a viable option to scale compared to Horizontal scaling, the hidden costs involved here are not transparent. Also, it is less effective than Octane Laravel. Still, the Significant benefit is that it allows you to scale without any alteration needed in your existing codebase, as with Laravel Octane.
Laravel Vapor is an auto-scaling, serverless deployment platform for Laravel. Vapor allows for scalability and simplicity of the serverless. When combined with Octane, the Laravel Octane Vapor combination provides autoscaling and faster load times at any scale. However, to understand it, even more, let us take an example from the official Laravel Documentation.
Vapor project on the us-west-1 region of Amazon. This Vapor application is configured with 1 GB of RAM and an RDS MySQL instance (db.t2.micro) with 1 VCPU and 1Gib RAM.
Now, looking at the API endpoint that gets a user from the database. With Octane, its endpoint becomes 7x faster. It uses 44% less memory:
# Before Vapor's Octane integration Time Taken by Request: 39.40 ms, Memory Usage: 169 MB Time Taken by Request: 40.20 ms, Memory Usage: 169 MB Time Taken by Request: 37.71 ms, Memory Usage: 169 MB Time Taken by Request: 42.16 ms, Memory Usage: 169 MB Time Taken by Request: 40.60 ms, Memory Usage: 169 MB Time Taken by Request: 45.75 ms, Memory Usage: 169 MB # After Vapor's Octane integration Time Taken by Request: 6.78 ms, Memory Usage: 112 MB Time Taken by Request: 6.64 ms, Memory Usage: 112 MB Time Taken by Request: 6.67 ms, Memory Usage: 112 MB Time Taken by Request: 6.38 ms, Memory Usage: 112 MB Time Taken by Request: 6.75 ms, Memory Usage: 112 MB Time Taken by Request: 6.47 ms, Memory Usage: 112 MB
While the “login” route which renders a static template. Octane at this endpoint has made it almost 3x faster. It uses 35% less memory:
# Before Vapor's Octane integration Time Taken by Request: 11.32 ms, Memory Usage: 165 MB Time Taken by Request: 11.35 ms, Memory Usage: 165 MB Time Taken by Request: 11.29 ms, Memory Usage: 165 MB Time Taken by Request: 11.29 ms, Memory Usage: 165 MB Time Taken by Request: 11.36 ms, Memory Usage: 165 MB Time Taken by Request: 11.43 ms, Memory Usage: 165 MB # After Vapor's Octane integration Time Taken by Request: 4.89 ms, Memory Usage: 108 MB Time Taken by Request: 4.89 ms, Memory Usage: 108 MB Time Taken by Request: 4.83 ms, Memory Usage: 108 MB Time Taken by Request: 4.66 ms, Memory Usage: 108 MB Time Taken by Request: 4.79 ms, Memory Usage: 108 MB Time Taken by Request: 4.91 ms, Memory Usage: 108 MB
Thus, this Laravel Octane tutorial can infer that Octane decreases the duration of requests and memory usage. Also, adding AWS applies 1ms billing granularity on Lambda, so you need to pay comparatively less for your HTTP function. Hence, combining Laravel Octane Vapor is a great choice for making a scalable Laravel Octane Vapor web application.
This is what we have on scaling your Laravel application using Laravel Octane. Though it is a decent option to scale your existing Laravel application, it shows significant improvement in terms of performance in comparison to the traditional Laravel application. Still, its incompatibility with the existing codebase of many traditional Laravel applications is a setback. It can be a good choice if you wish to improve the performance of your application, but if you plan on saving an already struggling application, then Octane Laravel is not the best option. You must do thorough research before making the final call.
Additionally, when you have opted for Octane, make sure to use the Octane Compatible packages you add to your project to get the ideal application compatible with Octane if you are still hesitant about making the final call or scaling your Laravel application. A Laravel Development Company like Bacancy can assist you in the same; our expert professionals are always available to guide and walk alongside you through your entire web application development process.
Laravel Octane is significantly fast and can handle up to 2 million requests per second.
Yes, Laravel is Scalable, but if you are planning for huge enterprise-grade applications, then going with some other framework would be a better choice.
Yes, Laravel Octane is generally stable and suitable for production use. Many organizations have successfully used it to improve performance and scalability. However, it is essential to thoroughly test it in a staging or development environment to ensure compatibility with your specific application and dependencies before deploying it in production.
The resource requirements for running Laravel Octane depend on various factors, including the complexity of your application, the number of concurrent requests it needs to handle, and the efficiency of your code. Octane is memory-efficient, but scaling your application will require more CPU and RAM server resources to manage the increased workload. Proper monitoring and resource allocation are crucial for smooth operation, which otherwise can adversely affect the application.
Octane significantly boosts performance by employing long-lived, coroutine-based processes to handle multiple requests simultaneously. This minimizes the overhead of repeatedly initializing PHP processes, resulting in lower latency and faster response times. Moreover, Octane supports preloading frequently used components, eliminating the need to reload them for each request and further enhancing performance.
Your Success Is Guaranteed !
We accelerate the release of digital product and guaranteed their success
We Use Slack, Jira & GitHub for Accurate Deployment and Effective Communication.