Queues
Run slow work in the background, so requests return straight away. Pixelflare uses four queues: variants (plus AI classification), S3 backups, custom domain verification, and webhook delivery.
Setup
Enable queues with:
ENABLE_QUEUES='true'Queues are provisioned by Terraform and bound in wrangler.toml.
They work on Cloudflare's free plan, which includes 10,000 queue operations a day. The $5/month Workers Paid plan includes 1 million a month, then $0.40 per extra million. Delivering one message usually takes three operations (a write, a read and a delete), and each retry adds a read. See Cloudflare's Queues pricing for the latest.
Why They're Needed
Some work is too slow, or too unreliable, to do while someone waits:
- AI classification - Running the image models would slow every upload down
- S3 backup - Uploading to external storage adds latency, and can fail
- Domain verification - Cloudflare can take a while to verify a custom hostname, so it has to be checked repeatedly
- Webhooks - The receiving server might be slow or offline, so deliveries need retries
Running these in-request would risk hitting Workers' time limits, and keep users waiting for uploads to finish.
How It Works
After an upload completes, the API returns immediately and queues the slower work:
- Upload completes and the API queues one message per variant size (w128, w256, etc.)
- Variants are generated on demand by the CDN the first time each size is requested, then cached in R2
- Queue consumer records each variant against the image in D1, and runs AI classification if it's enabled
Backups, domain checks and webhooks work the same way: something queues a message, and a consumer picks it up in the background. Messages are processed in batches, and failed ones are retried automatically.
The Queues
Variant Queue
- Purpose: Record which variants each image has, and run AI classification
- Batch size: 5 messages
- Batch timeout: 30 seconds
- Retries: Up to 3
flowchart TB
subgraph VQueue["Queue"]
direction LR
VTrigger[Upload Complete or<br/>Variant First Served] --> VEnqueue["Enqueue to VARIANT_QUEUE<br/>Message: imageId, variant"]
VEnqueue --> VConsumer["Queue Consumer<br/>Batch: 5, Timeout: 30s"]
end
subgraph VRecord["Record"]
direction LR
VCheck[Check Variant<br/>Exists in R2] --> VUpdate[Add to variants_ready<br/>in D1]
end
subgraph VClassify["Classify"]
direction LR
VAIEnabled{AI Enabled?} -->|Yes| VAI[Run AI Classification<br/>on the Original]
VAIEnabled -->|No| VDone[Complete]
VAI --> VDone
end
VQueue --> VRecord --> VClassify
style VTrigger fill:#90E24A,color:#000Backup Queue
- Purpose: Copy original images to your S3-compatible storage
- Batch size: 10 messages
- Batch timeout: 60 seconds
- Retries: Up to 3, with a delay between each
flowchart TB
subgraph BQueue["Queue"]
direction LR
BTrigger[Nightly or<br/>Manual Sync] --> BEnqueue["Enqueue to BACKUP_QUEUE<br/>Message: imageId, owner"]
BEnqueue --> BConsumer["Queue Consumer<br/>Batch: 10, Timeout: 60s"]
end
subgraph BSync["Sync"]
direction LR
BFetch[Fetch Original from R2] --> BS3[Upload to S3-compatible<br/>AWS/MinIO/Backblaze]
end
BQueue --> BCheck{Plan Includes<br/>Backups?}
BCheck -->|Yes| BSync
BCheck -->|No| BSkip[Skip]
BSync -->|Success| BDone[Mark as Synced in D1]
BSync -->|Error| BFail[Mark as Failed<br/>and Retry Later]
style BTrigger fill:#4A90E2,color:#000Custom Domain Queue
- Purpose: Check with Cloudflare until a new custom domain is verified
- Batch size: 5 messages
- Batch timeout: 30 seconds
- Retries: Up to 40 checks (every 30 seconds for 20 minutes)
flowchart TB
subgraph DQueue["Queue"]
direction LR
DTrigger[Custom Domain Added] --> DEnqueue["Enqueue to CUSTOM_DOMAIN_QUEUE<br/>Message: owner, hostname"]
DEnqueue --> DConsumer["Queue Consumer<br/>Batch: 5, Timeout: 30s"]
end
DQueue --> DCheck[Ask Cloudflare for the<br/>Hostname Status]
DCheck --> DStatus{Status?}
DStatus -->|Active| DActive[Mark Active in D1<br/>and Cache in KV]
DStatus -->|Pending| DRetry{Checks < 40?}
DStatus -->|Error or Blocked| DFail[Save Error to D1]
DRetry -->|Yes| DRequeue[Check Again in 30s]
DRetry -->|No| DStop[Stop Checking]
DRequeue --> DQueue
style DTrigger fill:#E2904A,color:#000Webhook Queue
- Purpose: Deliver signed webhook events to your endpoints
- Batch size: 10 messages
- Batch timeout: 30 seconds
- Retries: Up to 3, with a delay between each
flowchart TB
subgraph WQueue["Queue"]
direction LR
WTrigger[Event Fired<br/>e.g. image.uploaded] --> WEnqueue["Enqueue to WEBHOOK_QUEUE<br/>One message per webhook"]
WEnqueue --> WConsumer["Queue Consumer<br/>Batch: 10, Timeout: 30s"]
end
WQueue --> WSend[POST Signed Payload<br/>to Your URL]
WSend --> WOk{Delivered?}
WOk -->|Yes| WDone[Complete]
WOk -->|No| WRetry{Retries < 3?}
WRetry -->|Yes| WWait[Retry After a Delay]
WRetry -->|No| WGiveUp[Give Up]
WWait --> WQueue
style WTrigger fill:#E24A90,color:#000Scheduled Jobs
Alongside the queues, three cron jobs handle cleanup, analytics and backup sync.
flowchart LR
Cron1[Daily 1 AM UTC:<br/>Cleanup Job]
Cron2[Every 15 Minutes:<br/>Analytics Import]
Cron3[Daily 3 AM UTC:<br/>Backup Sync]
Cron1 --> Clean1[Expire and Purge<br/>Deleted Images]
Cron1 --> Clean2[Clean Up Orphaned Uploads]
Cron1 --> Clean3[Trim Old Audit Logs]
Cron1 --> Clean4[Retry Variant Jobs<br/>That Missed the Queue]
Cron2 --> Agg1[Import Traffic from<br/>Analytics Engine]
Cron2 --> Agg2[Roll Up Finished Days]
Cron2 --> Agg3[Daily Storage Snapshot]
Cron2 --> Agg4[Trim Old Days<br/>to Monthly Totals]
Cron3 --> Sync1[Queue Pending Images<br/>for Backup]
style Cron1 fill:#7B42BC,color:#fff
style Cron2 fill:#7B42BC,color:#fff
style Cron3 fill:#7B42BC,color:#fffDisabling
To disable queues (development only):
ENABLE_QUEUES='false'Uploads still work, and variants are still generated when they're first requested. But variants won't be recorded in D1, AI classification won't run automatically, backups won't sync, webhooks won't be delivered, and custom domains won't be re-checked (you can still verify one manually from settings).