GoogleCloudPlatform / jetty-runtime

Google Cloud Platform Jetty Docker image
Apache License 2.0
44 stars 33 forks source link

App Engine Flex Session Affinity #242

Open wesoos opened 6 years ago

wesoos commented 6 years ago

Hello, I believe the app engine flex uses the cloud load balancers underneath, correct? When will we be able to configure the load balancer to use session affinity? You can do it for a normal cloud load balancer in front of compute engine instances, why not app engine?

Using the gcp-datastore-sessions variable does not work in all cases, especially Vaadin apps where session affinity is required.

There are a lot of people wanting this feature!

Petikoch commented 5 years ago

+1 from me

gregw commented 5 years ago

Note also that if session affinity is available, we can add a more efficient memory cache for sessions in each instance that will reduce the load on the shared session store and improve semantics for co-located requests. The image can easily be modified to take advantage of affinity.

Petikoch commented 5 years ago

Just dicovered this https://cloud.google.com/blog/products/application-development/introducing-websockets-support-for-app-engine-flexible-environment and this https://cloud.google.com/appengine/docs/flexible/java/using-websockets-and-session-affinity (see bottom of this page)

It looks like in order to offer WebSockets on the flex environment "they" implemented session-affinity

app.yaml:

network:
  session_affinity: true
gregw commented 5 years ago

@Petikoch Thanks for that - I'll test that out (probably next week) and if it works will see what session options should be turned on in the image to best make use of that.

Petikoch commented 5 years ago

Excellent, thanks a lot @gregw

gregw commented 5 years ago

I've done some testing with the session_affinity and it appears to work fine. It does not trigger off the JSESSION_ID, rather it set's it's own GCLB cookie. The normal default in memory session should work OK with this setup, but if the affinity is not entirely sticky (if the GCLB cookie is lost, a node shuts down etc.) then session data will be lost. So a better setup would be to have an in memory cache fronting the gcloud datastore. This can be achieved with the following in your app.yaml:

env_variables:
    JETTY_MODULES_ENABLE: session-cache-hash,gcp-datastore,session-store-gcloud

This will avoid hitting the datastore on every request, sessions will only be written when dirty. I can see in my tests that objects in the sessions are coming back with the same value and same hashcode (so they have not been serialized deserialized)!

I will add this to the doco!

gregw commented 5 years ago

Pull request #260 documents this

menulis commented 5 years ago

Should it be enough to set session_affinity to true in app.yaml to enable session affinity or is there a specific Jetty module that also needs to be enabled with JETTY_MODULES_ENABLE? Does the GCP Load Balancer needs to be set up too? Documentation does not say anything about that and I cannot see any additional cookies (like the GCLB cookie mentioned by @gregw before) set in the response after having enabled session_affinity. Or is it just websockets for which App Engine flex supports session affinity?

gregw commented 5 years ago

If you set session_affinity to true, you will get session affinity implemented by the Google load balancer using its own additional cookie. This will work with no changes to Jetty.

However, one of the main points of session affinity is to allow efficient session management from an in memory session cache, so if you have affinity, you can turn this on with the session-cache-hash module. It's not necessary, but it's kind of the point!

menulis commented 5 years ago

Thanks for the clarification, @gregw. This was exactly how I thought. However, as I wrote, basic session infinity does not seem to work in my case. I set session_affinity under network: in app.yaml, but do not see any additional (GCLB or other) cookies added to the HTTP responses. This is why I thought that I may be missing one or another Jetty module or the Load Balancer has to be configured in some way (I do not have configured it at all, so as I understand, all App Engine Flex defaults should be functioning).