Thursday, October 23, 2025

AWS - SQS

Amazon SQS is a fully managed message queuing service that enables decoupling and scaling of microservices, distributed systems, and serverless applications. It acts as a buffer between components of a system that communicate asynchronously.


Why Use SQS?

Imagine you have a system with multiple services (e.g., a web app and a background worker). You don't want your web server to wait for long tasks (like image processing or email sending). With SQS:

  • The web server sends a message to the queue.

  • The worker processes messages from the queue asynchronously.

  • If the worker fails, the message stays in the queue until it’s successfully processed (or moved to a dead-letter queue after repeated failures).


Core Concepts

ConceptDescription
QueueA buffer where messages are stored until they're processed.
MessageA unit of data sent between components (can include up to 256 KB of data).
ProducerThe component that sends messages to the queue.
ConsumerThe component that receives and processes messages.
Visibility TimeoutTime during which a message is invisible after being read (to avoid duplication).
Dead-Letter QueueStores messages that couldn't be processed successfully after retries.

Types of SQS Queues

  1. Standard Queue

    • Default type

    • At-least-once delivery (possible duplicates)

    • Best-effort ordering

    • High throughput (nearly unlimited)

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

    • Exactly-once processing (no duplicates)

    • Preserved order

    • Lower throughput compared to Standard (up to 3000 messages/sec with batching)


Key Features

  • Fully managed: No infrastructure to manage

  • Scalable: Handles millions of messages per second

  • Secure: Supports IAM policies, encryption (SSE)

  • Durable: Messages stored redundantly across multiple AZs

  • Long polling: Reduces empty responses by waiting up to 20 seconds for messages


Common Use Cases

  • Decoupling microservices

  • Buffering requests (throttling)

  • Asynchronous task processing (email, video transcoding)

  • Distributed workloads

  • Order processing systems (FIFO)


Sample Workflow

  1. Producer sends a message to the queue:

    aws sqs send-message --queue-url <QUEUE_URL> --message-body "ProcessOrder123"
  2. Consumer polls and processes:

    aws sqs receive-message --queue-url <QUEUE_URL> --max-number-of-messages 1
  3. Deletes message after processing:

    aws sqs delete-message --queue-url <QUEUE_URL> --receipt-handle <HANDLE>

Security and Access Control

  • Use IAM policies to restrict who can send/receive/delete messages.

  • Enable server-side encryption with SSE-SQS or SSE-KMS.

  • Use VPC endpoints for private access.


Integrations

  • AWS Lambda: Trigger Lambda functions from SQS messages (FIFO supported).

  • Amazon SNS: SNS can fan-out messages to SQS queues.

  • Step Functions: Use SQS for wait and queue-based orchestration.

  • EC2, ECS, EKS: Poll SQS from worker nodes.

No comments:

Post a Comment

CI/CD - Safe DB Changes/Migrations

Safe DB Migrations means updating your database schema without breaking the running application and without downtime . In real systems (A...