I'm trying to understand whether I need SQS in my workflow if someone can help explain. In my app, when an action is taken, it submits info to SNS topic which invokes Lambda to do some processing. This is working great as it is.
When I do research online, it seems that people are using SQS in this stack as well where SNS would put info on SQS and then SQS would then invoke Lambda.
I guess what I'm trying to understand is the need for SQS in this. What value does that add? In other words, what am I losing by invoking my Lambda directly from SNS?
Having SQS in between SNS and Lambda allows reprocessing older unprocessed events in case Lambda fails to process. SQS allows to put a delay, so that message gets processed after some time, it may be useful in the scenario where data takes time to be available.
SQS is mainly used to decouple applications or integrate applications. SNS is used to broadcast messages and it's up to the receivers how they interpret and process those messages.
Using Amazon SNS and Amazon SQS together, messages can be delivered to applications that require immediate notification of an event, and also persisted in an Amazon SQS queue for other applications to process at a later time.
Sending Amazon SNS messages to an Amazon SQS queue in a different account. You can publish a notification to an Amazon SNS topic with one or more subscriptions to Amazon SQS queues in another account.
Primary advantage of having a SQS in between SNS and Lambda is Reprocessing. Assume that the Lambda fails to process certain event for some reason (e.g. timeout or lack of memory footprint), you can increase the timeout (to max 5 minutes) or memory (to max of 1.5GB) and restart your polling and you can reprocess the older events.
This would not be possible in case of SNS to Lambda, wherein if Lambda fails the event is lost. And even if you configure DLQ you would still have to make provisions for reading that separately and processing the message
So if your events are critical and you don't want to miss out on them, then go for SNS - SQS - Lambda
The other advantage of having SQS is cost saving on Lambda invocations (Thanks @codesinthedark for bringing this up). You can have much better scaling and less cost, as it allows you to process messages in batches. So one lambda can be executed for a batch of 10 messages while in case of direct SNS each message would trigger a lambda invocation.
I think couple of things changed in 2019 and SQS can trigger lambda via event source mapping which is mentioned by @alexs. Related blog post: https://aws.amazon.com/about-aws/whats-new/2018/04/aws-lambda-now-supports-amazon-sqs-as-event-source/
To summarize, you can use SQS to lambda with following benefits:
you can choose to use SNS:
In both the cases, there can be duplicate messages(in the cases of retry) and there cannot be order guarantees. If you need one, consider Kinesis streams.
You can now use SQS as en event source
AWS Lambda Adds Amazon Simple Queue Service to Supported Event Sources
Adding to @Arafat Nalkhande's answer here are few benefits of SQS's lambda
In SQS we can put a delay, so that message gets processed after some time, it may be useful in the scenario where data takes time to be available.
SQS can serve as a contingency store, lets say downstream services are unavailable, message can be retained in sqs for 15 days.
SQS does not invoke Lambda. SQS cannot invoke anything. People using Lambda with SQS are running Lambda on an event timer, like once a minute, and every time the function runs it polls SQS to see if there is a message to process.
If you don't need to queue things up and prevent too many Lambda functions from running concurrently then you don't need a queue system like SQS.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With