My application wishes to have:
Automatic scalability
- I want App Engine to spin up new instances of my app when traffic increases
- When instances become idle, I want App Engine to shut them down
Client/server affinity
- After an initial client->server HTTP request, I want clients to be able to connect to the same appserver, so that the appserver can maintain a bunch of client state
- State may be updated frequently (in order to support real-time interactions), so memcache+datastore based persistence is not desirable.
- The server may need to make decisions based on the state of multiple clients, e.g. real time multi-player game
How can I accomplish this?
You can achieve these goals using App Engine backends (long-running, configurable, addressable, persistent servers):
Python implementation
Configure a backend to be both public and dynamic
In addition to deploying your app in usual way:
remember to deploy you backend:
For the initial connection, have your client use the non-instance specific backend hostname, e.g.:
App Engine will route your request to available backend instance, after optionally starting a new backend instance.
In the request handling code on the server, use the backends API to determine which instance is handling the request and return to the client a instance specific URL.
Client makes subsequent connections to the instance specific backend URL:
Note: when using https be sure to replace subdomain dots with
-dot-
in order to avoid SSL certificate issues.Limitations
Automatic scalability is what AppEngine does best and what most distinguishes it from other cloud-hosting providers. Both of your requirements are met without doing any extra work on your part.
AppEngine does not have a concept of individual servers. You can't think in such terms in creating a good, scaleable AppEngine app. You can, however, store client state between requests in Memcache or the Datastore, which are shared among all instances of application code.
To complement Adam's good answer: you don't need server affinity in GAE, because data stored in the HTTP session is not held in memory, but in the persistent datastore. So any server will find what any other server previously stored in the session. See the documentation: