Print this pageEmail this pageGo to the ForumsView the PDFShare this page on TwitterShare this page on FacebookBookmark this page on DeliciousSubmit this page to RedditSubmit this page to DiggDid this page help you?  Yes  No   Tell us about it...

Introduction to Amazon SQS

Topics

This introduction to Amazon SQS is intended to give you a detailed summary of this web service. After reading this section, you should have a good idea of what SQS offers and how it can fit in with your business.

Overview of Amazon SQS

Amazon SQS is a distributed queue system that enables web service applications to quickly and reliably queue messages that one component in the application generates to be consumed by another component. A queue is a temporary repository for messages that are awaiting processing.

Using Amazon SQS, you can decouple the components of an application so they run independently, with Amazon SQS easing message management between components. Any component of a distributed application can store any type of data in a fail-safe queue. Any other component can then later receive the data programmatically using the SQS API.

The queue acts as a buffer between the component producing and saving data, and the component receiving the data for processing. This means the queue resolves issues that arise if the producer is producing work faster than the consumer can process it, or if the producer or consumer are only intermittently connected to the network.

SQS ensures delivery of each message at least once, and supports multiple readers and writers interacting with the same queue. A single queue can be used simultaneously by many distributed application components, with no need for those components to coordinate with each other to share the queue.

Amazon SQS is engineered to always be available and deliver messages. One of the resulting tradeoffs is that SQS does not guarantee first in, first out delivery of messages. For many distributed applications, each message can stand on its own, and as long as all messages are delivered, the order is not important. If your system requires that order be preserved, you can place sequencing information in each message, so that you can reorder the messages when the queue returns them.

Be sure to read about distributed queues, which will help you understand how to design an application that works correctly with Amazon SQS. For more information, see Properties of Distributed Queues.

Amazon SQS Features

Amazon SQS provides the following major features:

  • Redundant infrastructure—Guarantees delivery of your messages at least once, highly concurrent access to messages, and high availability for sending and retrieving messages

  • Multiple writers and readers—Multiple parts of your system can send or receive messages at the same time

    SQS locks the message during processing, keeping other parts of your system from processing the message simultaneously.

  • Configurable settings per queue—All of your queues don't have to be exactly alike

    For example, one queue can be optimized for messages that require a longer processing time than others.

  • Variable message size—Your messages can be up to 8 KB in size

    For even larger messages, you can store the contents of the message using the Amazon Simple Storage Service (Amazon S3) or Amazon SimpleDB and use Amazon SQS to hold a pointer to the Amazon S3 or Amazon SDB object. Alternately, you can split the larger message into smaller ones.

    For more information about the services, go to the Amazon S3 detail page and the Amazon SimpleDB detail page.

  • Unlimited queues and messages—You can have as many queues and messages in the Amazon SQS system as you want

  • Access control—You can control who can send messages to a queue, and who can receive messages from a queue

Architectural Overview

There are three main actors in the overall system:

  • The components of your distributed system

  • Queues

  • Messages in the queues

In the following diagram, your system has several components that send messages to the queue and receive messages from the queue. The diagram shows that a single queue, which has its messages (labeled A-E), is redundantly saved across multiple SQS servers.

Architectural Overview

How SQS Queues Work

This section describes the basic properties of Amazon SQS queues, identifiers for queues and messages, how you determine the general size of the queue, and how you manage the messages in a queue.

About SQS Queues

You can have as many queues with as many messages as you like in the Amazon SQS system. A queue can be empty if you haven't sent any messages to it or if you have deleted all the messages from it.

You assign a name to each of your queues (for more information, see Queue URLs). You can get a list of all your queues or a subset of your queues that share the same initial characters in their names (for example, you could get a list of all your queues whose names start with "T3").

You can delete a queue at any time, whether it is empty or not. Be aware, however, that queues retain messages for a set period of time. By default, a queue retains messages for four days. However, you can configure a queue to retain messages for up to 14 days after the message has been sent.

We reserve the right to delete a queue without notification if one of the following actions hasn't been performed on it for 30 consecutive days: SendMessage, ReceiveMessage, DeleteMessage, GetQueueAttributes, SetQueueAttributes, AddPermission, and RemovePermission.

[Important]Important

It is a violation of the intended use of Amazon SQS if you repeatedly create queues and then leave them inactive.

The following table lists the API actions to use.

To do this... Use this action

Create a queue

CreateQueue

List your queues

ListQueues

Delete a queue

DeleteQueue

Properties of Distributed Queues

The following information can help you design your application to work with Amazon SQS correctly.

Message Order

SQS makes a best effort to preserve order in messages, but due to the distributed nature of the queue, we cannot guarantee you will receive messages in the exact order you sent them. If your system requires that order be preserved, we recommend you place sequencing information in each message so you can reorder the messages upon receipt.

At-Least-Once Delivery

SQS stores copies of your messages on multiple servers for redundancy and high availability. On rare occasions, one of the servers storing a copy of a message might be unavailable when you receive or delete the message. If that occurs, the copy of the message will not be deleted on that unavailable server, and you might get that message copy again when you receive messages. Because of this, you must design your application to be idempotent (i.e., it must not be adversely affected if it processes the same message more than once).

Message Sample

When you retrieve messages from the queue, SQS samples a subset of the servers (based on a weighted random distribution) and returns messages from just those servers. This means that a particular receive request might not return all your messages. Or, if you have a small number of messages in your queue (less than 1000), it means a particular request might not return any of your messages, whereas a subsequent request will. If you keep retrieving from your queues, SQS will sample all of the servers, and you will receive all of your messages.

The following figure shows messages being returned after one of your system components makes a receive request. SQS samples several of the servers (in gray) and returns the messages from those servers (Message A, C, D, and B). Message E is not returned to this particular request, but it would be returned to a subsequent request.

Message Sampling

Queue and Message Identifiers

SQS uses the following three identifiers that you need to be familiar with:

  • Queue URL

  • Message ID

  • Receipt handle

Queue URLs

When creating a new queue, you must provide a queue name that is unique within the scope of all your queues. If you create queues using both the 2008-01-01 WSDL and a previous version, you still have a single namespace for all your queues. SQS assigns each queue you create an identifier called a queue URL, which includes the queue name and other components that SQS determines. Whenever you want to perform an action on a queue, you provide its queue URL.

The following is the queue URL for a queue named "queue2" owned by a person with the AWS account number "123456789012".

http://sqs.us-east-1.amazonaws.com/123456789012/queue2

[Important]Important

In your system, always store the entire queue URL as Amazon SQS returned it to you when you created the queue (for example, http://sqs.us-east-1.amazonaws.com/123456789012/queue2). Don't build the queue URL from its separate components each time you need to specify the queue URL in a request because Amazon SQS could change the components that make up the queue URL.

You can also get the queue URL for a queue by listing your queues. Even though you have a single namespace for all your queues, the list of queues returned depends on the WSDL you use for the request. For more information, see ListQueues.

Message IDs

In versions of Amazon SQS prior to 2009-02-01, each message received a system-assigned identifier that you needed in order to delete the message from the queue. SQS still returns this message ID to you in the SendMessage response, but you can no longer use the message ID to delete the message. Instead you need a receipt handle.

Receipt Handles

Each time you receive a message from a queue, you receive a receipt handle for that message. The handle is associated with the act of receiving the message, not with the message itself. To delete the message or to change the message visibility, you must provide the receipt handle and not the message ID. This means you must always receive a message before you can delete it (you can't put a message into the queue and then recall it).

[Important]Important

If you receive a message more than once, each time you receive it, you get a different receipt handle. You must provide the most recently received receipt handle when you request to delete the message or the message might not be deleted.

Following is an example of a receipt handle.

MbZj6wDWli+JvwwJaBV+3dcjk2YW2vA3+STFFljTM8tJJg6HRG6PYSasuWXPJB+Cw
Lj1FjgXUv1uSj1gUPAWV66FU/WeR4mq2OKpEGYWbnLmpRCJVAyeMjeU5ZBdtcQ+QE
auMZc8ZRv37sIW2iJKq3M9MFx1YvV11A2x/KSbkJ0=

Resources Required to Process Messages

To help you estimate the amount of resources needed to process your queued messages, Amazon SQS can provide you with an approximate number of messages in a queue. You can view the number of messages that are visible or you can view the number of messages that are not visible. Messages that are not visible are messages in a queue that are not timed-out and not deleted. For more information about visibility, see Visibility Timeout.

[Important]Important

Because of the distributed architecture of SQS, the result is not an exact count of the number of messages in a queue. In most cases it should be close to the actual number of messages in the queue, but you should not rely on the count being precise.

The following table lists the API action to use.

To do this... Use this action

Get the approximate number of messages in the queue

GetQueueAttributes

Get the approximate number of messages in the queue that are not visible

GetQueueAttributes

Visibility Timeout

When a consuming component in your system receives and processes a message from the queue, the message remains in the queue. Why doesn't Amazon SQS automatically delete it?

Because your system is distributed, there's no guarantee that the component will actually receive the message (it's possible the connection could break or the component could fail before receiving the message). Therefore, Amazon SQS does not delete the message, and instead, your consuming component must delete the message from the queue after receiving and processing it.

Immediately after the component receives the message, the message is still in the queue. However, you don't want other components in the system receiving and processing the message again. Therefore, Amazon SQS blocks them with a visibility timeout, which is a period of time during which Amazon SQS prevents other consuming components from receiving and processing that message. The following figure and discussion illustrate the concept.

Visibility Timeout

General Recommendations for Visibility Timeout

The visibility timeout clock starts ticking once Amazon SQS returns the message. During that time, the component processes and deletes the message. But what happens if the component fails before deleting the message? If your system doesn't call DeleteMessage for that message before the visibility timeout expires, the message again becomes visible to the ReceiveMessage calls placed by the components in your system and it will be received again. If a message should only be received once, your system should delete it within the duration of the visibility timeout.

Each queue starts with a default setting of 30 seconds for the visibility timeout. You can change that setting for the entire queue. Typically, you'll set the visibility timeout to the average time it takes to process and delete a message from the queue. When receiving messages, you can also set a special visibility timeout for the returned messages without changing the overall queue timeout.

We recommend that if you have a system that produces messages that require varying amounts of time to process and delete, you create multiple queues, each with a different visibility timeout setting. Your system can then send all messages to a single queue that forwards each message to another queue with the appropriate visibility timeout based on the expected processing and deletion time for that message.

Extending a Message's Visibility Timeout

When you receive a message from the queue, you might find the visibility timeout for the queue is insufficient to fully process and delete that message. Amazon SQS allows you to extend the visibility timeout for that particular message. When you extend the visibility timeout, Amazon SQS overwrites the original timeout value and the new value begins at the time you changed it.

For example, let's say the timeout for the queue is 30 seconds, and you receive a message. Once you're 20 seconds into the timeout for that message (i.e., you have 10 seconds left), you extend it by 60 seconds by calling ChangeMessageVisibility with VisibilityTimeoutset to 60 seconds. You have then changed the remaining visibility timeout from 10 seconds to 60 seconds.

The extension you request is not stored in memory for that message. If for some reason you don't delete the message and the message is received again, the visibility timeout for the message the next time it's received is the overall value for the queue and not the extended value you previously set.

Terminating a Message's Visibility Timeout

When you receive a message from the queue, you might find that you actually don't want to process and delete that message. Amazon SQS allows you to terminate the visibility timeout for a specific message, which immediately makes the message visible to other components in the system to process. To do this, you call ChangeMessageVisibility with VisibilityTimeout=0 seconds.

API Actions Related to Visibility Timeout

The following table lists the API actions to use to manipulate the visibility timeout.

To do this... Use this action

Set the visibility timeout for a queue

SetQueueAttributes

Get the visibility timeout for a queue

GetQueueAttributes

Set the visibility timeout for the received messages without affecting the queue's visibility timeout

ReceiveMessage and set the VisibilityTimeout parameter to the value you want

Extending or terminating a message's visibility timeout

ChangeMessageVisibility

Message Lifecycle

The following diagram and process describe the lifecycle of an Amazon SQS message, called Message A, from creation to deletion. Assume that a queue already exists.

Message Lifecycle

Message Lifecycle

Component 1 sends Message A to a queue and the message is redundantly distributed across the SQS servers.

When Component 2 is ready to process a message, it retrieves messages from the queue, and Message A is returned. While Message A is being processed, it remains in the queue and is not returned to subsequent receive requests for the duration of the visibility timeout.

Component 2 deletes Message A from the queue to avoid the message being received and processed again once the visibility timeout expires.

[Note]Note

SQS automatically deletes messages that have been in a queue for more than 4 days.

AWS Identity and Access Management

Amazon SQS integrates with AWS Identity and Access Management (IAM), a service that lets your organization do the following:

  • Create users and groups under your organization's AWS account

  • Easily share your AWS account resources between the users in the account

  • Assign unique security credentials to each user

  • Granularly control users access to services and resources

  • Get a single AWS bill for all users under the AWS account

For example, you can use IAM with Amazon SQS to control the type of access a User or group of Users has to a specific queue your AWS Account owns.

For general information about IAM, go to:

For specific information about how you can control User access to Amazon SQS, go to Integrating with Other AWS Products in Using AWS Identity and Access Management.