Skip to main content
AWS Fundamentals Tutorial
CHAPTER 19 Beginner

AWS SNS and SQS Messaging Services

Updated: May 15, 2026
25 min read

# CHAPTER 19

AWS SNS and SQS Messaging Services

1. Introduction

In a modern cloud application, services must communicate. If your web server directly calls your database, and the database is busy, the web server freezes, the user gets an error, and the data is lost. This is called a "Tightly Coupled" architecture. To build resilient, enterprise-grade systems, architects "Decouple" their services using messaging systems. In this chapter, we will master the two fundamental AWS messaging services: Amazon SNS (Simple Notification Service) and Amazon SQS (Simple Queue Service).

2. Learning Objectives

By the end of this chapter, you will be able to:
  • Define Tightly Coupled vs. Loosely Coupled architectures.
  • Understand the Pub/Sub (Publish/Subscribe) model of Amazon SNS.
  • Understand the message polling model of Amazon SQS.
  • Design the "Fan-Out" architectural pattern by combining SNS and SQS.
  • Prevent data loss during traffic spikes using message queues.

3. Beginner-Friendly Explanation

Imagine a fast-food drive-thru.
  • Tightly Coupled (Bad): You order a burger at the window. The cashier turns around, walks to the grill, cooks the burger, and hands it to you. The next 50 cars in line must wait 10 minutes each just to place their order.
  • Loosely Coupled (SQS): You order at the speaker. The cashier writes the order on a ticket and sticks it on a rotating rail (The Queue / SQS). The cashier immediately takes the next car's order. Meanwhile, three cooks in the back pull tickets off the rail one by one and cook the burgers. The line moves instantly, and no orders are lost.
  • Pub/Sub (SNS): The Manager grabs a megaphone and yells: "Free fries for everyone!" (Publish). Anyone standing in the lobby who hears it (Subscribers) gets free fries.

4. Amazon SQS (Simple Queue Service)

SQS is a Message Queue. It is the oldest service in AWS (older than EC2!).
  • A "Producer" (like your Web Server) sends a message into the SQS Queue.
  • The message sits safely in the queue.
  • A "Consumer" (like an Auto Scaling group of worker EC2s or a Lambda function) polls the queue, reads the message, processes the data, and then *deletes* the message from the queue so no one else processes it.
  • Why? If you get 10,000 orders in one minute, your database might crash. Instead, you drop the 10,000 orders into SQS. Your database slowly reads them from the queue at its own safe pace. Zero dropped orders!

5. Amazon SNS (Simple Notification Service)

SNS is a Publish/Subscribe (Pub/Sub) system.
  • You create an "SNS Topic".
  • "Subscribers" sign up for the topic. Subscribers can be Emails, SMS Text Messages, HTTP endpoints, or even SQS Queues.
  • A "Publisher" sends ONE message to the SNS Topic.
  • SNS instantly duplicates that message and "pushes" it to every single Subscriber simultaneously.

6. The Fan-Out Pattern (Combining SNS and SQS)

This is a legendary cloud architecture pattern. Scenario: A user uploads a video to your website. You need to do three things: (1) Compress the video for mobile, (2) Extract the audio for a podcast, (3) Update the database. The Solution:
  1. 1. The Web Server uploads the video and publishes ONE message to an SNS Topic.
  1. 2. You have three SQS Queues subscribed to that SNS Topic.
  1. 3. SNS instantly duplicates the message into all three Queues (The Fan-Out!).
  1. 4. Three separate worker servers read their respective queues and perform their specific jobs in parallel, completely independently.

7. Mini Project: Build a Notification System

Let's build a simple Pub/Sub system using SNS.

Step-by-Step Tutorial:

  1. 1. Open the AWS Console and search for SNS.
  1. 2. Click Topics -> Create topic.
  1. 3. Select Standard type.
  1. 4. Name: MyWebsiteAlerts. Click Create topic.
  1. 5. Once created, click Create subscription.
  1. 6. Protocol: Select Email.
  1. 7. Endpoint: Enter your personal email address. Click Create.
  1. 8. Check your email inbox! AWS will send a confirmation link. You MUST click it to verify the subscription.
  1. 9. Go back to your SNS Topic in the console. Click Publish message.
  1. 10. Subject: Server Alert. Message body: The database is under heavy load!
  1. 11. Click Publish message.
  1. 12. Check your phone/email. The message arrived instantly! *(This is the exact mechanism CloudWatch Alarms use from Chapter 17).*

8. Best Practices

  • Dead Letter Queues (DLQ): What happens if an SQS message contains a glitch, and your worker server crashes every time it tries to read it? It will go into an infinite loop! Always attach a Dead Letter Queue to your main SQS queue. If a message fails to process 5 times, SQS moves it to the "Jail" (the DLQ) so developers can analyze the glitch without blocking the main line.

9. Common Mistakes

  • Confusing "Push" vs. "Pull": Remember the fundamental difference. SNS is a Push system (it forces the message onto the subscriber immediately). SQS is a Pull/Poll system (the message sits quietly in the queue until a worker server asks for it).

10. Exercises

  1. 1. Define the architectural benefit of a "Loosely Coupled" system utilizing SQS during an unexpected traffic spike.
  1. 2. Explain the "Fan-Out" architecture pattern using SNS and SQS.

11. MCQs with Answers

Question 1

An e-commerce application experiences massive database crashes during Black Friday sales because the web servers attempt to write thousands of orders to the database simultaneously. Which AWS service should be introduced to decouple the web servers from the database, allowing orders to be processed at a safe, controlled pace?

Question 2

Which of the following best describes the core operational difference between Amazon SNS and Amazon SQS?

12. Interview Questions

  • Q: Explain the concept of decoupling a monolithic architecture. Detail how introducing Amazon SQS between a frontend web tier and a backend worker tier prevents data loss.
  • Q: Describe a Dead Letter Queue (DLQ). Why is it an essential component of a robust message processing system in AWS?

13. FAQs

Q: Does SQS guarantee the exact order of messages? A: A Standard SQS Queue does *not* guarantee strict order (Message B might be processed before Message A). If your application requires strict sequence (e.g., banking transactions), you must use an SQS FIFO (First-In-First-Out) Queue, which guarantees exact chronological processing.

14. Summary

In Chapter 19, we elevated our architectural thinking from monolithic servers to decoupled, resilient microservices. We introduced Amazon SQS as a buffer queue to protect backend databases from devastating traffic spikes. We explored Amazon SNS as a Pub/Sub broadcasting engine. Finally, we combined both to understand the powerful "Fan-Out" pattern, ensuring that our systems can process massive asynchronous workloads safely and efficiently.

15. Next Chapter Recommendation

Building VPCs, Load Balancers, and Auto Scaling groups by hand takes hours. What if AWS could build it all for you automatically based on your application code? Proceed to Chapter 20: AWS Elastic Beanstalk.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·