Skip to main content

What Is Amazon SQS? Queue Types and Message Operations

Tags:

When components in an application communicate directly, there's a risk of data loss if either side goes down. Amazon SQS provides a queue that temporarily holds messages, enabling loose coupling between components. This post covers the basics of SQS — from core concepts to creating queues and sending and receiving messages.

What Is Amazon SQS?

SQS (Simple Queue Service) is a fully managed message queuing service provided by AWS. Producers send messages to a queue, and consumers pull messages from it — allowing components to communicate asynchronously without direct connections.

Because the queue acts as a buffer, messages aren't lost even if the consumer goes down temporarily. You can also scale throughput by running multiple consumers in parallel.

Key Features

SQS has several characteristics unique to managed services. Let's take a look at each one.

FeatureDescription
Fully managedAWS handles infrastructure management and redundancy, reducing operational overhead
High availabilityMessages are automatically replicated across multiple Availability Zones, eliminating single points of failure
Unlimited throughputStandard queues support nearly unlimited messages per second (TPS)
Flexible retentionMessage retention can be set from 1 minute to 14 days (default: 4 days)
Pay-as-you-goCharged based on the number of messages sent and received, with 1 million requests free per month

Once a message is received, it becomes invisible to other consumers for a period called the visibility timeout. If the consumer doesn't delete the message after processing, it becomes available again after the timeout — be aware that this can lead to reprocessing.

Queue Types

SQS offers two queue types: standard and FIFO. Choose the one that fits your use case.

Standard QueueFIFO Queue
ThroughputNearly unlimitedUp to 3,000 TPS (with batching)
Message orderingBest-effort (not guaranteed)Strictly preserves FIFO order
Duplicate deliveryOccasionally possibleNo duplicates
Use casesLog aggregation, notifications, bulk processingOrder processing, payments, inventory updates

Standard queues prioritize throughput, so message ordering isn't guaranteed and the same message may occasionally be delivered more than once. Design your consumers to be idempotent (producing the same result when processing a message multiple times) to handle this safely.

FIFO queues guarantee message ordering and deduplication, but come with throughput limits. Queue names must end with .fifo.

Dead-Letter Queues

A dead-letter queue (DLQ) is a mechanism that moves messages to a separate queue when they repeatedly fail to be processed.

When a consumer fails to process a message and doesn't delete it, the message becomes available again after the visibility timeout. If it exceeds the configured maximum receive count (maxReceiveCount), it's automatically moved to the DLQ. Inspecting and reprocessing DLQ messages makes it easier to track problematic ones.

The SQS console also provides a "DLQ Redrive" feature to move messages from the DLQ back to the original queue.

Try SQS with the AWS CLI

Let's walk through the basic flow using the AWS CLI — from creating a queue to sending, receiving, and deleting messages.

Create a Queue

Start by creating a standard queue. Specify the queue name with --queue-name.

❯ aws sqs create-queue --queue-name exrecord-queue
{
"QueueUrl": "https://sqs.us-east-1.amazonaws.com/123456789012/exrecord-queue"
}

Save the URL to a variable for use in subsequent commands.

QUEUE_URL="https://sqs.us-east-1.amazonaws.com/123456789012/exrecord-queue"

Send a Message

Send a message to the queue. Specify the message content as a string with --message-body.

❯ aws sqs send-message \
--queue-url "$QUEUE_URL" \
--message-body 'This is a test message from SQS.'
{
"MD5OfMessageBody": "4b999f2b9ac63a0e8c4a2bea7ae2e12b",
"MessageId": "ca0330b3-c5e5-44d3-85a4-4062ef5dd0f5"
}

Receive a Message

Retrieve a message from the queue. The received message becomes invisible to other consumers for the duration of the visibility timeout.

❯ aws sqs receive-message --queue-url "$QUEUE_URL"
{
"Messages": [
{
"MessageId": "ca0330b3-c5e5-44d3-85a4-4062ef5dd0f5",
"ReceiptHandle": "AQEBwJnKyrHigUMZj...",
"MD5OfBody": "4b999f2b9ac63a0e8c4a2bea7ae2e12b",
"Body": "This is a test message from SQS."
}
]
}

Delete a Message

Once processing is complete, delete the message from the queue. If you don't delete it, it will be redelivered after the visibility timeout expires.

❯ aws sqs delete-message \
--queue-url "$QUEUE_URL" \
--receipt-handle "AQEBwJnKyrHigUMZj..."

Delete the Queue

Delete the queue when it's no longer needed. All messages in the queue will also be deleted, so confirm that processing is complete beforehand.

❯ aws sqs delete-queue --queue-url "$QUEUE_URL"

Summary

  • Amazon SQS is a fully managed message queuing service that loosely couples application components
  • Standard queues offer nearly unlimited throughput; FIFO queues provide ordering guarantees and deduplication
  • During the visibility timeout, a message is hidden from other consumers
  • Messages that repeatedly fail processing can be moved to a dead-letter queue for easier tracking and reprocessing