ARQ for FastAPI: Async Background Tasks
ARQ lets your FastAPI app offload heavy work to background workers, keeping the API responsive. It's a task queue built for asyncio. Use it for slow tasks like sending emails or processing data. The footgun is using blocking task libraries with async code.
WHY IT EXISTS: FastAPI is fast because it's asynchronous. But if an API endpoint runs a slow task, like processing a file for 30 seconds, it blocks the server from handling other requests. Task queues solve this by letting the API immediately return a 'job accepted' response and handing the actual work to a separate process.
THE MENTAL MODEL: Think of your FastAPI app as a restaurant's front counter. ARQ is the kitchen staff. A customer (API client) places a complex order (a slow task). The counter staff (FastAPI) takes the order, gives the customer a ticket number (job ID), and sends the order to the kitchen (ARQ worker via Redis). The customer can check their order status with the ticket number without waiting at the counter, and the counter is free to serve others.
HOW IT WORKS: A FastAPI endpoint receives a request. Instead of performing the long task itself, it enqueues a job onto a Redis queue. This is a fast, non-blocking operation. A separate ARQ worker process, running independently, listens to the Redis queue. When a new job appears, the worker picks it up and executes the corresponding async task function. The API can provide another endpoint for the client to poll the job's status using its unique ID.
WHEN TO USE IT: Use ARQ when a FastAPI endpoint needs to trigger a task that takes more than a few hundred milliseconds. This includes sending emails, generating reports, processing images, or calling slow third-party services. It's the go-to choice when your application is already built on asyncio, as it integrates seamlessly with async def functions.
WHEN NOT TO USE IT: If your application is synchronous (e.g., built with Flask or synchronous Django), Celery or RQ might be a more direct fit. For very simple, non-critical background tasks that don't need persistence or retries, FastAPI's built-in BackgroundTasks feature can be sufficient and requires no extra infrastructure.
ONE CANONICAL EXAMPLE: A user signs up. The FastAPI /signup endpoint needs to create a user record and send a welcome email. Creating the user is fast. Sending the email can be slow and might fail. The endpoint creates the user, then enqueues a send_welcome_email(user_id) task with ARQ. It immediately returns a success response to the user. An ARQ worker picks up the task and handles sending the email in the background, retrying if necessary.
Read the original → github.com
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.