I want to be able to delay messages coming through an SQS FIFO queue by a defined period of time. This delay needs to be at the Message Group ID level. Please provide advice on your solution, or on my solutions.
Sequential message processing
Message processing no-faster-than 2 minutes per message per message group ID.
Messages 1, 2, 3 come in to a FIFO queue, for Message Group ID A, all 1 second after each other.
Let's say it's 10am.
I have 2 solutions in my head
Message delay won't solve the problem. If messages come in 1 second after each other, and processing time is brief (1 second on average), will just result in the messages being processed rapidly, just n seconds later than their arrival.
Setting a wait in the lambda that processes them This could run up costs on the lambda, even if the lambda was set to it's lowest memory allocation
This is a common ask... The ability to throttle the processing of messages, typically due to limitations imposed by an external system.
Unfortunately, native SQS functionality can't address this requirement. I would recommend against intentionally triggering Lambda failure as a form of 're-queuing' -- it could cause problems when there is a backlog of messages. Don't abuse a system designed for one purpose to try and make it fit another purpose.
Instead, I'd recommend implementing your own logic by putting the 'queue' in a database, then having an 'orchestrator' that determines when a message is able to be processed and invokes the Lambda function directly. The Lambda function would either need to update the database to say that it has finished processing the message, or it could return information to the orchestrator that then updates the database.
The orchestrator would either need to be 'continuously running code' (eg on EC2 or ECS) or it could be an AWS Lambda function triggered every minute or so.