AWS SQS

Gavin Dang - Sep 5 - - Dev Community

Amazon Simple Queue Service (SQS) is a distributed message queuing service introduced by Amazon Web Services (AWS) in 2004. It allows you to send and receive messages through an HTTP API over the internet. Designed with high scalability in mind, SQS facilitates seamless message exchange between different components of a system.

Key Features of SQS:

  • High Scalability: SQS scales automatically based on message read/write capacity, eliminating the need to manage queue scaling manually—AWS handles this for you.

  • Pay-as-You-Go Pricing: You only pay for the number of read/write operations you perform, with no recurring fees.

  • Easy Setup: There is no need to set up any infrastructure to use SQS. You simply call the API to read or write messages.

  • Standard and FIFO Queues: SQS offers two types of queues: Standard and FIFO. You can choose the type that best suits your needs.

  • Automatic Deduplication in FIFO Queues: SQS automatically handles duplicate messages in FIFO queues. This ensures that tasks are executed only once, making FIFO queues ideal for processes where each task should be executed exactly once.

  • Error Handling with Dead Letter Queues (DLQs): A critical feature for debugging, DLQs store messages that could not be processed successfully. You can inspect and handle failed messages separately from the main queue.

How Does SQS Work?

Behind the Scenes:

  1. Queue Operation: Typically, a queue operates by having producers send messages to it. Consumers then retrieve and process these messages. The system tracks messages as pending or in-flight and delivers them to consumers. Background jobs at the consumer end process these messages.

  2. Message Processing: Once a consumer finishes processing a message, it sends an acknowledgment to the queue to confirm successful processing. The message status changes from pending to processed. If the acknowledgment is not received within a certain time frame, the message is considered not processed and is made available to other consumers.

  3. Handling Failures: If processing fails after a specified number of retries, the message is either stopped from being sent to consumers or moved to a Dead Letter Queue (DLQ) for further analysis.

Overall

The operation of AWS SQS is straightforward. When an application or system needs to send a message, it places the message into an SQS queue. The message remains in the queue until another process, known as a "consumer," retrieves it for processing.

For instance, if you are developing a bulk email sending application, you can utilize SQS to manage email sending requests. Each request is added to the SQS queue, and a separate process retrieves and processes these requests to send the emails.

With SQS:

  • SQS provides an API endpoint for producers to send messages and for consumers to read and process them. Messages sent to SQS can be in any format, including strings, XML, or JSON.
  • There is no limit to the number of messages that can be sent or processed by SQS.
  • Example Message Structure:

Here’s what a message in SQS might look like:

{
  "message_id": "a123d234g-82451-4qwf-a2f36-ee2135as25td",
  "receipt_handle": "AQEBLVo6YHmeJXe2np4Dz7Yf0UpRsIoM5Nk9T7W1jC/LuPp9ZG9b8QJv6EZo8L5XbKQ1dIn4yqV9G0YkCwH3j0TNRhM1gIb5LBcL2R1HrAo8ZLkH1lBG9ZPXTpG2bCu7HjpQK/lQ5Z8r1kLg+V8dV0b9mJ4CpF4Pne/Ay7GQfW9r8ZlF2r1uJvlzFv8nN3yV6d9LPokjImRTBxF8gP4C2QDLX0wF8NHdjpt7Ud9lKM0W5Q3be0Nsn9Nzj94QG+Fx7bT7SgA0lMLUyzk7OPg0xrP9kT+Rr1YmB9gSxfNl4+xk19vY5k/pS7Brz0YsUk0Tc8MtXpE61wXlsz6+VbOk2e8M6+HVtX39FNbGfhf5jHDu/k14EYtQv/g9ZRVUyQh6F9bC5Wv/g1Cs1VvoTLKq56yJQF9y3oRtx0OrX2bH2JlmBfs0aeY5rlQ==",
  "md5_of_body": "c923g2gf23g2g95asdg23sd12tg2g",
  "body": "8",
  "attributes": {
    "SenderId": "12345",
    "ApproximateFirstReceiveTimestamp": "13458591097096",
    "ApproximateReceiveCount": "1",
    "SentTimestamp": "1758926812155"
  },
  "md5_of_message_attributes": null,
  "message_attributes": {
    "EventType":"Updated",
    "bodyMessage":{
      "samplesA":"A",
      "sampleB":"B",
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

...

. . .
Terabox Video Player