kakakakakku blog

Weekly Tech Blog: Keep Learning!

Cloud Run の Startup CPU boost でコールドスタートを高速化する

はじめに

Cloud Run の Startup CPU boost(起動時 CPU ブースト)を使うとコンテナのコールドスタート時間を短縮できる.具体的にはコンテナの起動中に「10秒間」多くの CPU を割り当ててくれるような仕組みになっている.

Google Cloud Blog の記事を読むと Java で実装された Spring PetClinic Sample Application は最大 50% も短縮されて,シングルスレッドの Node.js でも最大 30% も短縮されたと書いてあった.

cloud.google.com

さらに記事の最後には Cloud Run functions ではデフォルトで Startup CPU boost(起動時 CPU ブースト)が有効化されていると書いてあった💡へぇ〜

Even better, Cloud Functions uses startup CPU boost by default.

また Cloud Run の General development tips ドキュメントを読んでいたら Use startup CPU boost to reduce startup latency というガイダンスも載っていた📝

docs.cloud.google.com

最近 Google Cloud や Cloud Run を試していて,Startup CPU boost という機能は AWS にはないよな〜(AWS Lambda SnapStart はまた違うアプローチだし)と思って興味を持って試してみることにした❗️

環境

今回は Google Cloud Blog の記事にも載っていた Spring PetClinic Sample Application を使う.

github.com

Cloud Run を実行する(Startup CPU Boost: false

Terraform で実装した!

resource "google_cloud_run_v2_service" "noboost" {
  name     = "spring-petclinic-rest-noboost"
  location = "asia-northeast1"
  ingress  = "INGRESS_TRAFFIC_ALL"

  template {
    containers {
      image = "docker.io/springcommunity/spring-petclinic-rest:4.0.2"

      ports {
        container_port = 9966
      }

      resources {
        limits = {
          cpu    = "1"
          memory = "1Gi"
        }
        startup_cpu_boost = false
      }
    }
  }
}

resource "google_cloud_run_v2_service_iam_member" "noboost" {
  name     = google_cloud_run_v2_service.noboost.name
  location = google_cloud_run_v2_service.noboost.location
  role     = "roles/run.invoker"
  member   = "allUsers"
}

Cloud Run を実行する(Startup CPU Boost: true

Terraform で実装した!

resource "google_cloud_run_v2_service" "boost" {
  name     = "spring-petclinic-rest-boost"
  location = "asia-northeast1"
  ingress  = "INGRESS_TRAFFIC_ALL"

  template {
    containers {
      image = "docker.io/springcommunity/spring-petclinic-rest:4.0.2"

      ports {
        container_port = 9966
      }

      resources {
        limits = {
          cpu    = "1"
          memory = "1Gi"
        }
        startup_cpu_boost = true
      }
    }
  }
}

resource "google_cloud_run_v2_service_iam_member" "boost" {
  name     = google_cloud_run_v2_service.boost.name
  location = google_cloud_run_v2_service.boost.location
  role     = "roles/run.invoker"
  member   = "allUsers"
}

結果

コールドスタートになるように間隔を空けながら合計4リクエストを実行してみた⌛️

確かに Google Cloud Blog の記事と似ていて 40% 前後の短縮になっていた👏

$ curl -s -o /dev/null -w "%{time_total}s\n" https://spring-petclinic-rest-noboost-00000000000.asia-northeast1.run.app/petclinic/api/pets      
32.400681s
26.432985s
25.692748s
28.576059s

$ curl -s -o /dev/null -w "%{time_total}s\n" https://spring-petclinic-rest-boost-00000000000.asia-northeast1.run.app/petclinic/api/pets
18.956838s
15.405225s
15.316089s
15.371090s

さらに Cloud Monitoring の Metrics Explorer で Container startup latency の値を確認すると同じように 40% 前後の短縮になっていることを確認できた👏

まとめ

Cloud Run の Startup CPU boost(起動時 CPU ブースト)を試してみた.Java で実装された Spring PetClinic Sample Application のコールドスタート時間が 40% 前後の短縮になった.設定も簡単でコスト影響も少ないため機会があったら導入してみよう💪

Google から Kube Startup CPU Boost っていうリポジトリも公開されてるし,裏側ではこういう仕組みを使ってるのかなぁ〜!?

github.com