Overview
Background jobs let you process work asynchronously without blocking API responses. Motia provides a robust queue system with automatic retries, dead letter queues, and distributed processing.Basic queue pattern
The most common pattern is to enqueue work from an API endpoint and process it in a separate step.Scheduled jobs with Cron
Run jobs on a schedule using cron expressions:steps/periodic-cleanup.step.ts
Cron expressions use standard format:
minute hour day month weekday* * * * *- Every minute0 * * * *- Every hour0 0 * * *- Daily at midnight0 9 * * 1- Every Monday at 9 AM
Processing batch jobs
Process multiple items from a queue in a scheduled batch:steps/batch-processor.step.ts
Retry logic
Motia automatically retries failed queue processing. You can also implement custom retry logic:Dead letter queue
Handle permanently failed jobs with a dead letter queue:steps/handle-failed-orders.step.ts
Queue priorities
Process high-priority items first using conditional triggers:Best practices
Keep jobs idempotent
Design jobs to produce the same result if run multiple times. Use unique IDs to track processed items.
Use appropriate timeouts
Set realistic timeouts for long-running jobs. Break very long jobs into smaller chunks.
Monitor queue depth
Track how many items are waiting in queues. Scale up processing if queues grow too large.
Implement dead letter queues
Always have a fallback for permanently failed jobs. Alert humans when manual intervention is needed.
Next steps
Real-time Streaming
Add WebSocket support for real-time updates
AI Integration
Build AI-powered workflows with LLMs