Make faster outbound requests with Laravel (10x faster, actually)
An example of how you can make your requests five times faster with Laravel Octane.
Everything I said
Laravel Octane can speed up your app a ton by booting it one time and then serving requests to that booted application. This is not a video about that. I do have that video, but this is a video about a weird little trick that ... O- one weird trick, developers hate them. This is about a weird little trick using Octane that can make your outbound requests, like, five times faster.
Let me show you what I'm talking about. Here is our application, if you can, if you can even call it that. It is a single route that hits the weather API with my zip code and my private key, which I've already rolled, so don't super care about that. And we don't actually do anything with the response, because I am not actually after the response. What I am after is we're gonna tell Guzzle debug of true.
So I wanna get a little bit more information from Guzzle about what's happening here. And because this is an Octane application, I'm gonna say Octane start and pass the watch flag, which will gracefully reload this app if we make changes. So this server is running now, and if we hit it in the browser, we see nothing, which is fine. That's what we expected.
But looking back here in the terminal, we see all kinds of information. So this is all of the information that Guzzle has dumped out here. What I am looking for is the first request comes in, and then it says, "Here's all the Guzzle information." So then it looks up the host, it does a bunch of connections, handshakes, all of this stuff, sends it all off.
And then the interesting thing, connection zero to the host was left intact. Well, that sure is interesting, isn't it? So I see connection zero left intact, and I think, "Wonder if I could reuse that connection?" In traditional PHP, no. The universe is created, the universe is destroyed every single request.
In Octane, yes, you can reuse that connection. Is it beneficial? Well, you have to imagine when you're creating a connection, it has to do all the DNS, and then it has to do the, the TLS handshakes. So it's gotta do a lot of stuff to establish a connection. How much stuff? I don't know, but I bet we can find out. So I've got this extremely basic class here. All that it does is creates a new Guzzle client and sets it as a static variable on this class.
Now, this is super important because using Octane, your app is going to be booted once and then kept in memory, which means any static variables, they're gonna be kept around. So now this client is going to stay alive between requests. So let's take a look over in web, and let's change this.
Let's just comment this out and say we want client to equal new API client get client. Or what is it? Create client. And then we can actually take this guy, come down here. And so now if we hit this again in the browser, and then we'll hit it again twice.
Now, come back over here and scroll up. Look at this. Reusing existing connection with weatherapi.com. So this is our second request here, and it says, "I'm just gonna reuse that existing connection." And so it doesn't have to go through that whole negotiation process. So we know already that Guzzle is reusing that connection, but the question still remains: Is it worth it?
Did we gain anything? Let's measure it and find out. We are gonna use Laravel's benchmark class for that. So we're gonna say benchmark measure, and we're gonna pass a closure through, and we're gonna do this twice. So we're gonna do two different measurements here. One is the plain old Guzzle one. So this is where it will not be reused, and so we gotta pass through use URL there.
And this one, we will use our static accessor. So we'll use ours that should be preserved there. We don't need all that information anymore, which means this gets a lot simpler, yada, yada, yada. So we will say, um, this is the fresh client, and this is the ...
What do you wanna call this? The cached client. And so we will, uh, yeah, let's just echo fresh was fresh and cached was cached. And let's see if we can see this in the browser. Okay, so off to a pretty similar start, but then if you look again, now the fresh one is taking 122 milliseconds, and the cached one is taking 28 milliseconds.
300 milliseconds versus 27. So you can see that this is a pretty durable result every time we're redoing it. Every time you have to create a fresh client and make that fresh connection, it's taking four to five to six to seven times as long as reusing the existing connection.
So if your application is making a lot of connections to third parties, you might consider using Laravel Octane and keeping that client as a static variable so that Guzzle will keep that connection open. If you wanna learn more about Laravel Octane, I've got a video that we'll put right here. Until the next time, see ya.