Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How To Rate-Limit Google Cloud Pub/Sub Queue

I'm using Google's Pub/Sub queue to handle messages between services. Some of the subscribers connect to rate-limit APIs.

For example, I'm pushing street addresses onto a pub/sub topic. I have a Cloud function which subscribes (via push) to that topic, and calls out to an external rate-limited geocoding service. Ideally, my street addresses could be pushed onto the topic with no delay, and the topic would retain those messages - calling the subscriber in a rate-limited fashion.

Is there anyway to configure such a delay, or a message distribution rate limit? Increasing the Ack window doesn't really help: I've architected this system to prevent long-running functions.

like image 853
chris stamper Avatar asked Jul 13 '17 17:07

chris stamper


People also ask

Can Pubsub hold millions of messages?

Resource limitsThere is no limit on the number of retained messages.

What is the maximum retention duration for a message in pub sub?

The default message retention duration is 7 days and the default expiration period is 31 days. To create a subscription with retention of acked messages enabled, follow these steps: In the Google Cloud console, go to the Pub/Sub subscriptions page. Click Create subscription.

Is Pubsub scalable?

The system is designed to be horizontally scalable, where an increase in the number of topics, subscriptions, or messages can be handled by increasing the number of instances of running servers. Pub/Sub servers run in all GCP regions around the world.


2 Answers

An aproach to solve your problem is by using: async.queue

There you have a concurrency attribute wich you can manage the rate limit.

// create a queue object with concurrency 2
var q = async.queue(function(task, callback) {
    console.log('hello ' + task.name);
    callback();
}, 2);

// assign a callback
q.drain = function() {
    console.log('all items have been processed');
};

// add some items to the queue
q.push({name: 'foo'}, function(err) {
    console.log('finished processing foo');
});

// quoted from async documentation
like image 94
Alexandru Olaru Avatar answered Sep 19 '22 13:09

Alexandru Olaru


Because there's no answer so far describing workarounds, I'm going to answer this now by stating that there is currently no way to do this. There are workarounds (see the comments on the question that explain how to create a queueing system using Cloud Scheduler), but there's no way to just set a setting on a pull subscription that creates a rate limit between it and its topic.

I opened a feature request for this though. Please speak up on the tracked issue if you'd like this feature.

https://issuetracker.google.com/issues/197906331

like image 33
Matt Welke Avatar answered Sep 16 '22 13:09

Matt Welke