Stacktape
Stacktape


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:

  1. SNS topics
  2. SQS queues
  3. 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
SqsQueue  API reference
type
Required
properties.delayMessagesSecond
properties.maxMessageSizeBytes
Default: 262,144(256Kib)
properties.messageRetentionPeriodSeconds
Default: 345600(4days)
properties.longPollingSeconds
properties.visibilityTimeoutSeconds
Default: 30
properties.fifoEnabled
properties.fifoHighThroughput
properties.contentBasedDeduplication
properties.redrivePolicy
properties.alarms
properties.disabledGlobalAlarms
properties.policyStatements
overrides

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: function
properties:
packaging:
type: stacktape-lambda-buildpack
properties:
entryfilePath: path/to/my-lambda.ts
events:
- type: sqs
properties:
sqsQueueName: mySqsQueue
mySqsQueue:
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-queue
properties:
fifoEnabled: true
processing:
type: worker-service
properties:
packaging:
type: stacktape-image-buildpack
properties:
entryfilePath: processing.ts
connectTo:
- myQueue
resources:
cpu: 0.25
memory: 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 handling
console.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 (First-In-First-Out) queues have all the capabilities of the standard queues, but are designed to enhance messaging between applications when the order of operations and events is critical, or where duplicates can't be tolerated.
  • For more information on FIFO queues refer to AWS docs

    When using FIFO, each message should contain MessageDeduplicationId or contentBasedDeduplication must be enabled.

resources:
mainQueue:
type: sqs-queue
properties:
fifoEnabled: true

Content-based deduplication

You can enable content-based deduplication to prevent duplicate messages from being sent.

  • Specifies whether to enable content-based deduplication. Deduplication explained:
  • During the deduplication interval, queue treats messages identifies duplicates and delivers only one copy of the message
  • Duplicate messages are recognized based on MessageDeduplicationId:
    • You may provide a MessageDeduplicationId explicitly.
    • If you do not provide MessageDeduplicationId, content-based deduplication is used(if enabled) - this means Amazon SQS uses a SHA-256 hash to generate the MessageDeduplicationId using the body of the message (but not the attributes of the message).

      FIFO must be enabled in order 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.

  • Receiving messages explained: When a consumer receives and processes a message from a queue, the message remains in the queue. Amazon SQS doesn't automatically delete the message. Because Amazon SQS is a distributed system, there's no guarantee that the consumer actually receives the message (for example, due to a connectivity issue, or due to an issue in the consumer application). Thus, the consumer must delete the message from the queue after receiving and processing it.
  • Visibility timeout explained: Immediately after a message is received, it remains in the queue(see above). To prevent other consumers from processing the message again, Amazon SQS sets a visibility timeout, a period of time during which Amazon SQS prevents other consumers from receiving and processing the message.
  • For more information about visibility timeout refer to AWS docs.
  • Visibility timeout must be from 0 to 43,200 seconds (12 hours)

You can set the visibility timeout using the visibilityTimeoutSeconds property.

resources:
mainQueue:
type: sqs-queue
properties:
visibilityTimeoutSeconds: 300

Contents