# HTTP 503 服务不可用：请求失败，因为服务正在扩展。请稍后重试。

- **ID:** `cloud/gcp-cloud-run-cold-start-http-503`
- **领域:** cloud
- **类别:** runtime_error
- **错误码:** `HTTP 503`
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

Cloud Run 的冷启动延迟（因容器镜像拉取和启动）超过请求超时时间，导致负载均衡器在容器就绪前返回 503。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| Cloud Run (fully managed) gen2 | active | — | — |
| Cloud Run for Anthos 1.28 | active | — | — |

## 解决方案

1. ```
   将最小实例数设置为至少 1 以保持一个常驻实例：gcloud run deploy SERVICE --min-instances 1。生产环境建议设为 2-3 以应对流量峰值。
   ```
2. ```
   优化容器启动：使用 distroless 基础镜像、减小镜像体积、将初始化移至后台线程。示例 Dockerfile：FROM gcr.io/distroless/java17-debian11，然后使用 Spring Boot 的懒加载。
   ```
3. ```
   启用 'startup CPU boost' 在容器启动期间分配额外 CPU：gcloud run deploy SERVICE --cpu-boost
   ```

## 无效尝试

- **** — Simply retrying the request without addressing cold start may succeed eventually but adds latency and costs. (70% 失败率)
- **** — Increasing max instances doesn't reduce cold start frequency; it only limits concurrency. (95% 失败率)
- **** — Setting min instances to 1 reduces cold start for the first instance but doesn't help if all instances are busy. (60% 失败率)
