What is the difference between Amazon SNS and Amazon SQS?

后端 未结 6 717
广开言路
广开言路 2020-12-22 15:52

I don\'t understand when I would use SNS versus SQS, and why are they always coupled together?

6条回答
  •  春和景丽
    2020-12-22 16:04

    The answers on this thread are a little bit outdated, so I've decided to add my two cents to it:

    You can see SNS as a traditional topic which you can have multiple Subscribers. You can have heterogeneous subscribers for one given SNS topic, including Lambda and SQS, for example. You can also send SMS messages or even e-mails out of the box using SNS. One thing to consider in SNS is only one message (notification) is received at once, so you cannot take advantage from batching.

    SQS, on the other hand, is nothing but a Queue, where you store messages and subscribe one consumer (yes, you can have N consumers to one SQS queue, but it would get messy very quickly and way harder to manage considering all consumers would need to read the message at least once, so one is better off with SNS combined with SQS for this use case, where SNS would push notifications to N SQS queues and every queue would have one subscriber, only) to process these messages. As of Jun 28, 2018, AWS Supports Lambda Triggers for SQS, meaning you don't have to poll for messages anymore. Furthermore, you can configure a DLQ on your source SQS queue to send messages to in case of failure. In case of success, messages are automatically deleted (this is another great improvement), so you don't have to worry about the already processed messages being read again in case you forgot to delete them manually. I suggest taking a look at Lambda Retry Behaviour to better understand how it works. One great benefit of using SQS is that it enables batch processing. Each batch can contain up to 10 messages, so if 100 messages arrive at once in your SQS queue, then 10 Lambda functions will spin up (considering the default auto-scaling behaviour for Lambda) and they'll process these 100 messages (keep in mind this is the happy path as in practice, a few more Lambda functions could spin up reading less than the 10 messages in the batch, but you get the idea). If you posted these same 100 messages to SNS, however, 100 Lambda functions would spin up, unnecessarily increasing costs and using up your Lambda concurrency. However, if you are still running traditional servers (like EC2 instances), you will still need to poll for messages and manage them manually.

    You also have FIFO SQS queues, which guarantee the delivery order of the messages. This is not a supported trigger by Lambda, thus when choosing this type of Queue, keep in mind that polling is still necessary as well as having to delete the messages manually. SQS FIFO is also supported as an event source for Lambda as of November 2019

    Even though there's some overlap in their use cases, both SQS and SNS have their own spotlight.

    Use SNS if:

    • multiple subscribers is a requirement
    • sending SMS/E-mail out of the box is handy

    Use SQS if:

    • only one subscriber is needed
    • batching is important

提交回复
热议问题