GCP cloud run send a request to all running instances

Viewed 428

I have a rest API running on cloud run that implements a cache, which needs to be cleared maybe once a week when I update a certain property in the database. Is there any way to send a HTTP request to all running instances of my application? Right now my understanding is even if I send multiple requests and there are 5 instances, it could all go to one instance. So is there a way to do this?

2 Answers

Let's go back to basics:

Cloud Run instances start based on a revision/image.

If you have the above use case, where suppose you have 5 instances running and you suddenly need to re-start them as restarting the instances resolves your use case, such as clearing/rebuilding the cache, what you need to do is:

Trigger a change in the service/config, so a new revision gets created.

This will automatically replace, so will stop and relaunch all your instances on the fly.

You have a couple of options here, choose which is suitable for you:

  1. if you have your services defined as yaml files, the easiest is to run the replace service command:

    gcloud beta run services replace myservice.yaml

  2. otherwise add an Environmental variable like a date that you increase, and this will yield a new revision (as a change in Env means new config, new revision) read more.

    gcloud run services update SERVICE --update-env-vars KEY1=VALUE1,KEY2=VALUE2

As these operations are executed, you will see a new revision created, and your active instances will be replaced on their next request with fresh new instances that will build the new cache.

You can't reach directly all the active instance, it's the magic (and the tradeoff) of serverless: you don't really know what is running!! If you implement cache on Cloud Run, you need a way to invalidate it.

  • Either based on duration; when expired, refresh it
  • Or by invalidation. But you can't on Cloud Run.

The other way to see this use case is that you have a cache shared between all your instance, and thus you need a shared cache, something like memory store. You can have only 1 Cloud Run instance which invalidate it and recreate it and all the other instances will use it.

Related