当前位置: 首页 > 工具软件 > Sqs4J > 使用案例 >

Amazon SNS和Amazon SQS有什么区别?

欧桐
2023-12-01

本文翻译自:What is the difference between Amazon SNS and Amazon SQS?

我不明白何时使用SNS与SQS,为什么它们总是耦合在一起?


#1楼

参考:https://stackoom.com/question/vP6j/Amazon-SNS和Amazon-SQS有什么区别


#2楼

SNS is a distributed publish-subscribe system. SNS是分布式发布 - 订阅系统。 Messages are pushed to subscribers as and when they are sent by publishers to SNS. 当发布者消息发送给SNS时,消息将被推送给订阅者。

SQS is distributed queuing system. SQS是分布式排队系统。 Messages are NOT pushed to receivers. 消息不会被推送到接收者。 Receivers have to poll or pull messages from SQS . 接收方必须从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不同,轮询固有地在SQS中引入了一些消息传递延迟,其中消息被立即推送给订户。 SNS supports several end points such as email, sms, http end point and SQS. SNS支持多个端点,如电子邮件,短信,http端点和SQS。 If you want unknown number and type of subscribers to receive messages, you need SNS. 如果您想要未知的订户数量和类型来接收消息,则需要SNS。

You don't have to couple SNS and SQS always. 您不必总是将SNS和SQS结合在一起。 You can have SNS send messages to email, sms or http end point apart from SQS. 除了SQS,您可以让SNS向电子邮件,短信或http端点发送消息。 There are advantages to coupling SNS with SQS. 将SNS与SQS耦合具有优势。 You may not want an external service to make connections to your hosts (firewall may block all incoming connections to your host from outside). 您可能不希望外部服务与主机建立连接(防火墙可能会阻止从外部到主机的所有传入连接)。 Your end point may just die because of heavy volume of messages. 你的终点可能会因为大量的消息而死亡。 Email and SMS maybe not your choice of processing messages quickly. 电子邮件和短信可能不是您快速处理邮件的选择。 By coupling SNS with SQS, you can receive messages at your pace. 通过将SNS与SQS耦合,您可以按照自己的节奏接收消息。 It allows clients to be offline, tolerant to network and host failures. 它允许客户端脱机,容忍网络和主机故障。 You also achieve guaranteed delivery. 您也可以保证交货。 If you configure SNS to send messages to an http end point or email or SMS, several failures to send message may result in message being dropped. 如果将SNS配置为将消息发送到http端点或电子邮件或SMS,则若干发送消息失败可能会导致消息被丢弃。

SQS is mainly used to decouple applications or integrate applications. SQS主要用于解耦应用程序或集成应用程序。 Messages can be stored in SQS for short duration of time (max 14 days). 消息可以在SQS中存储很短的时间(最多14天)。 SNS distributes several copies of message to several subscribers. SNS向几个订户分发了几个消息副本。 For example, lets say you want to replicate data generated by an application to several storage systems. 例如,假设您要将应用程序生成的数据复制到多个存储系统。 You could use SNS and send this data to multiple subscribers, each replicating the messages it receives to different storage systems (s3, hard disk on your host, database, etc.). 您可以使用SNS并将此数据发送给多个订阅者,每个订阅者将收到的消息复制到不同的存储系统(s3,主机上的硬盘,数据库等)。


#3楼

From aws doc: 来自aws doc:

Amazon SNS allows applications to send time-critical messages to multiple subscribers through a “push” mechanism, eliminating the need to periodically check or “poll” for updates. Amazon SNS允许应用程序通过“推送”机制向多个订户发送时间关键消息,从而无需定期检查或“轮询”更新。

Amazon SQS is a message queue service used by distributed applications to exchange messages through a polling model, and can be used to decouple sending and receiving components—without requiring each component to be concurrently available. Amazon SQS是分布式应用程序用于通过轮询模型交换消息的消息队列服务,可用于解除发送和接收组件 - 无需每个组件同时可用。

http://docs.aws.amazon.com/sns/latest/dg/SendMessageToSQS.html http://docs.aws.amazon.com/sns/latest/dg/SendMessageToSQS.html


#4楼

AWS SNS is a publisher subscriber network, where subscribers can subscribe to topics and will receive messages whenever a publisher publishes to that topic. AWS SNS是一个发布者订阅者网络,订阅者可以订阅主题,并在发布者发布到该主题时接收消息。

AWS SQS is a queue service, which stores messages in a queue. AWS SQS是一种队列服务,用于将消息存储在队列中。 SQS cannot deliver any messages, where an external service (lambda, EC2 etc) is needed to poll SQS and grab messages from SQS. SQS无法传递任何消息,其中需要外部服务(lambda,EC2等)来轮询SQS并从SQS获取消息。

SNS and SQS can be used together for multiple reasons. SNS和SQS可以出于多种原因一起使用。

  1. 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 . 看到这个链接

  2. 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. 当消息发布到SNS时,它可以将其并行分发到多个SQS队列。 This can be great when loading thumbnails in an application in parallel, when images are being published. 在发布图像时,并行地在应用程序中加载缩略图时,这可能很棒。 See this link . 看到这个链接

  3. 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. 在这种情况下,如果SNS将通知推送到服务,并且该服务不可用,则通知将丢失。 Therefore we can use SQS as a persistent storage and then process it afterwards. 因此,我们可以使用SQS作为持久存储,然后再处理它。


#5楼

Here's a comparison of the two: 以下是两者的比较:

Entity Type 实体类型

  • SQS : Queue (Similar to JMS) SQS:队列(类似于JMS)
  • SNS : Topic (Pub/Sub system) SNS:主题(发布/订阅系统)

Message consumption 消息消费

  • SQS : Pull Mechanism - Consumers poll and pull messages from SQS SQS:拉动机制 - 消费者从SQS轮询和拉取消息
  • SNS : Push Mechanism - SNS Pushes messages to consumers SNS:推送机制 - SNS将消息推送给消费者

Use Case 用例

  • SQS : Decoupling 2 applications and allowing parallel asynchronous processing SQS:解耦2个应用程序并允许并行异步处理
  • SNS : Fanout - Processing the same message in multiple ways SNS:扇出 - 以多种方式处理相同的消息

Persistence 坚持

  • SQS : Messages are persisted for some (configurable) duration if no consumer is available SQS:如果没有可用的消费者,则消息会持续一段(可配置的)持续时间
  • SNS : No persistence. SNS:没有坚持。 Whichever consumer is present at the time of message arrival gets the message and the message is deleted. 在消息到达时存在的任何消费者获得消息并且消息被删除。 If no consumers are available then the message is lost. 如果没有可用的消费者,则消息将丢失。

Consumer Type 消费者类型

  • SQS : All the consumers are supposed to be identical and hence process the messages in exact same way SQS:所有消费者都应该是相同的,因此以完全相同的方式处理消息
  • SNS : The consumers might process the messages in different ways SNS:消费者可能以不同的方式处理消息

Sample applications 样品申请

  • SQS : Jobs framework: The Jobs are submitted to SQS and the consumers at the other end can process the jobs asynchronously. SQS:作业框架:作业被提交给SQS,而另一端的消费者可以异步处理作业。 If the job frequency increases, the number of consumers can simply be increased to achieve better throughput. 如果作业频率增加,则可以简单地增加消费者的数量以实现更好的吞吐量。
  • SNS : Image processing. SNS:图像处理。 If someone uploads an image to S3 then watermark that image, create a thumbnail and also send a Thank You email. 如果有人将图像上传到S3,则为该图像添加水印,创建缩略图并发送感谢信。 In that case S3 can publish notifications to a SNS Topic with 3 consumers listening to it. 在这种情况下,S3可以向SNS主题发布通知,其中3个消费者正在收听它。 1st one watermarks the image, 2nd one creates a thumbnail and the 3rd one sends a Thank You email. 第一个水印图像,第二个创建缩略图,第三个发送谢谢你的电子邮件。 All of them receive the same message (image URL) and do their processing in parallel. 他们都收到相同的消息(图像URL)并进行并行处理。

#6楼

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. 您可以将SNS视为一个传统主题,您可以拥有多个订阅者。 You can have heterogeneous subscribers for one given SNS topic, including Lambda and SQS, for example. 例如,您可以为一个给定的SNS主题拥有异构订阅者,包括Lambda和SQS。 You can also send SMS messages or even e-mails out of the box using SNS. 您还可以使用SNS发送短信甚至是开箱即用的电子邮件。 One thing to consider in SNS is only one message (notification) is received at once, so you cannot take advantage from batching. 在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. 另一方面, SQS只是一个队列,你可以在其中存储消息并订阅一个消费者(是的,你可以让N个消费者加入一个SQS队列,但考虑到所有消费者的意愿,它会很快变得混乱而且难以管理需要至少读取一次消息,因此在这个用例中SNS结合SQS会更好,SNS会将通知推送到N个SQS队列,每个队列只有一个用户来处理这些消息。 As of Jun 28, 2018, AWS Supports Lambda Triggers for SQS , meaning you don't have to poll for messages anymore. 自2018年6月28日起, AWS支持SQS的Lambda触发器 ,这意味着您不必再轮询消息。 Furthermore, you can configure a DLQ on your source SQS queue to send messages to in case of failure. 此外,您可以在源SQS队列上配置DLQ,以便在发生故障时发送消息。 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. 我建议看看Lambda Retry Behavior以更好地理解它是如何工作的。 One great benefit of using SQS is that it enables batch processing. 使用SQS的一大好处是它可以实现批处理。 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). 每个批次最多可包含10条消息,因此如果在SQS队列中一次有100条消息到达,那么10个Lambda函数将会启动(考虑到Lambda的默认自动缩放行为)并且它们将处理这100条消息(保留在请注意,这是一条快乐的道路,在实践中,一些Lambda函数可以在批量中读取少于10条消息,但是你得到了这个想法。 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. 但是,如果您向SNS发布了这100条相同的消息,那么100个Lambda函数将会启动,不必要地增加成本并耗尽您的Lambda并发。 However, if you are still running traditional servers (like EC2 instances), you will still need to poll for messages and manage them manually. 但是,如果您仍在运行传统服务器(如EC2实例),则仍需要轮询消息并手动管理它们。

You also have FIFO SQS queues , which guarantee the delivery order of the messages. 您还有FIFO SQS队列 ,它保证了消息的传递顺序。 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. 这不是Lambda支持的触发器,因此在选择此类型的Queue时,请记住仍然需要轮询以及必须手动删除消息。

Even though there's some overlap in their use cases, both SQS and SNS have their own spotlight. 尽管他们的使用案例存在一些重叠,但SQS和SNS都有自己的聚光灯。

Use SNS if: 使用SNS如果:

  • multiple subscribers is a requirement 多个订户是必需的
  • sending SMS/E-mail out of the box is handy 开箱即用发送短信/电子邮件非常方便

Use SQS if: 在以下情况下使用SQS

  • only one subscriber is needed 只需要一个用户
  • batching is important 批处理很重要
 类似资料: