AWS - Difference Between SQS and SNS
AWS - Difference Between SQS and SNS
TL;DR
SQS and SNS are important components for scalable, large scale,
distributed, cloud-based applications:
SNS is a distributed publish-subscribe service.
SNS
With Amazon SNS, you can send push notifications to Apple, Google,
Fire OS, and Windows devices , as well as to Android devices in
China with Baidu Cloud Push. You can use SNS to send SMS
messages to mobile device users in the US or to email recipients
worldwide.
SQS
SQS is distributed queuing system. Messages are not pushed to
receivers. Receivers have to poll SQS to receive messages. 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.
Key Differences
Entity Type
SQS : Queue (similar to JMS, MSMQ).
SNS : Topic-Subscriber (Pub/Sub system).
Message consumption
SQS : Pull Mechanism — Consumers poll messages from SQS.
SNS : Push Mechanism — SNS pushes messages to consumers.
Persistence
SQS : Messages are persisted for some duration is no consumer
available. The retention period value is from 1 minute to 14 days. The
default is 4 days.
SNS : No persistence. Whichever consumer is present at the time of
message arrival, get the message and the message is deleted. If no
consumers 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.
SNS : All the consumers are (supposed to be) processing the messages
in different ways.
Use Cases
Choose SNS if:
We can design fanout pattern by using both SNS and SQS. In this
pattern, a message published to an SNS topic is distributed to multiple
SQS queues in parallel.
Summary
You don’t have to couple SNS and SQS always. You can have SNS
send messages to email, sms or http end point apart from SQS. There
are advantages to coupling SNS with 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. 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.
SQS is mainly used to decouple applications or integrate applications.
Messages can be stored in SQS for short duration of time (max 14
days). SNS distributes several copies of message to several
subscribers. 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.).