Amazon SQS and Amazon SNS are two popular tools for engineers when designing distributed systems in the AWS cloud ecosystem.
It can be useful if we understand what are the similarities and differences between these tools. I'm going to introduce that to you down below.
Amazon SQS:
- Used for Application to Application messaging
- Communication Channel: Queue
- Pull/Poll mechanism
- Typical queueing, producer/consumer paradigm
- 14 days message retention
- Two variants, Standard and FIFO
- Guarantees exactly delivery
- Maximum Message Size: 256 KB
- Provides no message filtering
- Supports Cross-Region messaging
- Does not support Message Schema Registry
Amazon SNS:
- Used for Application to Application messaging as well as application to Person notification
- Communication Channel: Queue
- Push mechanism with retry
- Publisher/Subscriber messaging
- 14 days message retention
- Two variants, Standard and FIFO
- Guarantees at least once delivery
- Maximum Message Size: 256 KB
- Provides some message filtering
- Supports Cross-Region messaging
- Does not support Message Schema Registry
Differences:
Entity Type
- SQS : Queue
- 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. Which ever 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.
- In SQS the message delivery is guaranteed but in SNS it is not.
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 processing the messages in different ways.
When to use?
Choose SNS if:
- You would like to be able to publish and consume batches of messages.
- You would like to allow same message to be processed in multiple ways.
- Multiple subscribers are needed.
Choose SQS if:
- You need a simple queue with no particular additional requirements.
- Decoupling two applications and allowing parallel asynchronous processing.
- Only one subscriber is needed.
That's it! Thanks for reading!