user482594
user482594

Reputation: 17486

Does Google Cloud Run allow you to spawn additional threads in Java?

When Cloud Run spins up an instance to handle HTTP request for my Java service, the service would have to reach out to multiple external services through network calls to get the final conclusion about the data.

Instead of making those external service calls sequentially, I want to parallelize them so that I can save execution time while waiting for networkIO to complete. This is because my service itself is not very compute-intensive, but spends most of the time waiting for network responses which each can take hundreds of (ms). Simple new Thread will do the work for me.

I was curious if Cloud Run limits/prohibits the creation of new threads, as I could not find that information in the documentation.

(e.g. Java AppEngine asks you to use gcloud specific ThreadManager API to manage threads.)

Upvotes: 3

Views: 3933

Answers (2)

guillaume blaquiere
guillaume blaquiere

Reputation: 75940

In addition, processing capability is close to 0 CPU allowed when no request is processing. Be sure that all threads processing are terminated when you send the http response. Moreover, on managed platform, you have 1 vcpu allowed. Thus multi thread for processing is useless ( but it's not your case, you just have to wait in parallel). Finally, in case of high concurrency, take care to not spawn to many threads! And of the memory.

Upvotes: 4

Doug Stevenson
Doug Stevenson

Reputation: 317760

Cloud Run doesn't impose any limitations on what you can do with code in your docker container, as long as you don't exceed the limits of the virtual server instance. Your only obligation is to send a response to the client over the HTTP connection that's being managed. If you want to start threads, that's fine. Your chosen HTTP server is likely also doing that. Just bear in mind that the server instance running your container could be deallocated at any time when it's not serving a request, and your threads will be terminated immediately without notice.

App Engine is very different from Cloud Run in that it strictly manages the runtime behavior of the code you deploy. Cloud Run doesn't do that at all - you get to decide which JVM to use, which HTTP server to use, and how they operate in the container that you deploy.

Upvotes: 6

Related Questions