SQS Queues
An SQS queue lets you send, store, and receive messages between software components at any volume without losing messages or requiring other services to be available. It's a reliable way to decouple and connect microservices.
Under the hood, Stacktape uses Amazon Simple Queue Service (SQS).
When to use them
Stacktape offers three managed resources for messaging and communication:
- SNS topics
- SQS queues
- Event buses
Use an SNS topic when:
- You need to publish messages to many different subscribers with a single action.
- You require high throughput and reliability.
Use an SQS queue when:
- You need reliable, one-to-one, asynchronous communication to decouple your applications.
- You want to control the rate at which messages are consumed.
Use an event bus when:
- You want to publish messages to many subscribers and use the event data itself to filter which messages are delivered to which subscribers.
- You need to integrate with third-party SaaS providers like Shopify, Datadog, or PagerDuty.
Basic usage
You don't need to specify any properties to create a basic SQS queue.
resources:mainQueue:type: sqs-queue
Integrating with workloads
You can trigger a function or a batch job whenever messages are available in an SQS queue. Messages are processed in batches. If the function or batch job fails, the messages will reappear in the queue after the visibility timeout.
resources:myLambda:type: functionproperties:packaging:type: stacktape-lambda-buildpackproperties:entryfilePath: path/to/my-lambda.tsevents:- type: sqsproperties:sqsQueueName: mySqsQueuemySqsQueue:type: sqs-queue
A Lambda function with an SQS queue integration.
You can also receive messages using the AWS SDK, as shown in this worker service example:
resources:myQueue:type: sqs-queueproperties:fifoEnabled: trueprocessing:type: worker-serviceproperties:packaging:type: stacktape-image-buildpackproperties:entryfilePath: processing.tsconnectTo:- myQueueresources:cpu: 0.25memory: 512
import { DeleteMessageCommand, ReceiveMessageCommand, SQSClient } from '@aws-sdk/client-sqs';const sqsClient = new SQSClient({});const interval = setInterval(async () => {try {const { Messages } = await sqsClient.send(new ReceiveMessageCommand({ QueueUrl: process.env.STP_MY_QUEUE_URL }));if (Messages?.length) {// do some message processing// .....// After processing, delete messages from queue.// If you do NOT delete messages, they will become visible after visibility timeout elapses.// This can lead to same message being processed twice.await Promise.all(Messages.map(({ ReceiptHandle }) =>sqsClient.send(new DeleteMessageCommand({ QueueUrl: process.env.STP_MY_QUEUE_URL, ReceiptHandle }))));}console.info('Nothing to process');} catch (err) {// error handlingconsole.error(err);clearInterval(interval);}}, 10000);
The code for processing.ts.
Sending messages
You can use the AWS SDK to send messages to a queue.
import { SendMessageCommand, SQSClient } from "@aws-sdk/client-sqs";const sqsClient = new SQSClient({});sqsClient.send(new SendMessageCommand({MessageBody: "Hello from the other side",QueueUrl: process.env.STP_MY_QUEUE_URL,}));
FIFO
You can enable FIFO (First-In, First-Out) to ensure that messages are processed in the exact order that they are sent.
FIFO queues are designed for applications where the order of operations and events is critical and duplicates cannot be tolerated.
When using a FIFO queue, each message must have a MessageDeduplicationId, or contentBasedDeduplication must be enabled.
For more information, see the AWS documentation on FIFO queues.
resources:mainQueue:type: sqs-queueproperties:fifoEnabled: true
Content-based deduplication
You can enable content-based deduplication to prevent duplicate messages from being sent.
During the deduplication interval, the queue treats messages with the same content as duplicates and delivers only one copy.
Deduplication is based on the MessageDeduplicationId. If you do not provide one, Amazon SQS will generate a SHA-256 hash of the message body to use as the MessageDeduplicationId.
fifoEnabled must be true to use this feature.
Visibility timeout
The visibility timeout is the amount of time that a message is hidden from other consumers after it has been received. This prevents other consumers from processing the same message.
When a consumer receives a message, it remains in the queue but is made invisible to other consumers for the duration of the visibility timeout. This prevents the message from being processed multiple times. The consumer is responsible for deleting the message from the queue after it has been successfully processed.
The visibility timeout can be set from 0 to 43,200 seconds (12 hours).
For more information, see the AWS documentation on visibility timeout.
You can set the visibility timeout using the visibilityTimeoutSeconds property.
resources:mainQueue:type: sqs-queueproperties:visibilityTimeoutSeconds: 300