Docs

Queue

A Queue is a message queue that hands work from one part of your code to another in the customer's cloud: background jobs, event pipelines, and work that must survive a crash. Messages are delivered at least once on SQS, Pub/Sub, Service Bus, or a local queue, through one API.

alien.ts
import * as alien from "@alienplatform/core"

const tasks = new alien.Queue("tasks").build()

const api = new alien.Worker("api")
  .code({ type: "source", src: "./api", toolchain: { type: "typescript" } })
  .link(tasks)
  .permissions("producer")
  .build()

const processor = new alien.Worker("processor")
  .code({ type: "source", src: "./processor", toolchain: { type: "typescript" } })
  .trigger({ type: "queue", queue: tasks.ref() })
  .build()

export default new alien.Stack("acme")
  .add(tasks, "frozen")
  .add(api, "live")
  .add(processor, "live")
  .permissions({
    profiles: { producer: { tasks: ["queue/publish"] } },
  })
  .build()

In this stack:

  • .link(tasks) connects the queue to the api Worker, the workload (the running code) that sends to it. Alien injects the queue's connection details, its binding, into the Worker, read by the SDK client.
  • The producer permission profile, the named set of cloud permissions a workload runs with, grants queue/publish.
  • The processor Worker gets messages through its trigger instead of a link.
  • "frozen" means the customer's admin creates the queue during initial setup. "live" resources, like the Workers, are created and updated by Alien on every release. See Frozen and live.

The api Worker sends messages, and the processor Worker handles them:

api/src/index.ts
import { queue } from "@alienplatform/sdk"

await queue("tasks").send({ imageId: "img_123" })
processor/src/index.ts
import { onQueueMessage } from "@alienplatform/sdk"

onQueueMessage<{ imageId: string }>("tasks", async message => {
  await resize(message.payload.imageId)
})

Platforms

tasks becomes a queue in the customer's cloud:

PlatformBacking serviceCreated by
AWSSQS standard queueAlien
GCPPub/Sub topic and pull subscriptionAlien
AzureService Bus queue in a Standard namespace that Alien adds to the stackAlien
KubernetesSQS, Pub/Sub, or Service BusThe customer, through an external binding
MachinesSame services as KubernetesThe customer, through an external binding. The resource must be "frozen". See Machines.
LocalEmbedded database file (localqueue.sqlite)Alien
  • Kubernetes: Alien doesn't create the queue. The build fails unless each Queue resource has an external binding of type queue.
  • Kubernetes triggers: Worker queue triggers aren't available. Consume with receive instead.

How delivery works

  1. A producer sends a JSON or text message.
  2. A consumer receives it, which starts a lease.
  3. The consumer processes the message and acknowledges it.
  4. If the lease runs out before the acknowledgment, the message is delivered again.

Frozen or live

Queue can be "frozen" (created once during initial setup by the customer's admin) or "live" (created and updated by Alien on releases). See Frozen and live.

The resource ID must be lowercase letters, digits, and hyphens.

Alternatives

  • For request-response calls, call the Worker or send a command.
  • For ordered event streams, use something else: queues don't guarantee order.

Options

OptionDescription
.deadLetter(queue, { maxAttempts })Moves a message to another Queue of the stack after maxAttempts failed deliveries (5 to 100, default 5). See Dead-letter queue.
.enabled(input)Creates the resource only when a boolean stack input is true.

Visibility timeouts, lock durations, and ack deadlines are derived from the Workers the queue triggers. See Behavior and limits.

Use it from code

Get a handle with queue(name). The handle is bound to its queue, so methods don't take a queue name.

src/tasks.ts
import { queue } from "@alienplatform/sdk"

const tasks = queue("tasks")

await tasks.send({ imageId: "img_123" })

for (const message of await tasks.receive(10)) {
  const body = JSON.parse(message.payload) as { imageId: string }
  await resize(body.imageId)
  await tasks.ack(message.receiptHandle)
}

The full method list is in the API reference.

Trigger a Worker

With a queue trigger, the Worker runs for each message, as processor does above. You don't call receive or ack:

  • Returning from the handler acknowledges the message.
  • Throwing makes it redeliver.

For higher throughput, add batch: { maxMessages, maxWindowSeconds } to the trigger and register onQueueBatch. It returns a success or retry outcome per delivery.

A Worker can have one queue trigger. See Events and triggers.

Common patterns

Long-running jobs

The default lease is 30 seconds. For longer work, renew the lease while you work, or another consumer gets the message:

const [message] = await tasks.receive(1)
const timer = setInterval(() => tasks.renewLease(message.receiptHandle, 120), 60_000)
try {
  await longJob(message)
  await tasks.ack(message.receiptHandle)
} finally {
  clearInterval(timer)
}

Retry limits

attempt counts deliveries, starting at 1. Use it to stop retrying a message that keeps failing:

if (message.attempt > 5) {
  await deadLetters.put(`failed/${crypto.randomUUID()}.json`, Buffer.from(message.payload))
  await tasks.ack(message.receiptHandle)
}

SQS and the local queue report real counts. Pub/Sub reports real counts for a queue with a dead-letter queue and 1 otherwise. Service Bus always reports 1 through receive. To park messages that keep failing without writing this code, use a dead-letter queue.

Dead-letter queue

A message that fails again and again is retried differently on each cloud: until SQS retention ends, for days on Pub/Sub, or parked in a Service Bus queue nothing reads. Name a second Queue, and a message that fails maxAttempts times moves there with its payload:

alien.ts
const failed = new alien.Queue("tasks-failed").build()
const tasks = new alien.Queue("tasks").deadLetter(failed, { maxAttempts: 5 }).build()

tasks-failed is a normal Queue. Link it, receive from it, or trigger a Worker from it:

src/review.ts
for (const message of await queue("tasks-failed").receive(10)) {
  await alertSomeone(message.payload)
  await queue("tasks-failed").ack(message.receiptHandle)
}

Both queues must be "frozen" or both "live", and the target can't have a dead-letter queue itself. There is no redrive call: to retry a message, send it to tasks again. The count is approximate, and the payload is kept without a failure reason. See Dead-letter queues for what each cloud does and how to change it later.

Large payloads

Messages are limited to 64 KiB. Put larger payloads in Storage and send the key.

New objects in Storage

A Storage can put an event into a queue for every new object that matches a prefix and suffix. Declare it on the Storage with .notify(queue, rule), and read each message with storageEventFromMessage. See Notify a queue.

Send from your hosted software

Publish the queue as a remote binding to send messages into a customer's environment from your backend.

Remote queues are send-only: send, sendText, sendBatch, and sendBatchText.

Permissions

SetGrants
queue/data-readReceive, acknowledge, nack, and renew leases.
queue/data-writeSend, and acknowledge received messages.
queue/publishSend only, for producers that never consume.
queue/batch-metadataReading the queue's visibility setting, which a batched Worker trigger depends on.
queue/storage-notificationsLetting Cloud Storage publish storage notifications into a live queue. Used by the management identity on Google Cloud.
queue/provision, queue/management, queue/heartbeatUsed by initial setup and by Alien's management identity.

When a workload's profile doesn't name the queue, .link() grants queue/data-read and queue/data-write, and a queue trigger grants queue/data-read. Naming the queue in the profile replaces those defaults. See Permissions.

Next

On this page