While working with AWS, many a times you may need to decouple the interaction between your applications.
But the problem you might face is, you are not sure if you should work with SNS or SQS. So let’s try to understand the differences between the two services!
What is SQS?
SQS or Simple Queue Service is a web service that makes it easy to coordinate work across distributed application components.
So let’s say you have two applications and you want to decouple the interaction between these applications. You can do this by, adding a service which can hold the messages from the Producer, until, your Consumer is up and ready to pick those messages. This is where SQS comes into picture. SQS receives the messages from the Producer and holds it until the consumer polls the message.
What is SNS?
SNS or Simple Notification Service is a web service that provides message delivery from publishers to subscribers (also known as producers and consumers).
Wasn’t that exactly what SQS did?
Not exactly. There is a BIG difference how the two services work.
So what is the difference between the two?
SQS is a pull based service, while SNS is push based.
As SQS is pull based, the consumer will be polling at regular intervals which will be chargeable. You can try to reduce these charges by using Long Polling, but this may affect your application performance.
SQS is a queue, while SNS is a Topic (Pub/Sub System).
As soon as a message is pushed by Producer into SNS, the SNS Topic pushes it out to all the Consumers.
With SQS too, you can have multiple consumers consume the message from the same queue concurrently, but for large volumes, your queue will itself become the bottleneck!
SQS can retain the messages for up to 14 days, whereas, the maximum lifetime of a message can be 1 hour in SNS.
With SQS, message delivery is guaranteed. With SNS it’s NOT!
SNS are better for Fan Out design patterns. Though you can get the best of both the services by using a combination of them. READ ME!
With SQS, you may end up receiving same message multiple times, which can screw some services (metric service, etc.), while with SNS, you won’t face such problems.
While using SQS, you’ll manually have to delete a message from the queue. While with SNS, the message is lost immediately.
What do we know, what have we learnt?
SQS : Pull Mechanism.
SNS : Push Mechanism.
SQS : Queue.
SNS : Topic.
SQS : Messages are persisted for some (configurable) duration if no consumer available. (1 minute to 14 days)
SNS : No persistence. Whichever consumer is present at the time of message arrival gets the message and the message is deleted. If no consumers available then the message is lost.
Ideal Use Case:
SQS : Decoupling applications and allowing parallel asynchronous processing.
SNS : Fanout.
Hope you enjoyed!
Feel free to share your suggestions in the comments below!
Show your support by clapping!!!
Buy me a Coffee: https://www.buymeacoffee.com/aashray