Have you ever tapped the "Create Account" or "Download PDF Report" button, then watched the loading page spin for 10-15 seconds with no answer?
Frustrating, right? The cause is almost always the same: the backend server executes heavy work synchronously inside the HTTP request-response cycle.
Yet there is a golden rule in modern backend architecture. Return the response to the user as fast as possible, and hand the heavy job to a background worker.
Let's go through why you have to apply this rule.
Key takeaways
- A background worker frees up the HTTP request-response cycle so the API can answer the user in milliseconds.
- This pattern keeps web server worker threads from locking up and running out of capacity during traffic spikes.
- A message broker like RabbitMQ or Redis holds the asynchronous task queue.
- An independent background worker handles sending email, generating documents, and processing media.
The Classic Problem: A Hanging HTTP Request
When a user sends an HTTP request, say to register a new account, the sequence often looks like this:
- Backend creates the user record in the database. (Fast, about 10 ms)
- Backend sends the verification email through SMTP or a third-party API. (Can take 2-5 seconds)
- Backend generates the default avatar and syncs analytics data. (Can take 1-3 seconds)
- Only after all of that finishes does the
200 OKresponse go back to the user.
It all looks safe when traffic is quiet. The real impact shows up when things get busy:
- Poor user experience. Users wait a long time just to learn their registration worked.
- Worker threads on the web server get locked. A server has a ceiling on simultaneous connections. When connections are held open for email delivery, the server can run out of capacity (thread exhaustion) and fall over during a traffic spike.
Think of a minimarket cashier who has to wait out every customer's long chat inside the store. The line behind grows longer, even though the cashier did nothing wrong. 😅
Cashier vs Cook: How a Restaurant Kitchen Works
To make it clearer, imagine ordering a steak in a restaurant.
- Without a background worker: the cashier takes the order, then walks into the kitchen, lights the stove, grills the meat for 20 minutes, plates it, and only then hands you the receipt. The line at the register snakes out the door because the cashier is busy cooking.
- With a background worker: the cashier takes the order, prints a ticket for the kitchen, and hands you a queue number within seconds. The cook in the kitchen, the worker, grills the meat separately. The cashier can serve the next customer without a pause.
The principle is exactly the same in backend work. Whoever takes the order should not be told to cook. A cashier does not need to be a chef.
The Solution: Asynchronous Task Queue & Background Worker
With an asynchronous task queue approach, the flow changes to this:
- The web server receives the request and stores the essential data, such as the user ID.
- The web server throws a task instruction, for example "send an email to user ID 123", into a queue (message queue/broker like Redis or RabbitMQ).
- The web server immediately returns a
200 OKresponse ("Registration successful, please check your email") in milliseconds. - Behind the scenes, a separate process called a background worker, for example Celery, RQ, or Temporal, pulls tasks from the queue and finishes them independently.
The Main Components in a Worker Ecosystem
- Producer (web API): the web application that receives requests from users and creates new tasks.
- Message broker / queue: the temporary holding area for the task queue, usually Redis or RabbitMQ.
- Consumer / worker: a script or separate process on the server that keeps listening to the queue and executing its tasks.
The arrangement is simple. The effect on your application's responsiveness is large.
A Practical Example in Python: FastAPI BackgroundTasks
For a simple case that does not need a big broker like Celery, FastAPI already ships a built-in BackgroundTasks module that is very practical:
import time
from fastapi import FastAPI, BackgroundTasks
app = FastAPI()
def send_welcome_email(email: str):
# Simulate a slow email delivery process
time.sleep(4)
print(f"Email selamat datang berhasil dikirim ke: {email}")
@app.post("/register")
async def register_user(email: str, background_tasks: BackgroundTasks):
# 1. Save the user to the database here...
# 2. Register the heavy task in the background
background_tasks.add_task(send_welcome_email, email)
# 3. Return the response to the user right away, without waiting for the email!
return {
"status": "success",
"message": "Registrasi berhasil! Email verifikasi sedang dikirim di latar belakang."
}
With that pattern, the /register endpoint responds in milliseconds, while email delivery keeps running safely in the background.
One important note. BackgroundTasks in FastAPI runs inside the same application process. It suits prototypes and light tasks, but if the server dies, the running task dies with it. At production scale, move the function into its own worker service that listens for messages from a broker like RabbitMQ or Redis.
So, When Do You Start Adding a Background Worker?
Freeing the main HTTP cycle from heavy work is the main secret behind modern applications that feel light and responsive.
Once your application starts having features like sending verification email, processing images or video, generating report documents, or integrating with third-party services, split them out into a background worker or a distributed queue like Celery. Users are happy because they do not wait, and your server stays solid when thousands of visitors arrive at once.
Do not wait for your traffic to break first before tidying up. Start with one heavy task.
Happy working, and may your requests always come home on time. 👋