Understanding threads and task queues

537 views
Skip to first unread message

Johann Blake

unread,
Feb 6, 2018, 9:02:53 AM2/6/18
to Google App Engine
In my GAE app I need to perform an OAuth2 flow by passing an authentication code to a Google endpoint in order to retrieve a user's profile. As part of this process, I also need to store the user's email address in GAE Datastore. The following points have been documented about GAE and Java 8:
  • Each request is limited to 50 concurrent App Engine API request threads.
  • When using threads, use high level concurrency objects, such as Executor and Runnable. Those take care of many of the subtle but important details of concurrency like Interrupts and scheduling and bookkeeping
  • The maximum number of concurrent background threads created by the App Engine API is 10 per instance. (This limit doesn't apply to regular Java threads unrelated to the App Engine API.)

So what happens when I have 100 concurrent requests occurring? Since only 50 concurrent App Engine API request threads is allowed, what happens to the requests once this limit is reached? Does GAE simply just put those requests in a waiting cache or terminate the request by issuing some response with a failure code? Does an exception get thrown? Or does GAE start up a new instance to service the new requests (I would hope so)?

It also isn't clear how to interpret what is meant by 50 concurrent App Engine API request threads. Does this mean that my servlet will only ever see a maximum of 50 current requests at any given time? Or does it mean that these threads are only applicable to App Engine APIs such as storing data in the Datastore or Cloud Storage? And then there is third bullet item that says there is a maximum of 10 background threads per instance for App Engine APIs. How is this different than the 50 request threads?

And finally, to accomplish my goal of obtaining the user's profile, should I be using a task queue or a thread to accomplish this? It seems that task queues were the original way of doing things before support for Java 8 threads was provided. Or have I misunderstood something?

Thanks for clarifying this.
Johann

Katayoon (Cloud Platform Support)

unread,
Feb 7, 2018, 10:18:14 AM2/7/18
to Google App Engine
App Engine's ThreadManager API will throw a java.lang.IllegalStateException if you try to use it to create more than 50 threads in a single request. You could have as many requests as you would like to, but you should handle them in a way that each request only creates up to 50 concurrent App Engine API request threads.

Background threads are the threads that live outside of the request environment and based on the documentation, the maximum number of concurrent background threads created by the App Engine API is 10 per instance, not per request.

For obtaining the user’s profile, it is on your preference. You could use either simple a request or a task queue or thread based on your application’s requirements.


Reply all
Reply to author
Forward
0 new messages