FastAPI + Celery — Background Jobs for Cloud ERP
Using Redis and Celery with FastAPI to run PDF generation, email notifications, and heavy imports without blocking API responses in Customer Flow.
ERP systems generate PDFs, send approval emails, recalculate landed costs, and import thousands of spreadsheet rows. Doing that inside a FastAPI request handler is a reliability trap. Customer Flow uses Celery + Redis for all of it.
What runs async
Typical background jobs in a trade ERP:
- PDF generation — packing lists, customs forms, invoice packs
- Email notifications — approval requests, secure download links
- Bulk imports — product catalogs, vendor price lists
- Landed cost recalculation — after duty or freight updates
- Audit log compaction — archival without blocking writes
Each job is triggered from an API endpoint that validates permissions, writes a job record, and returns immediately.
Task design rules
- Tenant context in every task — pass
organization_idexplicitly; never rely on global state - Retries with backoff for transient failures (SMTP, external APIs)
- Dead letter queue for jobs that fail after max retries — ops can inspect and replay
- Result backend stores completion status the frontend can poll if needed
FastAPI integration
API routes enqueue with .delay() or .apply_async() after the DB transaction commits. Ordering matters: enqueue after commit so workers never read uncommitted rows.
OpenAPI documents the synchronous response shape (job_id, status: pending). Clients pair this with WebSocket or polling updates from the gateway layer.
Redis dual role
Redis serves as:
- Celery broker and result backend
- Session and cache layer for hot reads
- Rate limit counters for public export APIs
Separate Redis DB indexes or key prefixes keep Celery traffic isolated from cache keys.
Operations
Monitor queue depth, task failure rates, and p95 task duration. Alert when PDF workers fall behind during month-end close — that is when import/export teams generate the most documents.
Takeaway
If your ERP does real work, plan for workers on day one. FastAPI stays fast when it only validates, persists, and enqueues. Celery owns the slow path.