Home > Net >  How can I ensure a persistent connection to a specific GCP Cloud Run instance?
How can I ensure a persistent connection to a specific GCP Cloud Run instance?

Time:06-25

I've built an app (with flask, flask-login and dash) on GCP Cloud Run. The app allows users to login, look at some fancy dashboards and leave comments on certain pages. It works great performance-wise: instances spin up quickly for users with minimal lag, the BigQuery interface I built works great and pub/sub messages sent from user interactions do exactly what they're supposed to do.

The only issue I'm having right now is that there's something weird about which instance of a container a user connects to. What will often happen is a user will login to my app via their browser successfully, and then when navigating to another password-protected page will receive a 401 error (seemingly randomly).

My belief is that this behavior is happening because the navigation request (clicking a link to another password protected page) from the user to another password protected page spins up another Cloud Run instance. Is there any way to force Cloud Run to maintain a specific instance of my container for a given request? So that if a user logs in and then navigates GCP doesn't take the next request and decide to autoscale?

I've experimented with setting the maximum number of requests for the app's frontend container to 1 but it doesn't seem to improve this behavior which happens sporadically throughout a given user's session.

To clarify, the frontend part of the app is still usable, but it is an annoying user experience to constantly have to login again.

Any help or guidance is appreciated!

CodePudding user response:

The answer was as simple as turning on session affinity per @DazWilkin 's comment.

What I did:

  1. Went to the Cloud Run dashboard on GCP and selected the service of interest

  2. Clicked "Edit and Deploy New Revision"

  3. Went to the "Connections"

  4. Checked the box next to the "Session affinity" preview feature

  5. Clicked deploy

This ended up completely solving the problem!

  • Related