Zenn Tech Blog

iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🎉

Migrating a Next.js App from Vercel to Google Cloud

に公開
2021/09/19
Inside Zennの記事です

At Zenn, we use Next.js for our frontend. Originally, we ran it on Vercel, but in March 2021, we migrated to Google Cloud. In this article, I would like to share the reasons for the migration, the specific architecture, and the migration process.

Why we migrated

Vercel is overwhelmingly superior as a deployment platform for Next.js. You can use powerful Next.js features like ISR and Image Optimization without any additional server-side configuration, and it handles things like static file caching on CDNs automatically.

When deploying outside of Vercel, many parts require manual maintenance, such as some Next.js features not working correctly or needing to put effort into performance tuning yourself.

However, in Zenn's case, we decided to migrate from Vercel to Google Cloud for the following reasons.

Reason 1: To reduce costs

The biggest reason is the running cost. As of March 2021, Vercel had only three pricing plans:

  • Hobby... $0
  • Pro... $20/month
  • Enterprise... Custom pricing

https://zenn.dev/lollipop_onl/articles/eoz-vercel-pricing-2020

At Zenn, we were using the Pro plan, but we were gradually approaching the ~1TB/month bandwidth limit, forcing us to decide within a few months whether to switch to the Enterprise plan or migrate to another service.

The Enterprise plan apparently costs several thousand dollars per month, and we were hesitant to spend that amount at the moment. Furthermore, we expected the costs to rise as the service continued to grow.

On the other hand, we found that we could operate on Google Cloud for a few hundred dollars per month for the time being. We are grateful to Vercel and had a desire to support them by switching to the Enterprise plan, but since we are not in a phase where we can afford that kind of spending, we decided that Google Cloud, which allows for low-cost operations, would be better.

Reason 2: Concerns over runtime limitations

Vercel uses AWS Lambda, and Next.js processes such as SSR and ISR are executed on Lambda as well. Consequently, we are indirectly subject to Lambda's limitations.

We were particularly concerned about the payload limit; on Vercel, if the request/response body exceeds 5MB, a 413: FUNCTION_PAYLOAD_TOO_LARGE error occurs. This is rarely an issue unless we are processing images, but because we struggled with this limitation during a previous outage, I wanted to address it if possible.

Reason 3: Consolidating the frontend and backend on GAE offers several advantages

Zenn's backend runs on Google App Engine (GAE). By unifying the frontend on GAE as well, setup and management become easier.

Easier CI/CD


Deployment workflow until now

By unifying on GAE, CI/CD using Cloud Build becomes easier, and the configuration becomes more transparent.

Easier to create staging environments

Integrating GAE and Vercel was quite troublesome when building integration or staging environments. Consolidating on GAE simplifies the configuration and enables IaC (Infrastructure as Code). Additionally, with GAE, we can use access control via Identity-Aware Proxy (IAP) almost for free.

Note: Vercel also has a password protection feature for deployments, but it costs $150/month to use.

Ability to use a common domain

We were using a subdomain (api.zenn.dev) for the backend, but by migrating the frontend to Google Cloud as well, we can now use a common domain (zenn.dev/api/*).

Reason 4: Minimizing exposure to outages

Previously, we had an architecture where the entire site would go down if an outage occurred in GAE, Vercel, or Lambda. For example, in February 2021, we were affected by both the Vercel outage and the AWS Tokyo Region issue. Reducing our dependencies is also psychologically comforting.

Reason 5: Avoiding the latency issue with custom domains in the GAE Tokyo region

In the GAE Tokyo region, using a custom domain increases latency.

https://zenn.dev/catnose99/articles/56f523d39cca43

Based on my own tests, I found that applying a custom domain results in a response delay of approximately 50-150ms (static files cached at the edge do not seem to be affected). This issue can be resolved by deploying a Google Cloud Load Balancer and pointing the custom domain to the load balancer. I will explain the detailed architecture later.


For the reasons above, we decided to migrate from Vercel to Google Cloud. It wasn't because we were dissatisfied with Vercel itself, but because we judged that operating on Google Cloud was better from an overall perspective. When I build new apps with Next.js in the future, I will likely choose Vercel unless there is a very compelling reason not to.

New architecture

We now run both the frontend and backend on GAE, with a Load Balancer (Cloud Load Balancing) in front. For the frontend, we have enabled Cloud CDN to partially cache files.

Note: Cloud CDN is used in conjunction with Cloud Load Balancing.

We point the zenn.dev domain to Cloud Load Balancing and use the URL map feature to route requests to the appropriate service based on the path. This resolves the latency issue with custom domains in the GAE Tokyo region, and we have noticed a perceptible improvement in API response speeds.

I have summarized the GAE and Cloud CDN/Load Balancing configurations in other articles:

Discontinuing ISR

As I wrote in Strategies for caching dynamic content with Next.js ISR, we originally made heavy use of Next.js ISR at Zenn.

However, since it is currently very difficult to implement Next.js ISR outside of Vercel, we decided to stop using the default Next.js ISR.

Fortunately, Cloud CDN supports stale-while-revalidate (SWR), a cache control directive that achieves behavior similar to ISR, so we plan to utilize it for pages like post lists in the future.

↓ Reference

https://zenn.dev/catnose99/articles/0b601c1f62019b

Additionally, for detailed pages like articles and books, we decided to eliminate caching altogether due to the high risk of improper cache settings. (The source code has become much clearer!)

Migration procedure

For the migration work, I simply created a To-Do list and tackled them one by one. I performed roughly the following steps:

  1. Removed ISR implementation and rewrote it to work in the new environment.
  2. Configured GAE, CDN, Load Balancer, and certificates in the development environment and verified that everything worked correctly.
  3. Switched from Vercel to Google Cloud in the development environment's DNS settings.
  4. Adjusted the autoscaling settings such as the number of instances through load testing in the development environment.
  5. Created a staging environment (the eventual production environment) on Google Cloud, identical to the development environment.
  6. Maintenance announcement (tweet).
  7. (On the day of migration) Pointed the zenn.dev domain to the staging environment in the DNS settings.

It took about two weeks from starting the preparations until the migration was complete.

Key points to keep in mind

Here are the points I focused on to ensure a smooth migration:

  • Deployed changes that were safe to apply to the running production environment in advance, keeping the changes during the migration itself as small as possible (to make it easier to identify the cause if problems arose).
  • Performed the migration in the development environment beforehand and ensured that exactly the same procedures were executed for the production environment.
  • Created extremely detailed step-by-step instructions for the day of the migration so that I wouldn't make mistakes even if I wasn't thinking clearly (ensuring the migration would be completed just by following the steps).

As a result, the migration was completed with almost no downtime. I have finally cleared one of the infrastructure-related worries. I am happy to be able to start working on improving the service itself.

Zenn Tech Blog
Zenn Tech Blog

Discussion