Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does Go handle concurrent request on Google App Engine

I'm a little confused on how Go handles concurrent requests on Google App Engine. So I'm hoping someone can provide some clarity.

Here are the facts that I have gathered:

  1. Go is single threaded on App Engine. - this is because it is possible to do arbitrary pointer arithmetic by creating race conditions with multiple threads

  2. Goroutines are multiplexed onto multiple OS threads so if one should block, such as while waiting for I/O, others continue to run.

  3. [App Engine has a] 10 concurrent limit [which] is enforced through a limit on concurrent threads on every runtime. Most of such cases, our scheduler will try to spin up a new instance.

If Go is single threaded on App Engine then point 3 is moot. This leaves 1 and 2. If Go on App Engine is single threaded and threads are required to continue execution while blocking for I/O, then it seems that an App Engine Go instance will block all goroutines while waiting on I/O.

Is this correct? If not how does Go's concurrency really work on App Engine?

To help quantify things. If I were to hold a connection open for 30 seconds. How may concurrent connections could a single AE Go instance maintain?

Thank you.

EDIT: here's the feature request which will allow Go Instance to handle more then 10 concurrent request Allow configurable limit of concurrent requests per instance. Please star it.

like image 829
Kyle Finley Avatar asked Jul 12 '12 07:07

Kyle Finley


People also ask

How does Google handle multiple requests?

An instance can handle multiple requests concurrently. The number of instances can be adjusted automatically as traffic changes. You can also change the number of concurrent requests an instance can handle by setting the max_concurrent_requests element in your app.

What is the difference between App Engine standard and flexible?

The standard environment can scale from zero instances up to thousands very quickly. In contrast, the flexible environment must have at least one instance running for each active version and can take longer to scale up in response to traffic. Standard environment uses a custom-designed autoscaling algorithm.

How long does it take my application to handle a request?

The average length of time it takes to hear back is one to two weeks or around 10-14 days after you submit your application materials. In contrast, certain jobs, like those for government positions could take as long as six to eight weeks to hear back.

What is Google App Engine Architecture?

The App Engine architecturePlatform as a Service (PaaS) to build and deploy scalable applications. Hosting facility in fully-managed data centers. A fully-managed, flexible environment platform for managing application server and infrastructure. Support in the form of popular development languages and developer tools.


2 Answers

A Go App Engine instance allows 10 concurrent requests, but only runs one CPU thread. In effect, multiple requests can be processed concurrently, but only one will be able to do CPU work at a time. If one request is, say, waiting for a datastore API call to return, another request is free to be processed by the same instance.

Your statement "If Go is single threaded on App Engine then point 3 is moot." is incorrect. There is still a limit of 10 concurrent in-flight requests to a single Go App Engine instance. The documentation is a bit loose with words when it talks about "threads".

like image 148
dsymonds Avatar answered Sep 19 '22 12:09

dsymonds


I must admit I have no inside knowledge of AppEngine. This is all speculation and guessing, but I think it is somewhat reasonable.

Your app will never reach the ten thread limit. This is because there are very few reasons for threads to be created. First, the maximum number of goroutines running at once is set to one to enforce memory safety. Second, unlike a normal go program, app engine does not actually need to make syscalls. The only time it does is for networking. All IO in appengine can be muxed into one epoll thread. This means all you need is two threads at any given time. You can then add a third or forth thread in case every once in a while you need to run other syscalls such as allocating memory and accepting/closing connections. These are fast syscalls that block for a very small amount of time. Even with these other syscalls, you are still very far from the ten thread limit.

Concurrency is not affected because in the end, everything you do in appengine boils down to waiting for something over the network to return. You do not need multiple threads to be doing many things at once.

like image 42
Stephen Weinberg Avatar answered Sep 20 '22 12:09

Stephen Weinberg