Loading
Current section: Multi-Region Data and Deployment 6 exercises
lesson

Scaling to Multiple Regions

We're finally ready to go multi-region!

First, let's set up a couple of things in our terminals to monitor what's happening.

We'll run fly status --watch in one terminal and fly logs in another to keep an eye on everything.

Creating Persistent Volumes

In Fly, you manage your regions by s

Loading lesson

Transcript

Instructor: 0:00 With all the work we've done leading up to this point, I'm really excited to finally go multi-region with you. We're going to set up a couple of things in our terminals so that we can watch what's going on.

0:10 First, we're going to say fly status with a watch flag. This will keep us up to date with what regions are currently active and all of that. Then, we'll make another one that is fly logs, so we can keep our logs going. Now we're going to create multiple regions. In Fly, you can run a simple command.

0:29 If we look right here, you can say fly regions add yada yada yada, but when we're using persistent volumes, you actually manage your regions by what regions you specify persistent volumes to be in. To create a persistent volume, you say fly vol. We'll just see what the output there for that is.

0:48 Fly vol, we want to create, and for the region that we want it to be in, we're going to deploy another region in the sjc region, so we'll have two instances in sjc. That way, either one of them can be the candidate, and if one falls over, the other can take over. We'll say fly vol create, and we want to call this data. We'll say region is sjc.

1:13 We're calling this data because we've configured that in our fly.toml, so we want to keep it to be the same name, so the region is sjc. We're going to say the size is one because this is just a demo app. Depending on how much data you have, you may want to increase that size.

1:27 We'll execute this, and Fly is going to create a persistent volume for us, and now that's a part of our app. We can say fly vol list, and this will list all of the volumes we have in our app. Only one of them is attached to a VM.

1:45 If we look at our fly scale settings here, we can say fly scale show, and our current resources for our app are set to a single instance, so a count of one. Now, if we want to have two instances so that we can connect this to another VM, then we can say fly scale count one. We'll take a look at what this does to our logs here in just a second.

2:12 We'll also see some output in our status, where it actually starts booting up a new instance. Here, we actually changed it to one, so let's change it to two. We want two instances, and now we can see it here in just a second. Our logs will have it popping up a new instance, and we'll see that new instance showing up here.

2:33 Great. In our logs, we can see we're starting a new instance. We'll see our previous instance was this ebb instance ID, that's the host name, and our new instance is running in this runner. Our new instance is here, this 0d8-yada-yada. You'll see our logs are now all mixed up, and you can run logs specifically for a specific instance and things, which can be quite useful.

2:55 You'll see some really familiar logs in here, but one thing that will be new is right here. We see a existing primary found, connecting as a replica. This new instance that we just created is going to be a read replica instance. It's a little hard to demonstrate that we're actually running against a primary or a read replica here, but the application does still work.

3:24 That's a good sign. We can take a look at our watch output. We see that we have two instances running now. That's cool to have some horizontal scaling, hooray for that, but let's actually deploy to another region, a completely different region. We're going to just go up, up, up, up, and our region, we'll say, is ams.

3:45 We're going to go to Amsterdam, and create that volume with a size one, so exactly the same sort of thing, but we still only have a scale of two. We're going to increase this to a scale of three, so fly scale count three. Once Fly realizes, "Oh, we've got another instance possibility here, we've got a volume there, so let's go ahead and get that started," and there it is.

4:07 Our runner is starting an instance in ams, and in just a couple of seconds, we'll get that instance started up. You'll see some more of those logs. It'll say it's connecting as a read replica. Here we go, uninitialized volume data. It's initializing, it's encrypted, and opening encrypted volume. Now it can actually start up our app.

4:28 We've got LiteFS running. It connected as a replica right here, and now our application is running in Amsterdam. If you really want to test this out, I use a extension called ModHelper, that allows you to specify a region, so you can prefer a specific region. What this effectively is doing is it's simply going to add a request header called fly-prefer-region, and the value is ams.

5:00 You can see that I've done this a lot for different regions to test this out. If you pull up your DevTools, and here we refresh, you'll notice this takes a little bit longer, because we are going around the world now. You'll see, in the request headers, we see our fly-prefer-region right there, so we are sending that.

5:19 Fly will recognize that and make sure that we're routing to the proper region. With that, if I do my POST request here, and that triggers a full refresh, but if we look at that POST request, we get that fly-prefer-region. There's nothing in here for us to notice exactly which region handled it, but if we take a look at our logs, we'll look for that POST.

5:41 You'll see that that was handled by sjc, appropriately, because that's the only one that is the primary region. If you recall, this is the instance that's been running the longest. The ebb one that we have right here, which has been running for longer than the rest of them, so this is the primary region.

6:00 That's how you add a bunch of new regions to your application. When we're dealing with a persistent volume, you're going to say fly vol create, you give it the name that we have in our fly.toml. Here, we call it data, and so that's why we said fly vol create data.

6:18 Then we specify the region, we specify the size. That creates it, and then we use the fly scale count command so that we can say, "Hey, we want to have another region." You can do this for all 26 regions, or however many Fly has, and you'll have this running all over the world.

6:34 Then, of course, redeploys work really well and make sure to just take the primary region and get a new primary region up and going. Then the rest of them come up, and it's amazing. That is how you get multiple regions running in Fly.