Search code examples
spring-bootgoogle-cloud-platformmicroservicesserverlessgoogle-cloud-run

Why is my google cloud run service inactive if requests not made in the last 24 hrs?


I have a google cloud run microservice written in java spring boot. If the service remains inactive for around 24 hrs, and then a request comes after that time period, the service takes too much time to respond to the first request. All subsequent requests run smoothly. Checking logs on cloud run only tells me that the spring-boot server was started to handle the request. Why did it have to start again? Why was it not constantly listening like a normal backend server would? My Dockerfile:

FROM maven:3.6.3-openjdk-8 as builder
# Set the working directory.
WORKDIR /usr/src/mymaven
COPY ./ /usr/src/mymaven
RUN [ "mvn" , "clean" , "install" ]

FROM openjdk:8
COPY --from=builder /usr/src/mymaven/target /usr/src/myapp
WORKDIR /usr/src/myapp
CMD ["java", "-jar" , "Backend-0.0.1-SNAPSHOT.jar"]

Solution

  • Since (fully managed) Cloud Run is a serverless platform where you aren't billed when requests aren't actively running, it is optimized for actively serving requests. As such, it will idle/stop instances that aren't actively being used. Therefore, you need to build your service to start quickly (or be tolerant of more latency for the cold-start requests) if it isn't going to be frequently accessed.