> For the complete documentation index, see [llms.txt](https://mamawhocode.gitbook.io/aws/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://mamawhocode.gitbook.io/aws/services/integration/sqs.md).

# SQS

fully managed message queuing service

[AWS Document](https://docs.aws.amazon.com/sqs/?icmpid=docs_homepage_appintegration) | [Workshop](https://awsworkshop.io/tags/sqs/) | [FAQs](https://aws.amazon.com/sqs/faqs/?saa=sec\&sec=prep) |

## Overview

* fully managed message queuing service
* provide way for *asynchronously* communicate between applications, *without loosing messages*
  * How? It stores the copies of messages redundantly across multi-AZ.
* *<mark style="color:red;">**decouple**</mark>* and *<mark style="color:red;">**scale**</mark>* microservices, distributed system and serverless applications.&#x20;

<figure><img src="https://2259236002-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fuh9xZDZ53qGqmMCM44PU%2Fuploads%2Fgit-blob-df8214b786ea5bd96d74c29bf224cab1bdc0e86e%2Ffigure_20230506103834.png?alt=media" alt="" width="375"><figcaption><p>SQS queue</p></figcaption></figure>

### Concepts

* [Dead-letter queue](/aws/services/integration/sqs.md) (DLQ): contains messages that could not be processed.
* [Short polling](/aws/services/integration/sqs.md):
  * default polling setting.
  * `WaitTimeSeconds` = 0. Messages are returned immediately.
* `ReceiveMessageWaitTimeSeconds`: when the wait time of `ReceiveMessage` API > 0, long polling are in effect. Long polling help reduce empty responses.
  * [Long polling](/aws/services/integration/sqs.md):
    * `WaitTimeSeconds` > 0
    * Maximum wait time: 20 secs
* `VisibilityTimeout`: after a message is received, it still remains in the queue. ***The length of time*** which a *<mark style="color:red;">message unavailable</mark>* after delivering. To stop other consumers from processing that message again -> set `visibility timeout` on the `ReceiveMessage` API call to beyond what the longest transaction takes to process.
  * Default: 30 seconds. Min is 0 sec, Max is 12 hours.
  * Increase `VisibilityTimeout` if a consumer received message more than one.

```bash
aws sqs set-queue-attributes --queue-url <queue-url> --attributes VisibilityTimeout=120
```

* `MessageRetentionPeriod`: seconds that SQS will keep the message. Default is 4 days.
* `DelaySeconds`: seconds for which the delivery of all messages is delayed. Default is 0 second.&#x20;

### Benefits

* Eliminate administrative overhead.
* Keep sensitive data secure. Refer [encryption](#encryption).
* Reliability delivery messages
* Scale elastically and cost-effectively.

## Features

* Redundantly stores messages across multiple servers.
* Automatically ***scale***
* Built-in ***encryption***, access control features
* ***Message locking***: when a message received, it is locked for processing.&#x20;
  * When completed, it deletes the message from the queue.
  * When failed, the lock expire -> the message is available again.
* ***Queue sharing***: queue can be shared anonymously or to specific AWS accounts.

### Queue types

| -             | Standard                            | FIFO                                                                               |
| ------------- | ----------------------------------- | ---------------------------------------------------------------------------------- |
| Throughput    | unlimited                           | 3000 msg/sec                                                                       |
| Delivery type | Best-effort ordering (out of order) | FIFO                                                                               |
| Duplicate?    | Yes                                 | <p>No (exactly-once send)<br>built-in deduplication and ordering capabilities.</p> |
| Ordering      | No                                  | Yes                                                                                |

## Use cases

### Buffer to database writes

![](https://2259236002-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fuh9xZDZ53qGqmMCM44PU%2Fuploads%2Fgit-blob-eaf3374337ed9d9f4a1d3ae2a7ab457c7e8bfaca%2Ffigure_20230506114005.png?alt=media)

### Decouple between application tiers

![](https://2259236002-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fuh9xZDZ53qGqmMCM44PU%2Fuploads%2Fgit-blob-2df77656a2fff4fba79a001d8477745e3b87d682%2Ffigure_20230506114049.png?alt=media)

### Integrate 2 Lambda functions (producer & consumer)

<figure><img src="https://2259236002-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fuh9xZDZ53qGqmMCM44PU%2Fuploads%2FgQXoX5HJxhCiZWNzfAkT%2Fimage.png?alt=media&amp;token=cafbcd55-c936-4f37-958e-2b755056d361" alt=""><figcaption><p>integrating serverless application</p></figcaption></figure>

## Security

### Encryption

* In-flight encryption: HTTPS API
* At rest encryption: KMS keys
* Client-side encryption
* Server-side encryption (SSE): encrypt each message body

### SQS Access Policies

* Similar to S3 bucket policies
* Useful for cross-account access to SQS queues.

## Best practices

* Use batching: sending messages in batch can improve performance and reduce costs.
* Setting appropriate visibility timeouts
* Monitor your queues.

## Trivia

* Cannot change the queue type (Standard -> FIFO or FIFO -> Standard) after you create the queue.
* Message is persisted in SQS until a consumer deletes it.
* `Message retention`: default 4 days, up to 14 days.
* When need a solution with decoup or performance improvement by distribute load, use SQS.
