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


当前回答

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

其他回答

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

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

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

来自AWS文档:

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

扇出到Amazon SQS队列

简单来说,

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

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

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