Chapter 34: Background Tasks and Services
ASP.NET Core has hosted services and background services. FastAPI has in-process background tasks, but they are only suitable for small, non-critical follow-up work.
Use in-process background tasks for:
- Sending a low-risk notification after a response.
- Writing an audit event when losing it is acceptable.
- Small cleanup work.
Use a real worker queue for:
- Email delivery that must retry.
- Image processing.
- Long imports and exports.
- Scheduled jobs.
- Work that must survive app restarts.
Python worker choices:
- Celery: mature, widely used, heavier.
- Dramatiq: simpler queue workers.
- ARQ: asyncio Redis queue.
- RQ: straightforward Redis jobs.
- APScheduler: scheduling inside a process, not a durable queue by itself.
RecipeVault background work:
- Import recipes from a URL.
- Export a user's recipes to CSV.
- Generate search indexes.
- Send account emails.
Keep background job code in application services so the web request and worker can call the same use case safely.