我什么时候使用SNS和SQS,为什么它们总是耦合在一起?


当前回答

SNS是一个分布式发布-订阅系统。当信息由发布者发送到SNS时,就会推送给订阅者。

SQS is distributed queuing system. Messages are not pushed to receivers. Receivers have to poll or pull messages from SQS. Messages can't be received by multiple receivers at the same time. Any one receiver can receive a message, process and delete it. Other receivers do not receive the same message later. Polling inherently introduces some latency in message delivery in SQS unlike SNS where messages are immediately pushed to subscribers. SNS supports several end points such as email, SMS, HTTP end point and SQS. If you want unknown number and type of subscribers to receive messages, you need SNS.

你不必总是把SNS和SQS结合在一起。你可以让SNS发送消息到电子邮件,短信或HTTP端点除了SQS。将SNS与SQS耦合有很多优点。您可能不希望外部服务连接到您的主机(防火墙可能会阻止所有来自外部的传入连接到您的主机)。

你的终点可能会因为大量的消息而消失。电子邮件和短信可能不是你快速处理信息的选择。通过将SNS与SQS耦合,您可以按照自己的节奏接收消息。它允许客户端离线,容忍网络和主机故障。您还可以实现有保证的交付。如果将SNS配置为向HTTP端点或电子邮件或SMS发送消息,则多次发送消息失败可能导致消息被丢弃。

SQS主要用于解耦应用程序或集成应用程序。消息可以在SQS中存储短时间(最多14天)。SNS将多个消息副本分发给多个订阅者。例如,假设您希望将应用程序生成的数据复制到多个存储系统。您可以使用SNS并将此数据发送给多个订阅者,每个订阅者将其接收到的消息复制到不同的存储系统(S3、主机上的硬盘、数据库等)。

其他回答

SNS和SQS之间有一些关键的区别:

SNS supports A2A and A2P communication, while SQS supports only A2A communication. SNS is a pub/sub system, while SQS is a queuing system. You'd typically use SNS to send the same message to multiple consumers via topics. In comparison, in most scenarios, each message in an SQS queue is processed by only one consumer. With SQS, messages are delivered through a long polling (pull) mechanism, while SNS uses a push mechanism to immediately deliver messages to subscribed endpoints. SNS is typically used for applications that need real time notifications, while SQS is more suited for message processing use cases. SNS does not persist messages - it delivers them to subscribers that are present, and then deletes them. In comparison, SQS can persist messages (from 1 minute to 14 days).

单独地,Amazon SQS和SNS用于不同的用例。但是,您可以在某些场景中将它们一起使用。

你可以看到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:

只需要一个订阅者 批处理很重要

来自AWS文档:

Amazon SNS允许应用程序发送时间紧迫的消息到 多个订阅者通过“推送”机制,消除了需求 定期检查或“轮询”更新。 Amazon SQS是分布式应用程序使用的消息队列服务 通过轮询模型交换消息,并可用于 分离发送和接收组件——不需要每个组件 组件要同时可用。

扇出到Amazon 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.

SQS和SNS耦合的一个原因是数据处理管道。

假设你正在生成三种产品,产品B和C都是从相同的中间产品a衍生而来的。对于每种产品(即管道的每个部分),你设置:

用于生成产品的计算资源(可能是lambda函数,或虚拟机集群,或自动伸缩kubernetes作业)。 用于跨计算资源对工作进行分区的队列(描述需要执行的工作单元)(以便每个工作单元只处理一次,但是可以以并行和彼此异步的方式分别处理单独的工作单元)。 新闻提要(宣布已生成的输出)。

然后进行排列,使B和C的输入队列都订阅A的输出通知。

这使得管道在基础设施级别上是模块化的。不同的管道阶段可以使用不同的硬件资源(例如,阶段B可能内存非常密集,但其他两个阶段可以使用更便宜的硬件/服务来执行),而不是拥有一个单独的服务器应用程序来同时生成所有三个产品。这也使得迭代一个管道段的开发变得更容易,而不会中断其他产品的交付。