April 202408:3480.3k views

Laravel Octane: supercharge your Laravel applications

About

In this video I give you an overview of Laravel Octane, a package that can supercharge your Laravel applications. Subscribe for more videos about the Laravel ecosystem in the coming weeks and months!

Transcript

Everything I said

00:00

Welcome to the What is Laravel series. My name is Aaron Francis. In this series of many, many videos, I'm gonna be covering the entire Laravel ecosystem. Today, we're gonna be talking about Laravel Octane. Octane is a first-party installable composer package from the Laravel team that supercharges your application and makes it a lot faster.

00:28

But before I explain how it makes it faster, I need to tell you a little bit about how PHP works in general. Typically, typically, you'll have Nginx sitting in front of a pool of PHP FPM workers. When a request comes in that is not a request for a static asset, Nginx hands that request off to PHP FPM.

00:50

PHP FPM then boots up your entire application, handles the request, and then tears down your entire application. There are two things you need to know about this model. The first is every PHP FPM process can handle one request at a time. Usually, you have a pool of many processes, but they're each handling one request at a time.

01:10

That's gonna be important. The second is this is a shared nothing approach. So every time a request is handled, the entire universe has to be built from scratch, your request is handled, and then the entire universe has to be destroyed. So every request that comes in, you're booting your application from nothing. That's how it's always been.

01:31

That's how PHP has always worked until Laravel Octane. Let's talk about Laravel Octane. Octane was first announced at Laracon Online in twenty twenty-one, and the first version was tagged in May of twenty twenty-one. It is a composer package that you install into your application. It is maintained by the Laravel team, and it is fundamentally different than using PHP FPM.

01:55

When you install Octane, which we'll do here in a second, when you install Octane, it asks you which driver you want to use. The choice of driver, you have four choices. You have Swoole, OpenSwoole, FrankenPHP, and Roadrunner. Those are the four drivers that you can choose with Octane. Each different driver has its own pros and cons.

02:16

I'll leave that as an exercise for the viewer, which is you, because that's a little bit too much detail for this video. But here's the deal. When you install Octane, you pick your driver, and then instead of Nginx handing off requests to PHP FPM, Nginx hands off the requests to Octane. So what is the benefit here? Two benefits, right? So remember, with PHP FPM, each worker or each process can handle a single request, and it's shared nothing.

02:44

With Octane, a worker or a process can handle multiple requests, maybe, and it's shared something. So let's start with shared something. In Octane, Laravel is going to boot the application framework included, and then keep that in memory and serve requests to an application that has already been booted.

03:06

So you get back all of that boot time. You're not, you're not creating the universe from scratch anymore. The universe has been created, and now it's just receiving requests. Now, the second benefit relates to that one request per worker thing that PHP FPM has, right? So with PHP FPM, it's one request per worker. With Octane, depending on the driver that you choose, you can process multiple requests at the same time with a single worker.

03:30

The way that that works is if you have a long blocking IO operation in one request, it'll hop to the next one, start working on that, and then come back to the original request. So you're gonna get much higher throughput if you're using Octane. Some of those details are driver-specific, but the general idea stands. Now, I've got a standard Laravel application here that I just created from scratch. The only thing I've done is replace the default route with a Hello World.

03:53

So if we hit that in the browser at octane.test, you see there is our Hello World. Easy enough so far. Let's go ahead and install Laravel Octane. You just say composer require laravel/octane. It's gonna do its thing. We'll clear that out, and then we wanna run one command, which is octane install. And this is going to ask us for which driver we want to use.

04:15

I'm just gonna pick Swoole here. That's it. We're good to go. Now, you can still hit this in the browser, but this is not running through Octane, so we need to start the Octane server. So we'll say php artisan octane start. And now this server is running at this address. So if we run it over here, you'll see we're getting the same Hello World, but you see that the requests are streaming in here.

04:42

So now we're running this on both places. This is running locally through Herd, which is Nginx to PHP FPM, and this is running through Octane. All right, we're gonna put both of these systems under load, the Octane and the non-Octane, and we're gonna see which one fails first. I think it's gonna be the non-Octane version, but this is not a benchmark. This is just a comparison.

05:03

There are a bunch of commands you can run to make Laravel production-ready. I haven't run any of those because I'm not benchmarking. I'm just comparing. That being said, let's take a look in the browser first before we run these tests. So if we load octane.test, which is the non-Octane version, and we look at the timeline down here, you see that forty-two milliseconds is spent booting Laravel the framework, right?

05:29

So if we refresh this a couple times, you'll see it bounce around, but it's between eighteen and forty milliseconds spent booting. If we check the Octane version, if we check the Octane version over here, you'll see that the booting has dropped down to five, three, seven. So this is what I'm talking about when I say it boots the application once and keeps it in memory.

05:51

So we've already gained back between twenty and thirty to forty milliseconds already. Now, let's run a few fake little load tests here. I'm gonna use the work command, and we're gonna run four threads with a hundred and fifty connections for five seconds against the Octane version, and we're gonna see how well this does.

06:14

So after five seconds, we should see... All right, we got- 245 requests per second. Now, if we run that again, if we run that again against octane.test, I think what we're going to see is a bunch of failures. So we see a huge amount of requests per second, but that's because most of them just failed.

06:37

So you can already tell, even on this tiny little application, that Octane is boosting the performance. Now, this is just the tip of what Octane has to offer. Depending on which driver you choose, you can have stuff like concurrent tasks. You can use it as a cache driver and get up to 2 million operations a second as a cache driver, which is pretty insane.

06:59

And so I think the question is going to be, why would you not use Octane? And the answer is you might not need it. If you're operating just fine without Octane right now, you don't need it. When you install and start using Octane, you do have to think about your application a little bit differently. You have to be more careful with static variables because it is going to boot your application and keep it in memory, so you could introduce a memory leak if you're not careful.

07:25

So if you're installing this in a legacy application, you're gonna have to kind of do a lot of work to go back and make sure that you're able to use your application in this new Octane paradigm. If you're running your application on Laravel Forge, Octane is super easy to configure over there. Regardless of where you're running your application, you do wanna restart all of those Octane workers at the end of your deploy script so that your new application does get loaded into memory.

07:53

Octane has a few commands that you can run to do that. If you're running your application on Laravel Vapor, there's actually an Octane integration there as well, which I helped discover. I didn't write it, but I did help discover the idea of it. But it doesn't use any of these drivers. It uses, um, an architectural quirk of how AWS Lambda works, but it still keeps your application booted and in memory.

08:17

That's it for Octane, a free and open source library maintained by the Laravel team that boosts your application performance. Stay tuned to this channel. I'm gonna be covering everything there is to know about the Laravel ecosystem and much more. Until that time, see ya.

Laravel & PHP

More videos on Laravel & PHP

All videos

Get the next one by email

I’ll send you some more. Not too many.

Join 7,500+ other readers.

Read past issues