问题
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 doe that add or in other words, what am I losing by invoking my LAMBDA directly from SNS?
回答1:
The only 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
Having said that SQS cannot trigger lambda like SNS. You will have to poll the SQS at frequent intervals
EDIT :
AWS announced SQS triggering Lambda support on 28 JUN 2018. So no need anymore for polling the queue at frequent intervals. New items to SQS can trigger Lambda
回答2:
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.
回答3:
You can now use SQS as en event source
AWS Lambda Adds Amazon Simple Queue Service to Supported Event Sources
回答4:
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:
- Reprocessing events in case of failure and configure how many times a message should be retried before you give up (receive count)
- Longer retention period
- Typically chosen in the scenarios where there is a long running job and the lambda polls one by one from job queue.
you can choose to use SNS:
- If you need to fanout a single message to multiple destinations, say X message should be processed by Y and Z applications. I feel this is the biggest advantage, and if you want reliability in this, you can couple SNS and SQS together.
- You do not care about lost messages. Remember that there are still retry stratergies when using SNS(linear, geometric, exponential etc)
- Typically used in the cases where you can ingest/process messages faster. This can sometimes be a problem as well; imagine a scenario where there is a SNS notification for every email that your business receives and you dont have enough lambda concurrency to process all of them. You can solve this by putting an SQS to consume at your own pace.
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.
回答5:
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.
来源:https://stackoverflow.com/questions/42656485/sns-to-lambda-vs-sns-to-sqs-to-lambda