Laravel Octane: supercharge your Laravel applications

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome to the what is laravel series my name is Aaron Francis in this series of many many videos I'm going to be covering the entire laravel [Music] ecosystem today we're going to 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 but before I explain how it makes makes it faster I need to tell you a little bit about how PHP Works in general typically typically you'll have engine X sitting in front of a pool of PHP fpm workers when a request comes in that is not a request for a static asset engine X hands that request off to PHP fpm 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 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 that's going to 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 that's how PHP has always worked until laral octane let's talk about laravel octane octane was first announced at Lon online in 2021 and the first version was tagged in May of 2021 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 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 swool open swool Franken PHP and Roadrunner those are the four drivers that you can choose with octane each different driver has its own pros and cons 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 engine X handing off requests to PHP fpm engine X 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 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 laral 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 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 work 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 going to 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 larel application here that I just created from scratch the only thing I've done is replace the default route with a Hello World so if we hit that in the browser at Octane doest you see there is our hello world easy enough so far let's go ahead and install laravel octane you just say composer require ll/ octane it's going to do its thing we'll clear that out and then we want to run one command which is octane install and this is going to ask us for which driver we want to use I'm just going to pick swool 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 so now we're running this on both places this is running locally through herd which is engine X to PHP fpm and this is running through octane all right we're going to put both of these systems under load the octane and the non Octane and we're going to see which one fails first I think it's going to be the non octane version but this is not a benchmark this is just a comparison there are a bunch of commands you can run to make laral production ready I haven't run any of those cuz 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 do test which is the non octane version and we look at the Timeline down here you see that 42 milliseconds is spent booting laravel the framework right so if we refresh this a couple times you'll see it bounce around but it's between 18 and 40 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 537 so this is what I'm talking about when I say it Boots the application once and keeps it in memory so we've already gained back between 20 and 30 to 40 milliseconds already now let's run a few fake little load tests here I'm going to use the work command and we're going to run four threads with 150 connections for 5 seconds against the octane version and we're going to see how well this does so after 5 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 doest 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 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 cash driver and get up to two million operations a second as a cash driver which is pretty insane 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 so if you're installing this in a legacy application you're going to 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 want to restart all of those octane workers at the end of your deploy script so that your new application does get loaded into memory octane has a few commands that you can run to do that if you're running your application on LEL 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 that's it for octane a free and open-source Library maintained by the larel team that boosts your application performance stay tuned to this channel I'm going to be covering everything there is to know about the laral ecosystem and much more until that time see you
Info
Channel: Aaron Francis
Views: 33,637
Rating: undefined out of 5
Keywords:
Id: YGBvdAWt0W8
Channel Id: undefined
Length: 8min 33sec (513 seconds)
Published: Fri Apr 26 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.