KK2491
KK2491

Reputation: 508

Concurrency in Java applications - Cloud Run

I am using Cloud Run to run a java application and the concurrency for this cloud run service is set to 1. Whenever it receives a message through a http end point, applications get triggered runs for X amount of time and returns the result.

I would like to understand the below items:

  1. What would happen if the cloud service gets one more http request and triggers the java application before the previous run is completed?
    (For example, lets assume the java application takes 5 seconds to complete the execution and send the response back. When one is already in progress, it receives one more http request at 3rd second and triggers the java application)
  2. Does it cause any failure in the expected result?

Thank you

Upvotes: 2

Views: 409

Answers (1)

MBHA Phoenix
MBHA Phoenix

Reputation: 2217

I will start by answering the second question:

Given you already have set concurrency (Maximum requests per container in the console) to 1.

the concurrency for this cloud run service is set to 1.

You will get an error code 429 for the new request, only if you have set max-instances ( Maximum number of instances in the console).

Ref: https://cloud.google.com/run/docs/troubleshooting#429

Otherwise and back to your first question: if max-instances > 1 a new Cloud Run instance will be created and the second request will suffer the container/application startup time.

This being said, if you are using Cloud Run, you are using a webserver like springboot and/or tomcat behind. Such server will be setup to handle concurrent requests with a thread-per-connection model and a default value of 200 for server.tomcat.max-threads. So why not profit from this model, and increase concurrency, if your request are not dealing with shared resources. Of course try to use non-blocking reactive programming model to reduce the number of threads and their memory footprint.

But if, for your requirement, you need to keep concurrency at 1, in this case Cloud Function will be a better choice, for these reasons:

  • You don't have to setup a webserver
  • Less memory, and less CPU requirement(no webserver, no multi threading)
  • Simpler code

Upvotes: 4

Related Questions