Cold boots and warming0:00
The first time your function is run, AWS has to spin up a new container, grab all your code, initialize your code, and then run the handler. That's called a cold boot, and it can take a lot of time, but Sidecar offers the ability to warm your functions. If you've used Laravel Vapor, you know that Vapor has a warming mechanism as well. Let's see how we can take advantage of that. We're going to use this browser function again. Let's look at this real fast so we can remember what's happening. So in our handler, we don't do very much. We await a page, then we visit Laravel.com and return the page title.
So in our handler, we don't do very much. We await a page, then we visit Laravel.com and return the page title. You might remember the reason we moved this out here is because this runs one time. This down here runs over and over. Because this is expensive, we moved it outside into the initialization phase. So in this method, this is where we launch the Chrome browser, we grab a new tab, and we return that tab. Now we're going to take a look at how warming might speed this up even faster for the first request. Before we do that, let's just deploy it and see how long it takes without warming it.
Baseline deploy timing1:17
request. Before we do that, let's just deploy it and see how long it takes without warming it. We'll run php artisan sidecar deploy and activate. Now we need to make sure we're invoking this in web.php, so we'll say browser execute, and we'll just return that to the browser, and then hit it from serverless.whip. So the first request took 4.38 seconds. Let's run it again and see how long it takes. After that, it's a whole lot faster. And that's because Chrome is booted, the tab is open, all we're doing is navigating to Laravel.com, which is a whole lot faster, right?
Using the pre-warm flag1:57
And that's because Chrome is booted, the tab is open, all we're doing is navigating to laravel.com, which is a whole lot faster, right? There may be times when this 4.8 seconds is just simply not viable. In those cases, you might consider pre-warming your functions. If we look at the deploy command and call the help on it, we do see that we have the --pre-warm flag, and what that flag does is that will issue requests to the function before it's activated. So between deployment and activation, it will issue a few requests so that the function will warm up, the container gets ready, all the initialization happens, and then after the warming is done, then it will activate it.
will warm up, the container gets ready, all the initialization happens, and then after the warming is done, then it will activate it. We'll come back to our index.js, and we just need to change something so that Sidecar will redeploy it. This time we're going to run php artisan sidecar deploy. We're still going to activate it, but we're also going to pre-warm it. And it looks like we got close, but it didn't actually warm it. It says no instance is warmed. If this is unexpected, confirm your warming config method is set up correctly. So we need to look at this warming config and see what's going on here.
Adding warming config3:11
If this is unexpected, confirm your warming config method is set up correctly. So we need to look at this warming config and see what's going on here. We'll go to our browser function. We don't have a warming config, so let's just put one in. We can check the parent to see what's going on. Return new warming config. So it says a warming configuration that can help mitigate against cold boot. Yep, that's what we're doing. Let's see what warming config is. warming config is pretty simple.
Let's see what warming config is. Warming config is pretty simple. All you do is you can set the number of instances you have and what you want the payload to be. So it looks like it has a couple of static helpers with payload. Okay, let's set up a warming configuration for this method. We will return warming config. We just do one instance. You could, in theory, do a hundred instances. It just depends on how many containers you want to have ready to go.
Handling warming payload4:08
You could, in theory, do a hundred instances. It just depends on how many containers you want to have ready to go. We'll do one instance and with payload, I think this is the default, but we're just going to say warming true. We know that we're going to pass warming true when it's a warming request, and so we can come in here and just bail out early. We can say if event.warming, we'll just return. This is the expensive thing. Remember, this is what will open the browser and the new tab, and so that's the thing that we want to happen.
