An API accepts an engineering calculation, a worker completes it, and the process dies before acknowledging the message. The queue may deliver the task again. If the second execution creates another report, sends another email or charges for the same operation, a retry has changed the business outcome.

Reliable background work starts by defining what repeated delivery means. The queue cannot decide that for the application.

Separate acceptance from completion

For long-running work, an API can persist a job and return an accepted response with an identifier and status URL. The client then has something durable to inspect after a timeout or reconnect.

FastAPI’s in-process background tasks are useful for suitable lightweight follow-up work, but they are not a substitute for a durable job system. Its documentation points to tools such as Celery when work needs separate processes or servers. Choosing a queue also requires understanding its acknowledgement and redelivery settings.

Scope the idempotency key

An idempotency key should identify one intended operation within an authenticated scope. Store a fingerprint of the normalised request with it. Reusing a key with a different payload should produce a clear conflict, rather than silently returning the result of a different operation.

A minimal data constraint might look like:

CREATE TABLE job_requests (
    id uuid PRIMARY KEY,
    tenant_id uuid NOT NULL,
    idempotency_key text NOT NULL,
    payload_hash text NOT NULL,
    status text NOT NULL CHECK (
        status IN ('pending', 'running', 'succeeded', 'failed')
    ),
    UNIQUE (tenant_id, idempotency_key)
);

This schema illustrates uniqueness, not a complete worker implementation. The conflict path still needs to compare hashes, authorise access and return the existing job state. Expiry and retention policies also need to be explicit.

Close the database-to-queue gap

Persisting a job and publishing a message are separate operations. A crash after the database commit but before publication can leave work permanently pending. Publishing first creates the opposite risk: a worker sees a job that never commits.

One option is a transactional outbox: write the job and an event record in the same database transaction, then have a dispatcher publish committed events. The dispatcher can still publish twice if it crashes before recording success, so consumers must remain idempotent.

Do not describe an outbox as an exactly-once guarantee. It gives the system a durable record from which delivery can be retried.

Make worker state recoverable

Use guarded state transitions and a lease or another concurrency-control strategy so two workers cannot both assume they own the same attempt. If leases expire, consider how a stale worker is prevented from committing after a replacement has started. External side effects need their own protection, such as a provider-supported idempotency key or a reconciliable delivery record.

Retry transient failures with a bounded policy and jitter. Invalid input is not likely to improve after ten attempts. Make permanent failure visible with a reason and a controlled recovery path.

Test the uncomfortable moments

Deliver the same message twice. Kill a worker after saving the result but before acknowledgement. Interrupt publication after the outbox commit. Run two workers against the same job and simulate an unavailable downstream service.

The expected result is one intended business outcome, a traceable set of attempts and a status the caller can trust. That is a stronger reliability target than a worker that only succeeds when nothing interrupts it.

References