Azure  

Amazon Simple Queue Service (SQS)

Amazon SQS

Amazon SQS is a fully managed message queue service in Amazon Web Services that enables asynchronous communication between different parts of an application.

It helps decouple components so they can work independently and reliably.

1. Why SQS is Needed

In modern applications, multiple services communicate with each other.

Without a queue:

Service A → directly calls → Service B

If Service B fails or is overloaded, the request is lost.

With SQS:

Service A → Queue (SQS) → Service B

Now:

  • Service A sends a message to the queue

  • Service B processes it when ready

This prevents data loss and improves scalability.

2. Core Components of SQS

1. Producer

The service that sends messages to the queue.

Example:

  • Web server

  • Microservice

  • Application

2. Queue

Temporary storage for messages.

Example message:

{
 "order_id": 12345,
 "product": "Laptop",
 "quantity": 1
}

3. Consumer

The service that reads and processes messages from the queue.

Example:

  • Order processing service

  • Email sending service

3. How SQS Works (Step-by-Step)

  • Application sends a message to SQS.

  • The message is stored in the queue.

  • A consumer polls the queue.

  • The consumer processes the message.

  • The message is deleted from the queue.

Flow:

Application

SQS Queue

Worker Service / Lambda

Process Task

4. Types of SQS Queues

1. Standard Queue

Features:

  • Unlimited throughput

  • At-least-once message delivery

  • Messages may be delivered out of order

Best for:

  • High-volume applications

  • Event processing

  • Background tasks

Example:

  • Image processing

  • Log processing

2. FIFO Queue (First-In-First-Out)

Features:

  • Messages processed in exact order

  • Exactly-once processing

  • Lower throughput compared to Standard

Best for:

  • Financial transactions

  • Order processing

  • Inventory management

Example:

Order 1 → Processed first

Order 2 → Processed second

Order 3 → Processed third

5. Important SQS Concepts

Message

A unit of data sent to the queue.

Maximum size:

256 KB

Example:

"Send email to user"

Visibility Timeout

When a consumer receives a message:

  • The message becomes temporarily invisible

  • Prevents other consumers from processing it simultaneously

Example:

Worker receives message
Visibility Timeout = 30 seconds

If processing completes → message deleted
If not → message becomes visible again.

Message Retention Period

Time SQS stores messages.

Range:

1 minute → 14 days

Dead Letter Queue (DLQ)

Used for messages that fail processing multiple times.

Example:

Main Queue → Processing fails → Retry → Retry → Retry

Dead Letter Queue

This helps in debugging failed tasks.

6. Integration with Other AWS Services

SQS works with many AWS services:

  • AWS Lambda – Automatically process queue messages

  • Amazon EC2 – Worker servers consume messages

  • Amazon S3 – Trigger events to SQS

  • Amazon SNS – Fan-out messaging pattern

Example architecture:

User uploads image

S3 Event

SQS Queue

Lambda Function

Image Processing

7. Real-World Example

E-commerce Order Processing

User places an order.

Flow:

User

Web Application

Send message to SQS

Order Processing Service

Payment Service

Email Service

Benefits:

  • Prevents system overload

  • Ensures orders are processed even if services fail

8. Advantages of SQS

✅ Decouples microservices

✅ Highly scalable

✅ Reliable message delivery

✅ Fully managed by AWS

✅ Handles millions of messages

9. Simple Visual Architecture

Producer (Web App)

SQS
Message Queue

Consumers
(EC2 / Lambda Workers)

Process Tasks

✅ One-line definition for interviews:

Amazon SQS is a fully managed message queue service that enables asynchronous communication between distributed application components.