Scalability
Queue-Based Job Processing
Background jobs for heavy tasks with retries, backoff, and progress tracking.
Problem this solves
Long-running tasks inside API requests cause timeouts and poor user experience.
When to use it
- You run document processing, report generation, or batch sync tasks.
- Task duration can exceed normal request timeouts.
- You need retry semantics and failure observability.
Code snippet
typescript
// lib/jobs/process-documents.ts
import { queue } from '@/lib/queue';
export async function enqueueDocumentProcessing(documentId: string) {
await queue.add(
'process-document',
{ documentId },
{ attempts: 5, backoff: { type: 'exponential', delay: 3000 } }
);
}
queue.process('process-document', async (job) => {
await updateStatus(job.data.documentId, 'processing');
await runExtractionPipeline(job.data.documentId);
await updateStatus(job.data.documentId, 'completed');
});Integration guide
- Enqueue work from the request path and return quickly.
- Store job IDs and status for client polling or subscriptions.
- Configure retries with exponential backoff and dead-letter handling.
- Instrument queue depth, failure rate, and processing latency.
Next step
Explore the full documentation and variants for this pattern.
Open full docs