Asynchronous Tasks
SST offers a couple of ways to run asynchronous tasks. They address different use cases, so let's look at them below.
Types
Queue
The Queue
construct uses Amazon Simple Queue Service (SQS) behind the scenes. A Queue
can have only one consumer that pulls the messages of the queue. A consumer is a Lambda function.
import { Queue } from "@serverless-stack/resources";
new Queue(stack, "MyQueue", {
consumer: "src/consumer.main",
});
You can also create a FIFO version of the queue.
new Queue(stack, "MyQueue", {
cdk: {
queue: {
fifo: true,
}
},
consumer: "src/consumer.main",
});
Example
Follow this tutorial on how to create a simple queue system in SST.
Topic
The Topic
construct supports a pub/sub model using Amazon SNS behind the scenes. A Topic
can have multiple subscribers.
import { Topic } from "@serverless-stack/resources";
new Topic(stack, "MyQueue", {
subscribers: ["src/subscriber1.main", "src/subscriber2.main"],
});
You can also create a FIFO version of the Topic.
new Topic(stack, "MyQueue", {
cdk: {
topic: {
fifo: true,
}
},
subscribers: ["src/subscriber1.main", "src/subscriber2.main"],
});
Example
This example shows you how to create a simple pub/sub system in SST.
KinesisStream
The KinesisStream
construct uses Amazon Kinesis Data Streams. It's similar to the Queue
in the way that the consumer pulls the messages, but it's designed to allow for multiple consumers. KinesisStream
also keeps a record of historical messages for up to 365 days, and consumers can re-process them. This makes it a good fit for cases where you are dealing with a large amount of messages or events.
import { KinesisStream } from "@serverless-stack/resources";
new KinesisStream(stack, "Stream", {
consumers: {
consumer1: "src/consumer1.main",
consumer2: "src/consumer2.main",
},
});
EventBus
The EventBus
construct uses Amazon EventBridge behind the scenes. Similar to Topic
, it's a pub/sub model. Added to that, you can archive the messages coming in to the EventBus
and replay them later.
import { EventBus } from "@serverless-stack/resources";
new EventBus(stack, "Bus", {
rules: {
rule1: {
eventPattern: { source: ["myevent"] },
targets: ["src/target1.main", "src/target2.main"],
},
},
});
Which one should I use?
You should always try to use the Topic
or Queue
constructs first. They are simple, lightweight, low latency, highly scalable, and have pay per use pricing. And use KinesisStream
or EventBus
if you are dealing with vast amount of data, or when you need more advanced functionality.