0
votes

So I have a cloud function that subscribes to a Pub/Sub topic, and the function interacts with a 3rd party service API that has a hard rate limit. This creates a problem, when the PubSub topic suddenly gets a surge of incoming traffic, the cloud function will be invoked frequently enough to go over the 3rd party API rate limit.

What's the best way to solve this problem? Am I able to throttle the PubSub topic or I have to implement some backoff logic in the cloud function? Thanks!

1

1 Answers

2
votes

You can control the scaling behavior of Cloud Functions to some degree. From the linked documentation:

You can set max instances for an individual function during deployment. Each function can have its own max instances limit. Functions scale independently of each other.

To set a max instances limit using the gcloud command-line tool, use the --max-instances flag at deploy time:

gcloud beta functions deploy FUNCTION_NAME --max-instances 10 FLAGS...

You will have to figure out how many concurrent instances are within the limits of this other API. If one instance is still able to generate too many requests, you will have to implement your own backoff, as you are not able to set the rate of execution, just the max number of allocated instances.