Queue producers and consumers
Send and consume messages
Bind a producer and consumer, enqueue only after authoritative state exists, and process each batch explicitly.
8 minute lesson
A Worker producer calls its Queue binding after validating and storing the request. Return 202 with the job ID when the result will be available later.
The consumer receives a batch. Handle each message, acknowledge successes, and retry failures according to the current API. One bad message should not repeat unrelated successful work when per-message decisions are available.
Enqueue three export jobs, fail the middle one, and verify only the intended message follows the retry path.
Send a small versioned message:
await env.EMAIL_QUEUE.send({
version: 1,
messageId,
userId,
template: 'welcome'
})
The consumer should validate the version and required fields before doing work. Keep secrets and full user records out of the message. Test a valid message, an unknown version, malformed data, and a provider failure before increasing batch size.
Lesson completed