Think Build Implement Repeat
London, UK +44 7367 067226
WhatsApp FOLLOW f in X
PHP Development

Work That Should Not Happen During a Request

Last updated:

The request should be fast

A user submitting a form should not wait while an email is sent, a PDF is generated and three external systems are notified. Each of those can fail, and each makes the request slower.

Sending email during a web request is the most common cause of slow form submissions, and the most common reason a form appears to fail when it actually succeeded.

What belongs in a queue

  • Email and notifications
  • Calls to external APIs
  • Report and document generation
  • Image and file processing
  • Bulk data operations
  • Anything that takes more than a moment

What queues give you

  1. Fast responses, because the work happens afterwards
  2. Retries when something transient fails
  3. Durability — work survives a restart
  4. Rate control for external services with limits
  5. Visibility into what failed and why

Design jobs to be retryable

RequirementWhy
IdempotentA retry must not duplicate the effect
Small payloadPass an identifier, not the whole object
Bounded attemptsFailing jobs must not retry forever
Failure handlingA failed job queue somebody actually checks
LoggedSo a missing outcome can be traced

The failed job queue is the part most commonly built and never monitored. Work fails silently and nobody knows.

Workers need supervision

A queue worker that stops leaves work piling up with no error anywhere. It needs a process supervisor to restart it and monitoring to alert when the queue is growing.

Alerting on queue depth is the single most useful monitor in a queued system.

Frequently asked questions

What queue backend should we use?

Redis or a database queue for most business applications. A database queue is adequate at modest volume and simpler to run.

How do we know if jobs are failing?

Alert on queue depth and on failed job count. Both should reach a person, not just a log.

Can scheduled tasks replace queues?

For periodic work, yes. For work triggered by user actions, no — the delay is unpredictable.

What about long-running jobs?

Break them into smaller jobs where possible. A job that runs for an hour is difficult to retry and difficult to monitor.

Keep reading

Forms that take five seconds to submit?

It is usually email being sent during the request. Moving it to a queue is a small change with an immediate effect.

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

Related services

What we build for problems like this one

Web DevelopmentCustom Software Development