amazon-web-servicesamazon-s3amazon-snsaws-lambda

How to limit the consuming rate from a topic?


Has anyone else solved the following problem?
I have SNS topic filled with events from S3 and there is Lambda function which is subscribed on this topic and when thousand of events are put to this topic, lambda function is throttled because of exceeding the limit of concurrency.
I don't want to request a limit increase for concurrent executions but I would decrease concurrent consuming from the topic, but I didn't find information how to do it. Thanks.


Solution

  • A couple of options regarding SNS:

    1. SNS Maximum Receive Rate

    Set the SNS Maximum Receive Rate. This will throttle the SNS messages sent to a subscriber, but may not be a great option if you have so many messages that they will be discarded before they can be processed. From the documentation:

    You can set the maximum number of messages per second that Amazon SNS sends to a subscribed endpoint by setting the Maximum receive rate setting. Amazon SNS holds messages that are awaiting delivery for up to an hour. Messages held for more than an hour are discarded.

    If you're only getting thousands of events at a time, setting the Maximum Receive Rate to Lambda's default concurrent execution limit of '100' might be worth a try.

    As @kndrk notes, this throttling is currently only available for HTTP/HTTPS subscribers to the SNS topic. To work around this, you can expose your lambda function via AWS API Gateway and subscribe that endpoint to the SNS topic, rather than the lambda function directly.

    1. Process from SQS

    Subscribe an SQS queue to the SNS topic and process messages from the queue, rather than directly from the sns topic. A single invocation of SQS ReceiveMessage can only handle 10 messages at a time, so that may be easier for you to throttle.


    It is also worth noting that you can publish S3 Events directly to AWS Lambda.