Prompt
How do I set up a cheap serverless background worker?
Latest observation
A cheap “serverless background worker” is usually best built from these pieces:
- A trigger that starts work
- A queue to hold jobs
- A function that processes jobs
- A dead-letter/error path for failures
Good cheap options
Option 1: AWS Lambda + SQS
Best if you want low cost and reliability.
- SQS stores background jobs
- Lambda polls SQS and processes each message
- CloudWatch logs output
- Optional: DLQ for failed jobs
Why this is good:
- Pay only when jobs run
- Easy to scale
- Very common pattern
- Cheap for low/medium traffic
Typical flow:
- Your app enqueues a job to SQS
- Lambda is triggered by SQS
- Lambda processes the job
- On success, message is removed
- On repeated failure, message goes to DLQ
Option 2: Google Cloud Functions / Cloud Run + Pub/Sub
Good if you’re on GCP.
- Pub/Sub as queue
- Cloud Functions or Cloud Run as worker
- Good for event-driven workloads
Option 3: Cloudflare Workers + Queues
Good for lightweight tasks and global latency.
- Cheap at small scale
- Nice developer experience
- Better for short jobs, not heavy compute
Option 4: Supabase Edge Functions + a DB queue
Good for very small setups, but less ideal for serious background processing.
Cheapest practical setup I’d recommend
If you want the simplest reliable setup:
- AWS SQS
- AWS Lambda
- DLQ
- CloudWatch logs
That gives you:
- near-zero cost when idle
- simple deployment
- no server to manage
Important design tips
Keep jobs short
Serverless workers are best for tasks that finish quickly:
- send emails
- resize images
- call APIs
- sync records
- generate reports in chunks
If a job takes a long time, split it into smaller jobs or use a container-based worker instead.
Make jobs idempotent
A queue can deliver messages more than once, so your worker should safely handle duplicates.
Example:
- check if job already processed
- use unique job IDs
- write results atomically
Use retries carefully
Set:
- max receive count
- visibility timeout
- DLQ for poison messages
Don’t use a database as a queue unless you must
It can work, but it’s usually less efficient and more error-prone than SQS/PubSub.
Example architecture on AWS
- API or webhook receives request
- API writes job to SQS
- Lambda consumes SQS
- Lambda processes job
- If it fails 3 times, message moves to DLQ
- Another Lambda or alert monitors the DLQ
Rough cost expectation
For small workloads, this can be extremely cheap:
- Lambda: often pennies to a few dollars
- SQS: very low cost
- CloudWatch logs: usually small unless very chatty
If you want the absolute cheapest possible
If you don’t need real-time processing:
- run a scheduled job on a free/cheap cron service
- have it poll a database or queue
- use a small always-free VM
But for a true serverless worker, Lambda + SQS is usually the best cheap option.
If you want, I can give you:
- a minimal AWS setup
- a Terraform example
- a Node.js/Python worker example