Queues.
Work waits its turn, so the thing doing the work never drowns.
A queue sits between the code that asks for work and the code that does it. The asker drops a message in and moves on. A worker takes messages off the front, one at a time, at a pace it can keep up. The first message in is the first one out.
This isn't a simulation. Each click sends a real message to a Cloudflare queue, and a real consumer spends two seconds on each one. Everyone on this page shares the same queue, so strangers' jobs may be ahead of yours.
Live queue demo
- Waiting
- 0
- Consumer
- Idle
- Done
- 31
- Updates
- Connecting
- Rate
- 1 job / 2.0s
Producer you
Click as fast as you like.
Queue Cloudflare Queues
Empty. Add a job to fill it.
Yours Other visitors'Front of the queue →
Consumer one at a time
Done newest first
- 034
- 033
- 032
- 031
- 030
31 completed in total
How it's wired.
Producer → Queue → Consumer- Producer
Your click sends a
POSTto/api/concepts/queues/jobs. The server writes the job on the board, callsqueue.send()and replies straight away with202 Accepted. It doesn't wait for the work. - Queue
Cloudflare holds the message until the consumer is free. The consumer takes one message per batch and one batch at a time.
- Consumer
queues/concept-jobs.tsmarks the job as started, works for two seconds, marks it done and acknowledges the message. - Board
A Durable Object keeps the record of every job, so a page that loads late still sees the queue as it stands.
- Live
Each change goes out on a server-sent events stream. That's how this page moves without refreshing.
// after the board records the job
await queues["concept-jobs"].send({ jobId });
return c.json(result, 202);export const maxBatchSize = 1;
export const maxConcurrency = 1;
export default defineQueue<JobMessage>(async (batch, env) => {
for (const message of batch.messages) {
// start → 2s of work → complete
await concept.processJob(message.body.jobId);
message.ack();
}
});When to reach for one.
Slow, spiky or fallible work- Email and notifications
- Nobody should wait for a mail server before their page loads.
- Incoming webhooks
- Acknowledge in milliseconds, process later and retry when something fails.
- Media and documents
- Resizing images, transcoding video and building PDFs are slow by nature.
- Absorbing spikes
- A burst of ten thousand orders becomes a steady stream that your database can keep up with.
The small print.
What real queues don't promise- At least once, not exactly once.
- A message can arrive twice. The board knows each job's state, so a duplicate is skipped. If a job is redelivered mid-work, the consumer shows “Delivered 2×”.
- Ordering is best effort.
- Cloudflare Queues don't promise strict first in, first out. One message per batch and one batch at a time get close. When order really matters, keep it in your data, not in your transport.
- Queues need backpressure.
- A queue with no limit just moves the outage. This one turns new jobs away at 100 waiting, or at 20 of yours.
- Cancelling doesn't unsend.
- Clearing your jobs takes them off the board. The messages still arrive, and the consumer skips them.