As the title suggests, I need to know if its possible to deploy a single NextJS App, with serverless functions, into two regions, namely, us-west and singapore. And have it so there’s one url and the users get routed to the closest deployment?
I’m asking because Vercel doesn’t allow this without forking out huge amounts of cash per month.
Please, if anybody can explain how to do this task, or if its even possible here. I’d greatly appreciate it.
Can you clarify what you mean by serverless? Serverless on Vercel (ie AWS) is slightly different than Fly (which are full servers.)
If you need the traditional serverless sense (AWS) then you might find Get Started - OpenNext interesting.
But you can achieve 99% of what Vercel offers by running your next app in a container on Fly. The 1% would be the Vercel specific functionality: distributed ISR caching, PPR, and next/after.
So like, in NextJS, /api endpoints are run as serverless functions. On Vercel, you can specify multiple regions for those serverless functions, but by default its just one.
I just have users in two regions so it should theoretically be as easy as just adding another region. If I can get that going I’d be happy.
Some more context: I have a CockroachDB cluster with regions in my two targeted areas. So just trying to put NextJS at those areas so as to make queries to /api as fast as possible for my users
Yes it’s possible. Instead of a “serverless” function, Fly boots up a container, runs the next app, and serves the request closest to your user (uswest or singapore).
If you configure your app to autostop, it’ll shutdown after 5-10minutes of idling.
This all sounds good to me. How would I go about doing this with fly? Like can it all be done with the fly webapp? Deploying the project to whatever regions I choose then setting up some sort of loadbalancer (if necessary) to route the users to the closest deployment?
Most of it will be via the flyctl CLI, see Getting started · Fly Docs you have to learn to walk before you fly
You don’t have to worry about any load balancing, fly handles all that.
And again confirming: “one url and the users get routed to the closest deployment” - you don’t have to do anything to get that. Start in one region, use fly scale count to add a second region, and you are done.
So the process of deploying to fly with multiple regions has been a breeze! Although I’m a little confused.
So, to test the regional deployment, I used a proxy ip in philippines, and called an api endpoint within my nextjs app that simply returns the ip address of the caller.
IP of Function call when done from Philippines: 2605:4c40:158:9902::6d6b:9f22:1
IP of Function call when done from Toronto: 2605:4c40:92:522a::e86e:a01e:1
This is in context to the original concern I mentioned. The Singapore machine is live, but in NextJS there is an /api route I call that returns me the ip of where the endpoint is being executed. So I connected to Philippines proxy, got the Singapore machine to boot up and loaded the website, but then hit the API from that, and it got executed in Chicago instead of Singapore?
Yeah just YYZ and SIN. Hmm, makes me wonder if it is the VPN. But why would the VPN successfully load the site from Singapore and not route the api call correctly.
You can also pass this header from your client app. Or if you want to get really fancy, create an ephemeral machine in any region and run curl from there:
fly console --image curlimages/curl -C sh --region syd
Instead of (or in addition to) displaying the ip address, there are a number of interesting request headers: Public Network Services · Fly Docs