• Technicalpig
  • Posts
  • TechnicalPig🐷: What are AWS SNS and SQS used for?

TechnicalPig🐷: What are AWS SNS and SQS used for?

Why we need SNS and SQS in distributed systems

Today's newsletter explains how incorporating AWS SNS and SQS services into system design offers a range of benefits that address common challenges in distributed systems.

What is SNS? 

  • SNS is a fully managed pub/sub messaging service used for sending messages from publishers to subscribers. Publishers send messages to topics, and subscribers receive messages from these topics. 

How does it work? 

  • First you create a topic which is essentially a communication channel for messages. Publishers send messages to these topics. 

  • Subscribers such as AWS Lambda functions, SQS queues and HTTP/HTTPS endpoints, can subscribe to these topics. 

  • Publishes send messages to topics. When a message is publish to a topic, SNS delivers this message to each subscriber using a “Fan-out” model. This is useful for broadcasting messages to multiple recipients simultaneously. 

  • SNS operates primarily as a one-way, fire-and-forget messaging service, meaning it doesn't wait for an acknowledgment from the subscriber's endpoint upon delivering a message. SNS doesn't automatically retry the delivery. It's up to the subscriber to implement any retry mechanisms if necessary.

Use cases: 

  • SNS is used for building event-driven architecture, where an event triggers an immediate notification to various subscribers. 

  • It is ideal for scenarios requiring multiple notifications from a single event source enabling async communication and enhancing system reliance. 

  • It automatically scales your application traffic by delivering messages to millions of subscribes. 

What is SQS? 

  • SQS is a fully managed message queuing service that enables you to decouple and scale microservices. It supports a pull-based consumption model. 

How does it work? 

  • Producers add messages to an SQS queue.

  • Consumers poll and retrieve messages from the queue  to process them at their own pace. 

Use cases: 

  • SQS is used to bugger requests, manage traffic spikes and ensure reliable transmission of messages between components of a distributed application without losing messages. 

  • It's beneficial for managing message volume surges, ensuring message processing in case of downstream failures, and decoupling components for increased scalability and reliability.

SNS and SQS together

  • Using SNS and SQS together allows you to leverage the benefits of pub/sub messaging patterns (with SNS) along with message queuing (with SQS). This combination is powerful for building scalable, flexible, and resilient applications that require actions to be taken based on events, with different components processing the same or different messages in various ways.