我什么时候使用SNS和SQS,为什么它们总是耦合在一起?
当前回答
AWS SNS是一个发布者订阅者网络,订阅者可以在其中订阅主题,并在发布者发布该主题时接收消息。
AWS SQS是一个队列服务,它将消息存储在队列中。SQS不能传递任何消息,需要一个外部服务(lambda、EC2等)来轮询SQS并从SQS获取消息。
由于多种原因,可以将SNS和SQS结合使用。
There may be different kinds of subscribers where some need the immediate delivery of messages, where some would require the message to persist, for later usage via polling. See this link. The "Fanout Pattern." This is for the asynchronous processing of messages. When a message is published to SNS, it can distribute it to multiple SQS queues in parallel. This can be great when loading thumbnails in an application in parallel, when images are being published. See this link. Persistent storage. When a service that is going to process a message is not reliable. In a case like this, if SNS pushes a notification to a Service, and that service is unavailable, then the notification will be lost. Therefore we can use SQS as a persistent storage and then process it afterwards.
其他回答
AWS SNS是一个发布者订阅者网络,订阅者可以在其中订阅主题,并在发布者发布该主题时接收消息。
AWS SQS是一个队列服务,它将消息存储在队列中。SQS不能传递任何消息,需要一个外部服务(lambda、EC2等)来轮询SQS并从SQS获取消息。
由于多种原因,可以将SNS和SQS结合使用。
There may be different kinds of subscribers where some need the immediate delivery of messages, where some would require the message to persist, for later usage via polling. See this link. The "Fanout Pattern." This is for the asynchronous processing of messages. When a message is published to SNS, it can distribute it to multiple SQS queues in parallel. This can be great when loading thumbnails in an application in parallel, when images are being published. See this link. Persistent storage. When a service that is going to process a message is not reliable. In a case like this, if SNS pushes a notification to a Service, and that service is unavailable, then the notification will be lost. Therefore we can use SQS as a persistent storage and then process it afterwards.
简单来说,
SNS -发送消息给订阅者使用推送机制,而不需要拉。 SQS——它是分布式应用程序使用的消息队列服务,用于通过轮询模型交换消息,并可用于分离发送和接收组件。
一种常见的模式是使用SNS将消息发布到Amazon SQS队列,以便可靠地将消息异步发送到一个或多个系统组件。
参考亚马逊SNS常见问题解答。
以下是AWS上主要消息传递技术(SQS, SNS, +EventBridge)之间的主要区别。为了选择特定的AWS服务,我们应该了解该服务提供的功能以及与其他服务的比较。
下图总结了该服务之间的主要相似点和不同点。
以下是两者的对比:
实体类型
队列(类似于JMS) SNS: Topic (Pub/Sub系统)
信息消费
SQS:拉取机制——消费者从SQS中轮询和拉取消息 SNS:推送机制——SNS向消费者推送消息
用例
SQS:解耦两个应用程序并允许并行异步处理 SNS: Fanout -以多种方式处理相同的消息
持久性
SQS:如果没有消费者可用,消息将被持久化一段时间(可配置的)(最多两周),因此当消息添加到队列时,消费者不必处于空闲状态。 社交网络:没有持久性。在消息到达时出现的任何一个消费者都将获得消息并删除消息。如果没有可用的消费者,则在几次重试后消息将丢失。
消费者类型
SQS:所有消费者通常都是相同的,因此以完全相同的方式处理消息(每个消息由一个消费者处理一次,尽管在极少数情况下消息可能会被重新发送) SNS:消费者可能以不同的方式处理消息
示例应用程序
SQS: Jobs framework: The Jobs are submitted to SQS and the consumers at the other end can process the jobs asynchronously. If the job frequency increases, the number of consumers can simply be increased to achieve better throughput. SNS: Image processing. If someone uploads an image to S3 then watermark that image, create a thumbnail and also send a Thank You email. In that case S3 can publish notifications to an SNS topic with three consumers listening to it. The first one watermarks the image, the second one creates a thumbnail and the third one sends a Thank You email. All of them receive the same message (image URL) and do their processing in parallel.
你可以看到SNS作为一个传统的主题,你可以有多个订阅者。对于一个给定的SNS主题,您可以拥有不同的订阅者,例如,包括Lambda和SQS。你也可以使用SNS发送短信甚至电子邮件。在SNS中需要考虑的一件事是,一次只能收到一条消息(通知),所以你不能利用批处理的优势。
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 any more.
此外,您还可以在源SQS队列上配置DLQ,以便在发生故障时向其发送消息。在成功的情况下,消息将被自动删除(这是另一个很大的改进),因此在忘记手动删除消息的情况下,您不必担心已经处理过的消息会再次被读取。我建议看看Lambda重试行为,以更好地理解它是如何工作的。
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.
但是,如果您仍然在运行传统的服务器(如EC2实例),则仍然需要轮询消息并手动管理它们。
您还有FIFO SQS队列,它保证消息的传递顺序。截至2019年11月,SQS FIFO还支持作为Lambda的事件源
尽管它们的用例有一些重叠,但SQS和SNS都有自己的焦点。
在以下情况使用社交网络:
多个订阅者是必需的 用手机发送短信或电子邮件很方便
在以下情况下使用SQS:
只需要一个订阅者 批处理很重要
推荐文章
- 警告:未受保护的私钥文件!当尝试SSH到Amazon EC2实例时
- 使用boto3连接CloudFront时,如何选择AWS配置文件
- 在亚马逊云服务器上设置FTP
- 无法将图像推送到Amazon ECR -由于“没有基本的身份验证凭据”而失败
- 如何测试AWS命令行工具的凭据
- 将Keypair添加到现有的EC2实例中
- AWS S3:您试图访问的桶必须使用指定的端点寻址
- 你会因为EC2上的“停止”实例而被收费吗?
- 下载一个已经上传的Lambda函数
- S3 - Access-Control-Allow-Origin头
- 何时使用Amazon Cloudfront或S3
- 如何处理错误与boto3?
- 什么数据存储在亚马逊EC2实例的临时存储?
- boto3 client NoRegionError:只能在某些时候指定区域错误
- AWS ssh访问“权限被拒绝(publickey)”问题