// 09 · architecture
Queues and background jobs
Move everything slow or fragile out of the request-response path: the API enqueues, workers process at their own pace and scale with the queue, and whatever keeps failing ends up isolated in a dead-letter queue.
Code and layout on GitHub ↗What it must guarantee
- The API responds as soon as the job is enqueued, without waiting for the result.
- A job can run more than once without producing duplicate side effects.
- Transient failures are retried with growing back-off; persistent ones go to a DLQ.
- Workers scale with queue depth and drop to the minimum when there is no work.
- The status of every job can be queried at any time.
- Scheduled jobs neither overlap nor go missing silently.
On-premises RabbitMQ as the queue with its dead-letter exchange, workers on Kubernetes scaled by KEDA on queue length and a `CronJob` for periodic work.
- Use quorum queues: they replicate messages and enforce a delivery limit, so a poison message ends up in the DLX instead of being requeued forever.
- KEDA reads the queue length and scales the
Deployment(or launchesJobs), down to zero. - On the
CronJob,concurrencyPolicy: Forbidprevents overlaps andstartingDeadlineSecondsdecides what happens to missed runs. - RabbitMQ has no built-in delayed retry: build it with TTL wait queues that dead-letter back to the original queue, or with the delayed message plugin.
- The cost is running the RabbitMQ cluster (replicas, disks, upgrades) and node capacity for peaks.
AWS The API and EventBridge Scheduler publish to SQS; workers are ECS Fargate tasks scaled on backlog or Lambda functions behind an event source mapping, and the DLQ is another SQS queue with a redrive policy.
- The SQS visibility timeout must exceed the job duration; with Lambda, AWS recommends at least six times the function timeout.
- With Lambda, enable
ReportBatchItemFailuresso one bad message doesn't force the whole batch to retry. - Lambda fits jobs that take minutes; for long or memory-hungry jobs, use Fargate with autoscaling on backlog per task.
- Standard SQS delivers at least once with no ordering; FIFO adds per-group ordering and deduplication at the cost of throughput.
- SQS bills per request and Lambda per invocation and duration; Fargate per task time, so scaling down to zero or the minimum matters.
Azure Service Bus queues with their dead-letter subqueue, workers on Container Apps scaled by the KEDA Service Bus scaler, and Container Apps Jobs for scheduled work.
- Every Service Bus queue has a dead-letter subqueue; a message lands there after exceeding the maximum delivery count, or on expiry if you enable it.
- Receive in peek-lock mode and renew the lock on long jobs: if it expires, another worker gets the same message.
- Container Apps supports KEDA scale rules on apps and also event-driven jobs, which suit cases where each message is a long job.
- Service Bus scheduled messages give you delayed retries with no extra infrastructure.
- The Standard tier bills per operation; Premium has a fixed cost per messaging unit in exchange for isolation and predictable performance.
Google Cloud The API and Cloud Scheduler publish to Pub/Sub; a push subscription delivers each message to Cloud Run and a dead-letter policy sets aside the ones that fail.
- With push, Cloud Run scales on incoming requests, not on depth: Pub/Sub adjusts the delivery rate based on responses. For continuous pull consumption there are Cloud Run worker pools.
- If you need rate limiting, per-task scheduling or deduplication by name, Cloud Tasks is a better job queue than Pub/Sub.
- Dead-lettering is set on the subscription and needs the Pub/Sub service account to have permissions on the target topic; the attempt count is approximate.
- The ack deadline and the Cloud Run request timeout must cover the job duration; for long batches, Cloud Run jobs are a better fit.
- Pub/Sub bills by data volume; Cloud Run by CPU and memory while serving requests.
Options by cost and scope
From the cheapest option to the most resilient. The "How it is built" column follows the selected platform.
| Option | Cost | Operations | How it is built | What you give up |
|---|---|---|---|---|
| Local queue or serverlessA small product with jobs of seconds or a few minutes, modest volume and a team that doesn't want to run a broker. | €€€€ | On-premisesOne host with Docker Compose: Celery with Valkey (Redis) as the broker, a single `celery beat` for scheduled work and PostgreSQL for job status; RabbitMQ if you can't accept losing messages on restart.AWSSQS with its DLQ, Lambda behind an event source mapping as the worker and EventBridge Scheduler for periodic work.AzureService Bus Standard, Container Apps at zero minimum replicas scaled by KEDA and Container Apps Jobs with a schedule trigger.Google CloudPub/Sub with a push subscription to Cloud Run at zero minimum instances, a dead-letter policy and Cloud Scheduler. | Duration limits (15 minutes on Lambda, the request timeout with push) and little control over pace; with Celery on Redis, delivery depends on the persistence you configure. | |
| Autoscaled workersSustained volume, jobs of up to hours and several job types with different priorities. | €€€€ | On-premisesRabbitMQ with quorum queues on three nodes, workers on Kubernetes scaled by KEDA (down to zero) and a `CronJob` with `concurrencyPolicy: Forbid`.AWSSQS and ECS Fargate tasks scaled on backlog per task, with Fargate Spot for capacity that can be interrupted.AzureService Bus with a queue per job type, Container Apps with the KEDA Service Bus scaler and event-driven jobs for long work.Google CloudPub/Sub with Cloud Run worker pools pulling, or Cloud Tasks when you need to limit the rate, and Cloud Run jobs for batches. | You have to tune visibility, locks, concurrency and scaling limits; the baseline cost rises if you keep workers warm. | |
| Batch executor for long and GPU jobsRendering, transcoding, batch inference or hours-long calculations that need GPUs, lots of memory or Spot machines. | €€€€ | On-premisesKubernetes with Kueue to queue `Job`s and share them across GPU nodes (NVIDIA GPU Operator), with per-team quotas.AWSAWS Batch with EC2 compute environments (GPU and Spot) or Fargate; EventBridge Pipes carries SQS messages to the Batch job queue.AzureAzure Batch with GPU VM pools and Spot VMs, fed from Service Bus by a dispatcher.Google CloudGoogle Cloud Batch with GPUs and Spot VMs, launched by a dispatcher on Cloud Run from Pub/Sub. | Start-up takes minutes (provisioning VMs and pulling the image), there is one more component to run, and bills grow fast if you don't cap vCPUs and GPUs. |
Extensions
Pieces you add to the base design when you need them.
| Extension | When | Service | Cost impact |
|---|---|---|---|
| Delayed retry | When failures come from third parties that recover on their own and retrying immediately only makes things worse. | On-premisesTTL wait queues or delayed pluginAWSSQS visibility timeout changeAzureService Bus scheduled messagesGoogle CloudCloud Tasks with a schedule time | Low |
| Rate control towards third parties | When an external API has a quota that worker autoscaling would blow through. | On-premisesToken bucket in ValkeyAWSSQS-Lambda maximum concurrencyAzureContainer Apps max replicasGoogle CloudCloud Tasks rate limits | Low |
| Priorities | When interactive jobs cannot wait behind a nightly batch. | On-premisesOne RabbitMQ queue per priorityAWSOne SQS queue per priorityAzureOne Service Bus queue per priorityGoogle CloudOne topic and subscription per priority | Low |
| Batch executor with GPUs | When some jobs run for hours, need GPUs or can run on Spot with retries. | On-premisesKueue + Jobs on GPU nodesAWSAWS BatchAzureAzure BatchGoogle CloudBatch (Google Cloud) | High |
| Multi-step flows | When a job chains steps with waits, compensations or human approval. | On-premisesTemporalAWSStep FunctionsAzureDurable FunctionsGoogle CloudWorkflows | Medium |
| Completion notification | When the client shouldn't poll for status and would rather get a webhook or a live notification. | On-premisesSSE from the API + webhooksAWSAmazon SNSAzureEvent GridGoogle CloudPub/Sub (results topic) | Low |
How to save
- Run idempotent workers on interruptible capacity: Fargate Spot, Spot in AWS Batch, Spot VMs in Azure Batch and Spot VMs in Google Cloud Batch. An idempotent worker survives the interruption without losing work.
- Scale to zero outside working hours: KEDA on Kubernetes, Container Apps with a minimum of zero, Cloud Run and Lambda charge no compute while the queue is empty; the fixed cost stays with the broker.
- On SQS, use long polling (
WaitTimeSecondsup to 20 s) and batch sends and receives: you pay per request, and empty receives are requests too. - Lambda on
arm64and Fargate on Graviton cost less per unit of compute than x86; worker images usually build for ARM unchanged. - Apply lifecycle rules to results in object storage: automatic expiry for temporary output and infrequent-access or archive classes for what you must keep.
- Keep Service Bus on Standard while volume allows; Premium has a fixed cost per messaging unit and only pays off when you need isolation or predictable throughput.
Equivalents by platform
| Piece | On-premises | AWS | Azure | Google Cloud |
|---|---|---|---|---|
| API (producer) | Kubernetes (RKE2/k3s) | ECS Fargate | Container Apps | Cloud Run |
| Scheduler (cron) | Kubernetes CronJob | EventBridge Scheduler | Container Apps Jobs | Cloud Scheduler |
| Job queue | RabbitMQ | Amazon SQS | Service Bus (queues) | Pub/Sub |
| Dead-letter queue | RabbitMQ DLX | SQS DLQ | Service Bus DLQ | Pub/Sub dead-letter |
| Autoscaled workers | Kubernetes + KEDA | ECS Fargate / Lambda | Container Apps (KEDA) | Cloud Run (push) |
| Results (objects) | Ceph RGW | Amazon S3 | Blob Storage | Cloud Storage |
| Job status | PostgreSQL + Patroni | Aurora PostgreSQL | Azure DB for PostgreSQL | Cloud SQL (PostgreSQL) |
In depth
When you need it
When a request triggers something that takes longer than a user should wait, or that depends on an unreliable third party: rendering a PDF, sending a thousand emails, rebuilding a report, calling a rate-limited external API. Also when there is periodic work (daily closes, clean-ups, syncs). Do it inside the request and a traffic spike or a slow third party takes the API down; with a queue, the spike turns into delay.
Scenarios
An MVP with Celery and Redis
A Python team with a single server doesn’t need a managed broker on day one. Celery with Valkey (or Redis) as the broker and celery beat for periodic work cover the case in one docker-compose.yml. Two conditions: turn on AOF persistence on the broker or accept that a restart loses messages, and set visibility_timeout above your longest job, because with Redis an unacknowledged job is redelivered to another worker when it expires. If those guarantees matter, switch the broker to RabbitMQ; the task code doesn’t change.
Long or GPU jobs
Transcoding video, rendering or running batch inference doesn’t fit in a function or an always-on worker. Here the executor changes: the API still enqueues the same way, but a dispatcher hands those jobs to AWS Batch (or Azure Batch, Google Cloud Batch, Kueue on-premises), which provisions GPU or Spot machines, runs the container and shuts them down. Batch adds per-attempt retries, per-job timeouts and array jobs.
Critical scheduled jobs
An accounting close or monthly billing must neither run twice nor be skipped. The scheduler only enqueues a message with a period key (close:2026-11-24), and a unique constraint on the jobs table prevents the duplicate. Schedule in UTC or an explicit time zone (EventBridge Scheduler and Cloud Scheduler accept a time zone; the CronJob has timeZone), watch daylight saving changes and alert if the expected run hasn’t finished on time.
Integrating with a rate-limited third party
If the provider allows a hundred requests a minute, autoscaling is your enemy. Cap the concurrency of that queue’s workers or use a queue with rate control (Cloud Tasks), honour Retry-After, and keep that provider’s messages on their own queue so its outage doesn’t block everything else.
How it fits together
The API validates the request, records the job as pending in the database, enqueues a message carrying its ID and returns 202 Accepted with a status URL. The scheduler does the same at fixed times: it never runs the job itself, it only enqueues it, so it inherits retries, the DLQ and scaling. The queue absorbs the burst, and its depth is the scaling signal.
Each worker takes a message, does the work, stores the heavy output in object storage, updates the job status in the database and only then acknowledges the message. If it fails, the message reappears after the visibility timeout or lock expires and is retried. After N attempts it moves to the dead-letter queue, where someone inspects it and decides whether to replay it.
The executor is swappable as long as the contract is stable: a message with job_id, type, idempotency key and references to the data (never the data itself), and a container image with an entry point like run-job <job_id>. That same image runs as a Celery worker, a Fargate task or an AWS Batch job; what changes is who starts it. Batch job state change events update the same jobs table.
Deployment: Docker, Kubernetes or serverless
Docker Compose on one host. Containers for api, worker (with --concurrency matched to the CPU), a single beat, the broker (Valkey with AOF or RabbitMQ) on a persistent volume and PostgreSQL for status. Results go to S3-compatible storage (Ceph RGW if you already have it; on a single host, Garage or SeaweedFS). Back up the database; the broker only holds pending work, but its volume stops you losing it on restart.
Kubernetes on-premises. The RabbitMQ operator creates the cluster with quorum queues, KEDA scales the worker Deployment with a ScaledObject or launches one Job per message with a ScaledJob, and CronJobs cover periodic work. CloudNativePG or Patroni for status. For GPUs, the NVIDIA GPU Operator and Kueue, which queues Jobs until quota and nodes are available.
Cloud without Kubernetes. This is the design in the diagram: SQS with Lambda or Fargate, Service Bus with Container Apps and Container Apps Jobs, Pub/Sub with Cloud Run and Cloud Scheduler. For long or GPU jobs, AWS Batch, Azure Batch or Google Cloud Batch.
Managed Kubernetes. EKS, AKS or GKE pay off if they already run the API and you want one model for services, workers and batches. KEDA is an add-on on AKS and installs with Helm elsewhere; on EKS, Karpenter provisions Spot or GPU nodes only while there are jobs.
| Option | Fixed cost | Operations | Scaling | When |
|---|---|---|---|---|
| Compose + Celery | One host | Medium | Manual | MVP, low volume |
| Kubernetes + KEDA | Cluster and broker | High | On queue depth, to zero | On-premises, many job types |
| Managed queue + serverless | Near zero | Low | Automatic | Default choice in the cloud |
| Batch service | Zero with no jobs | Medium | Per job, with Spot and GPUs | Hours-long or GPU jobs |
Key decisions
- Containers or functions for workers. Functions scale on their own and bill per use but have a duration limit; containers handle long jobs and heavy dependencies.
- Executor by job type. Queue and workers for short, frequent work; a batch service for long, GPU or Spot work. With a shared contract, moving a job type from one to the other is configuration.
- Queue or bus. Work that exactly one kind of worker must do belongs on a queue. If several systems react to the same fact, that is an event and belongs on a bus.
- Push or pull. Push keeps the worker simple (it’s an HTTP endpoint) but hands over control of the pace; pull gives fine-grained concurrency control in exchange for managing consumption.
- Managed or self-hosted. RabbitMQ offers routing flexibility; a managed queue removes operations but imposes its own size and duration limits.
Common pitfalls
- Assuming exactly-once delivery. All of these queues deliver at least once: workers must be idempotent, for example with an idempotency key and a unique constraint in the database.
- Retrying with no back-off and no limit, turning a third party’s outage into an attack on it.
- A visibility timeout shorter than the job: two workers process the same message.
- Starting two
celery beatprocesses (or two replicas of a home-grown scheduler): every scheduled job runs twice. - A DLQ with no alarm and no replay procedure, which just piles up.
- Putting the whole file in the message instead of a reference to the object.
These are starting points, not recipes: size, cost and compliance requirements change the choice.