Asynchronous processing
Taking work off the request path: queues, workers, schedules, and the retry and idempotency rules that come with them.
The cooks. They take tickets off the rail and do the slow jobs.
That is asynchronous processing. The cashier takes the order and answers the customer at once. The cooks work through the tickets in the back, and the customer is told when the food is ready.
How it works
In a synchronous flow, the user waits for the whole chain to finish.
A → B → C → D → Response
In an asynchronous flow, the request records the work and returns. Workers do it later.
A → Queue → Response (immediate)
Queue → Worker → B → C → D
Good candidates:
- emails and notifications
- image and video processing
- AI jobs
- report generation
- any background task the caller does not need to wait for
A queue also absorbs bursts. If traffic spikes, the queue grows and workers drain it at their own pace instead of the whole system slowing down. See message queues for the queue itself.
The parts
- Queue. Holds the jobs durably.
- Workers. Take jobs and run them. They can be scaled separately from the web servers.
- Scheduler. Starts regular chores, such as nightly tidying, at the right time.
- Outbound messages. Email and notifications are usually sent by a worker, not inside the request.
What you must also design
- Retries. Jobs fail. Decide how many attempts, what backoff and what happens at the end.
- Idempotency. A retry can run the same job twice, so the job must be safe to repeat. See idempotency.
- Telling the user. Polling, a push message or an email, so people know the work finished.
- A worker that is running. The queue only helps if something consumes it. A stopped worker means a silent backlog.
Not inside the business transaction
Sending a message inside the same database transaction as a business change is a trap. If the send succeeds and the transaction rolls back, you have announced something that never happened. The transactional outbox solves this: write the intent in the same transaction, and let a worker send it afterwards.
Watching the queue
Queue depth alone can mislead. A large queue that drains quickly is healthy. A small queue with one very old job is not. The age of the oldest pending job is the number that shows whether work is stuck, and it is the one worth putting on a screen and alerting on. Alongside it, track failures and retries, and give failed jobs a place where a person can inspect and replay them. A dead-letter list that nobody reads is only a slower way of losing work.
Where I used it
In IMS, I chose pg-boss so jobs are durable and tied to the business transaction, without Redis. The database carries the queue load, and a worker and its schedules have to be running. IMS is in progress and I make no production claim.
In my POS SaaS design, the sale and its intent to send a fiscal invoice commit in one transaction. A separate worker drains the outbox with backoff and writes the result back later. The receipt prints at once and says fiscal confirmation is pending. POS is early-stage, so this is design and not a production result. The design also says the age of the oldest pending invoice matters more than queue depth.
Trade-offs
- More moving parts. A queue, workers, monitoring and retry logic replace one function call.
- Two states instead of one. A job is pending before it is done, and the interface has to show that honestly.
- Failures move out of sight. A failed job does not produce an error for the user. You need alerts on failed and stuck jobs.
- Ordering is not guaranteed. Design jobs so that running them out of order is safe.
- Not for decisions the caller needs now. Validating a coupon before showing a total must stay synchronous.
A useful rule: go asynchronous when the work is slow and the answer is not needed immediately, and design the retry and idempotency rules in the same change.