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


当前回答

以下是两者的对比:

实体类型

队列(类似于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.

其他回答

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

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

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

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

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

简单来说,

SNS -发送消息给订阅者使用推送机制,而不需要拉。 SQS——它是分布式应用程序使用的消息队列服务,用于通过轮询模型交换消息,并可用于分离发送和接收组件。

一种常见的模式是使用SNS将消息发布到Amazon SQS队列,以便可靠地将消息异步发送到一个或多个系统组件。

参考亚马逊SNS常见问题解答。

以下是两者的对比:

实体类型

队列(类似于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.

以下是AWS上主要消息传递技术(SQS, SNS, +EventBridge)之间的主要区别。为了选择特定的AWS服务,我们应该了解该服务提供的功能以及与其他服务的比较。

下图总结了该服务之间的主要相似点和不同点。

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用于不同的用例。但是,您可以在某些场景中将它们一起使用。