Learning AWS - SQS
Tags: aws, go, programming, backend
Published: 2022-04-20 20:13:27

Lately I started to learn about queue systems and about their behaviors. As the first point, Let's dive into SQS.

P.S. These are my training notes. It can be left things in void. If you sense this feeling, please warn me in comments.

P.S. You can use [localstack](https://docs.localstack.cloud/overview/) for training too
## What is SQS?

SQS is basically a secure, durable, and available AWS hosted queue. It can be really useful because it is hosted in 3rd party and has high availability.

SQS has two kind of queues.
- Standart Queue
- FIFO Queue (First-In-First-Out Queue)

Both of them are really close but there is a difference. ´FIFO queues´ provide the added benefits of supporting ordering and exactly-once processing and ensure that the order in which messages are sent and received is strictly preserved.

[Here is a detailed article for compare.](https://medium.com/awesome-cloud/aws-difference-between-sqs-standard-and-fifo-first-in-first-out-queues-28d1ea5e153)

## What is `Short-Long Polling`?

[As concept reference, this video is amazing.](https://www.youtube.com/watch?v=ZBM28ZPlin8)
Highly recommend to watch first. It is less than 5min anyway.

[Here is an explanation from AWS Documents.](https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/sqs-short-and-long-polling.html#sqs-short-polling)

## What is `Visibility Timeout`?

Visibility timeout is the time-period or duration you specify for the queue item which when is fetched and processed by the consumer is made hidden from the queue and other consumers.

The main purpose is to avoid multiple consumers (or the same consumer), consuming the same item repetitively.

[Here is a great drawing from AWS Documents.](https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/sqs-basic-architecture.html#sqs-message-lifecycle)

Also I found a really explanatory answer for this question:
> When using SQS as queuing service, when you read the message from the queue it does not automatically delete the message from the queue. So when you are processing the message, SQS will wait for the time period defined as visibility timeout before other consumers will get the same message again.

## How to determine `Visibility Timeout`?

Here is a quick technique to find best Visibility Timeout for your system:
> The best time value to set for the visibility timeout will be at least the timeout value for the consumer process. If the consumer is able to successfully complete the processing, it would delete the message from the queue else if it times out then the message reappears in the queue for other consumer to pick it again.

## What is `Delay Queues`?

They were basically make your message wait when it added to queue first time. Sound similar to `Visibility Timeout`. But work mechanism is kinda different. Better to look document. It wasn't too long or complex.

[Here is an explanation from AWS Documents.](https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/sqs-delay-queues.html)

Also we can apply this mechanic for message only by using `message timers`.

[Here is an explanation from AWS Documents.](https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/sqs-message-timers.html)

## What is `Dead-Letter Queues`?

[Here is an explanation from AWS Documents.](https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/sqs-dead-letter-queues.html#sqs-dead-letter-queues-how-they-work) This is a whole another subject to work on it. So for now I just ping the root document.

---

#### Resources

- [Visibility Timeout - StackOverflow Question](https://stackoverflow.com/questions/28713366/what-does-visibility-timeout-mean-for-aws-sqs#:~:text=Visibility%20timeout%20is%20the%20time,consuming%20the%20same%20item%20repetitively.)
- list