Background jobs
Фоновые задачи
Work an application does without making the user wait for it to finish. For example, a server can accept a request to export a report, respond right away, and build the file separately.
Also known as: воркер, worker, celery
What makes a job a background job?
A background job is a unit of work that runs outside the path that must complete before a request can get its response. When a user asks for a large report, the server can acknowledge the request right away rather than keep the connection open while it builds a file. Other jobs, such as a nightly cleanup, run on a schedule without a user request at all. Background describes how the work runs, not how urgent it is.
How jobs get done
A common setup has a producer, a queue, and one or more workers. The producer submits a job with its type and arguments. A worker takes the job and runs the appropriate handler. For example, the queue can carry a report ID rather than the report data itself. The worker can then look up the data it needs. Celery is a Python task-processing system that provides worker software and can use a message broker such as RabbitMQ or Redis. The job is the work to do; the worker is what does it.
A queue can absorb a short burst of requests, but it does not make jobs run faster. If jobs consistently arrive faster than workers finish them, the delay before each job starts will grow. Watching the age of the oldest waiting job can reveal that problem even when the API itself still responds quickly.
Example: exporting a report
The client sends POST /exports. The server durably records the request and returns 202 Accepted with an ID. The client can then poll GET /exports/{id} for the status and get a download link once the worker has produced the CSV. The 202 means the server accepted the request, not that the export succeeded.
Imagine the worker writes the file and crashes before acknowledging the message. With a queue configured to redeliver unacknowledged work, another worker may run the same job. Writing to a storage key based on the export ID avoids producing a second separately named file. This is an example of idempotency: running the job again should not create an extra effect. For transient failures, use a limited number of retries with increasing delays; record jobs that still fail so they can be investigated.
Common interview questions
Is async/await enough? No. Awaiting a database call may let the server handle other requests while it waits, but the original request still needs the result before it can respond. Starting a task in memory and returning immediately does take it off the request path, but that task can disappear when the process restarts. Use durable storage when losing accepted work is not an option.
What if the database commit succeeds but publishing the job fails? A transactional outbox addresses this gap: save the business change and an outbox record in the same database transaction, then publish the record separately. Publication can still happen more than once, so the worker must be prepared for duplicate delivery.