Java 21 and Spring Boot 4 on Cloud Run, on virtual threads

The API is Spring Boot 4 on Java 21 with virtual threads, in a container on Cloud Run that scales to zero, with class data sharing to shorten cold starts.

Status
Accepted 1 October 2026 · runtime amended by ADR 0009: Java 25 with an AOT cache
The record
0003-java-21-spring-boot-4-on-cloud-run.md

Context

The backend is part of the portfolio: it should be written in the stack the site is selling. Traffic is low and bursty; most work is I/O (streams, LLM calls later), plus two deliberately CPU-heavy lab endpoints.

Decision

Spring Boot 4.1 on Java 21 with spring.threads.virtual.enabled=true, packaged as a container and deployed to Cloud Run with min-instances 0 and max-instances 1. Class Data Sharing is trained at image build time to shorten cold starts.

Consequences

  • Scales to zero: the first visitor after a quiet period waits for a cold JVM. The UI says so instead of hiding it.
  • max-instances 1 makes in-memory rate limiting correct; scaling out would move limits to Redis.
  • GraalVM native image remains an option if cold starts become a problem.