aws-lambdaamazon-sqsamazon-snspollingevent-driven

What are the benefits of using SNS to SQS to Lambda compared to having SNS to SQS to SQSConsumers?


We have a SQS queue subscribe to SNS Topic which publishes about 1-5 million events per month. I want to know which of these combinations - SNS->SQS->Lambda vs SNS->SQS->SQSConsumer would benefit me for such use-cases.

I understand the maintain difference between them is Event driven Vs Pull Driven. A lambda is triggered for each message that comes into a queue so that is an event driven architecture, an SQSConsumer has to constantly poll for messages. You have to have constant up time for a poller like that vs a lambda that is only triggered once a message is received.

I have couple of questions here :

  1. Why SNS->SQS-> Lambda is considered Event driven, when lambda has to poll the SQS queue similar to what SQSConsumer does?
  2. Followup : When Lambda is also constantly polling, then why lambda is considered to be more cost efficient than SQSConsumer?

Solution

  • If you ignore the 'internals' of how Amazon SQS with AWS Lambda is implemented, simply think of it as SQS directly triggering the Lambda function. This is a serverless model, whereas using an SQS consumer requires code to be running on a computer somewhere. Lambda will automatically scale, so it is more cost effective than having computing infrastructure waiting around for events (and costing money even when it isn't used).

    So, it's really a decision about whether to use a serverless architecture.

    You could also subscribe the AWS Lambda function direction to the Amazon SNS topic, without using Amazon SQS in the middle.