Think Build Implement Repeat
London, UK +44 7367 067226
WhatsApp FOLLOW f in X
Python & Django

Work That Should Not Block a Request

Last updated:

What belongs in a queue

  • Email and notifications
  • Calls to external systems
  • Report and document generation
  • Bulk operations across many records
  • Anything taking more than a moment
Sending email during a request is the most common cause of a slow form submission and of a form that appears to fail when it actually succeeded.

Task design

  1. Pass identifiers, not objects — the record may change before the task runs
  2. Idempotent — a retry must not double the effect
  3. Bounded retries with backoff
  4. Explicit timeouts
  5. Logged, with the originating request identifier

Watch out for transactions

A task queued inside a database transaction may start before the transaction commits, and then fail to find the record it was given.

Queue tasks after the transaction commits. Django provides a hook for exactly this, and it prevents a class of intermittent failure that is difficult to diagnose.

Monitor the queue

SignalMeans
Depth growingWorkers stopped or too few
Failed queue growingSomething systematically wrong
Zero tasks processedWorkers not running
Duration increasingSomething degrading
Retries increasingAn external dependency struggling

The failed queue needs an owner

Most applications have a dead letter queue built during development and never checked. Work fails, lands there, and stays there indefinitely.

It should alert when it grows and somebody should review it regularly. Otherwise it is a silent hole where work disappears.

Frequently asked questions

Which task queue with Django?

The established options integrate well. Choose on operational familiarity rather than feature comparison.

Can we use the database as a broker?

At modest volume, yes, and it is simpler to run. Dedicated brokers earn their place at higher throughput.

How do we test tasks?

Run them synchronously in tests, and separately test that they are queued when expected.

What about scheduled work?

A scheduler that enqueues tasks, so scheduled work gets the same retries and observability as everything else.

Keep reading

Forms that take five seconds to submit?

It is usually email or an external call in the request. Moving it to a queue is a contained change.

Book a free 30-minute call Get a project estimate WhatsApp us

Related services

What we build for problems like this one

Custom Software DevelopmentWeb DevelopmentMachine Learning