typescriptamazon-web-servicesamazon-sqsaws-cdkthroughput

How Do I Limit A Standard FIFO Queue ThroughPut In The AWS CDK?


I have created a FIFO queue, but would like to limit the number of requests it can make, so as not to overload the server it is sending those requests to.

CDK Code:

this.intoMagento = new Queue(this, "intoAPI", {
  queueName: `intoAPI.fifo`,
  fifo: true,
  contentBasedDeduplication: true,
  visibilityTimeout: Duration.minutes(5),
  fifoThroughputLimit: FifoThroughputLimit.PER_QUEUE,
});

Whilst I've limited the throughput PER_QUEUE, how do I tell it I only want to do 600 HTTP POST requests per minute? Do I do it in my Lambda Queue Consumer?

Also, is FifoThroughputLimit.PER_QUEUE only appropriate for high throughout SQS queues, or can it apply to standard queues too? I have many message groups.


Solution

  • SQS Queue throughput is not a fine-grained, user-determined setting*. Rather, it's a consequence of a mix of factors, including Queue type quotas, consumer count, batching, and message partition patterns.

    The Lambda Event Source Mapping integration with SQS pulls messages as fast as possible. However, here are a couple ways to limit your Lambda consumers' message consumption:

    1. The quick-and-dirty approach is to cap the number of concurrent Lambda consumers with the Event Source Mapping's MaximumConcurrency setting. If you're lucky, a MaximumConcurrency set to the lower limit of 2 (and batchSize set to 1) will slow processing to a rate that is acceptably close to your desired limit. I guess you could also periodically enable/disable the integration on a schedule if you need an additional lever.

    2. If you want finer-grained throughput control, you'll need to give up the conveniences of the Event Source Mapping integration. Trigger the Lambda on a schedule with EventBridge. You'll have more precise control over messages/hour, but will need to pull and delete messages from the queue manually. If you have a hard 600/hr limit, you'll additionally need to track of the hour-to-date processed message count (say, in a DynamoDB table) and exit early from the Lambda if the limit has been reached for a given period.


    * Whilst the name might suggest otherwise, the CDK Queue construct's fifoThroughputLimit is not a speed limit setting. Rather, it is part of the setup to enable High-throughput on FIFO Queues.