https://laravelnews.s3.amazonaws.com/images/og-image—laravel.png
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
Introduction
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.
What is Laravel Octane?
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.
Octane Key Features
The key features of Octane include several features that allow it to present better performance than the traditional Laravel; given below are a few such features:
- It makes your application extremely fast and efficient
- It saves resources compared to conventional Laravel applications.
How does Laravel Octane work?
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.
Octane Open Swoole
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.
Octane Roadrunner
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.
Laravel with Apache vs Laravel Octane Benchmarks
According to the creator of Laravel, Taylor Otwell, after the introduction of Apache and Octane, there is almost no requirement to choose Lumen for new projects. As we know that Lumen is a Laravel-based micro-framework intentionally devoid of a few components, making it fast. Also, using the Laravel Octane as its server is a fast 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:
- Laravel with Octane
- Laravel with Apache Web Server
- Laravel with its in-built server.
In this benchmarking, we used the tool wrk with the given below configuration:
1wrk -tl -c50 URL
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.
Why Scale your Laravel Application with Octane?
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.
What to Look for Before Scaling your Laravel Application with Octane (Prerequisites)
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.
Other Necessary Factors
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:
Refactoring Implications
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.
Handling Code Changes
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.
Are there any other Options to Scale Laravel applications?
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:
Horizontal Scaling
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.
Load Balancing (with AWS or Without AWS)
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 and 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
1Time Taken by Request: 39.40 ms, Memory Usage: 169 MB
2Time Taken by Request: 40.20 ms, Memory Usage: 169 MB
3Time Taken by Request: 37.71 ms, Memory Usage: 169 MB
4Time Taken by Request: 42.16 ms, Memory Usage: 169 MB
5Time Taken by Request: 40.60 ms, Memory Usage: 169 MB
6Time Taken by Request: 45.75 ms, Memory Usage: 169 MB
After Vapor’s Octane integration
1Time Taken by Request: 6.78 ms, Memory Usage: 112 MB
2Time Taken by Request: 6.64 ms, Memory Usage: 112 MB
3Time Taken by Request: 6.67 ms, Memory Usage: 112 MB
4Time Taken by Request: 6.38 ms, Memory Usage: 112 MB
5Time Taken by Request: 6.75 ms, Memory Usage: 112 MB
6Time 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
1Time Taken by Request: 11.32 ms, Memory Usage: 165 MB
2Time Taken by Request: 11.35 ms, Memory Usage: 165 MB
3Time Taken by Request: 11.29 ms, Memory Usage: 165 MB
4Time Taken by Request: 11.29 ms, Memory Usage: 165 MB
5Time Taken by Request: 11.36 ms, Memory Usage: 165 MB
6Time Taken by Request: 11.43 ms, Memory Usage: 165 MB
After Vapor’s Octane integration
1Time Taken by Request: 4.89 ms, Memory Usage: 108 MB
2Time Taken by Request: 4.89 ms, Memory Usage: 108 MB
3Time Taken by Request: 4.83 ms, Memory Usage: 108 MB
4Time Taken by Request: 4.66 ms, Memory Usage: 108 MB
5Time Taken by Request: 4.79 ms, Memory Usage: 108 MB
6Time Taken by Request: 4.91 ms, Memory Usage: 108 MB
Thus, we can infer that Octane decreases the duration of requests as well as memory usage. Also, adding the AWS applies 1ms billing granularity on Lambda, so you need to pay comparatively less for your HTTP function. Hence, the combination of Laravel Octane Vapor is a great choice for making a Laravel Octane Vapor web application with scalability.
Conclusion
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 News