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
🦀

Optimizing Next.js Builds in Cloud Build

に公開
Inside Zennの記事です

Introduction

In our Zenn project, we use Next.js for the frontend. The execution environment is Cloud Run on Google Cloud, and builds are performed using Cloud Build.

All subsequent information pertains to our staging environment.

Cloud Build is triggered by a push to a specific branch on GitHub, which builds the Next.js application, creates a Docker image, and pushes it to the repository. It then creates a new revision on Cloud Run and deploys the new Docker image.

This entire process was taking about 8 minutes on average.

Thinking this was too long, I attempted to optimize it and succeeded in reducing the time to about 3 minutes and 20 seconds on average.

Conclusion first

The effective changes were as follows:

  • Match the Cloud Build location to the same region as the Artifact Registry (where the Docker images are stored).
  • Use pnpm for installing dependencies and disable kaniko layer caching.

Initial state

First, let's look at the initial state.

The Cloud Build process consisted roughly of the following steps:

  1. Installing dependencies
  2. Building the Next.js application
  3. Pushing the Docker image created in steps 1 and 2
  4. Creating and switching to a new Cloud Run revision

We used kaniko to build the Docker image. kaniko is a tool for building Docker images that can store the image's layer cache remotely. This allows even Cloud Build environments to utilize layer caching. In practice, the installation of dependencies in step 1 almost always used the layer cache (unless dependencies changed or the cache expired).

The machine type for Cloud Build was E2_HIGHCPU_32 (using kaniko's layer cache on E2_HIGHCPU_8 would cause the process to fail due to memory exhaustion).

The breakdown of the process time in the initial state was as follows:

Task Time
Dependency installation (fetching/extracting layer cache) 3 min 40 sec
Next.js build 1 min 50 sec
Creating and pushing layer cache 20 sec
Pushing Docker image 15 sec
Cloud Run revision creation and deployment 50 sec

Including other minor tasks, the total time was about 8 minutes.

*Note: Dependency installation almost always hit the layer cache, so the time shown is for when the layer cache is used.

From here, I worked on resolving the bottlenecks while consulting with an LLM.

Changing the Cloud Build location

In the initial state, the Cloud Build location was the default "global (non-regional)."

https://cloud.google.com/build/docs/locations?hl=en

Regarding the selection of a location, the official documentation states the following:

When selecting a region for your build, prioritize latency and availability. In general, you should select the region closest to your Cloud Build users, but you should also consider the location of other Google Cloud products and services that your build integrates with. Having services located across multiple locations affects both application latency and cost.

To reduce the time taken to fetch and push the layer cache, I changed the Cloud Build location to the same region as the Artifact Registry.

As a result, the breakdown of processing time became as follows:

Task Before After
Dependency installation (fetching/extracting layer cache) 3 min 40 sec 2 min 10 sec
Next.js build 1 min 50 sec 1 min 20 sec
Creating and pushing layer cache 20 sec 10 sec
Pushing Docker image 15 sec 2 sec
Cloud Run revision creation and deployment 50 sec 50 sec

Overall, the time spent fetching and pushing the image was reduced. For some reason, the Next.js build time also decreased. This reduced the total processing time from 8 minutes to about 5 minutes and 30 seconds.

Using pnpm for dependency installation

"Dependency installation (fetching/extracting layer cache)" still took a significant amount of time.

By default, the layer cache is stored in a path that adds /cache to the --destination flag (the push destination for the Docker image). I checked the size of the layer cache for "dependency installation" and found it to be 1.4GB.

When I pulled this to a Cloud Shell environment, the download took about 20 seconds, but the extraction took a long time. The contents are, of course, the node_modules directory. Although I am not familiar with the detailed mechanism of kaniko's layer cache, I inferred that it takes a long time to extract an image containing a large number of files.

At this point, dependencies were managed with yarn v1, but I decided to use pnpm to reduce the number of files. (Although I'm not an expert on this either, my understanding is that pnpm manages node_modules with symbolic links, preventing file duplication and saving disk space.)

I tried installing dependencies in my local environment and compared the size of node_modules after archiving it with tar, with the following results:

Package Manager Size Installation Time
yarn v1 841MB 60 sec
pnpm 439MB 10 sec

Improvements were also seen in the Cloud Build environment:

Task Before After
Dependency installation (fetching/extracting layer cache) 2 min 10 sec 30 sec
Next.js build 1 min 20 sec 1 min 20 sec
Creating and pushing layer cache 10 sec 1 sec
Pushing Docker image 2 sec 1 sec
Cloud Run revision creation and deployment 50 sec 50 sec

This reduced the total processing time from 5 minutes and 30 seconds to about 3 minutes and 40 seconds.

Disabling kaniko layer caching

I finally realized it after coming this far: it was actually faster to install dependencies with a package manager than to use the layer cache for "dependency installation" from the beginning. And by switching the package manager to pnpm, the installation time became even faster.

Also, there are almost no scenarios where caching is effective for other layers. Therefore, I decided to disable kaniko's layer caching. To disable it, you specify the --cache=false flag.

Task Before After
Dependency installation 30 sec 5 sec
Next.js build 1 min 20 sec 1 min 20 sec
Creating and pushing layer cache 10 sec -
Pushing Docker image 2 sec 1 sec
Cloud Run revision creation and deployment 50 sec 50 sec

This reduced the total processing time from 3 minutes and 40 seconds to about 3 minutes and 20 seconds.

Since I disabled the layer cache, I no longer needed to use kaniko. Ultimately, I changed it to execute docker build and docker push using gcr.io/cloud-builders/docker.

Conclusion

I learned that fetching a layer cache containing a large number of files from a remote location takes a long time to extract.

However, it doesn't mean that one should never use kaniko in Cloud Build. For Zenn's backend server (Ruby on Rails) Docker build, using kaniko's layer cache still results in shorter processing times, so we continue to use it there.

GitHubで編集を提案
Zenn Tech Blog
Zenn Tech Blog

Discussion