A customer requests a weekly bookings report. The app saves that request and shows Waiting. The customer can close the page. A separate process builds the report, saves the file, and records when the customer can open it. When they return, the app checks that the report belongs to them before showing it.
Build one report this way before applying the pattern to other work such as resizing an image, preparing a receipt or importing a spreadsheet. The example uses fake records and a test-only file store.
When should work move to the background?
Keep the checks that decide whether to accept a request in the request itself. The app needs to know who is asking, whether they can read the bookings, and which week they want. The report can be built later because the customer can leave and come back to the result.
A job is a saved description of work to do. A queue holds jobs that are waiting. A worker is a process that takes a job and runs the code. The report file and the customer’s access to it belong in your app’s storage. They have to work even after the job tool removes old history.
| Part | What the report needs |
|---|---|
| Request | The customer, the requested week and one stable request ID. Accept it only after saving enough information to start or recover the work. |
| Job | A reference to the saved request. The worker checks what it can read before fetching the bookings. |
| Result | One complete file and a status the customer can check. Verify access every time they open the report. |
A request saved in one system and a job handed off to another can fail between those two actions. For a report that must survive that gap, save the request and a pending handoff record together in the same database operation. A separate process can pick up that handoff, send the job, and try again if the queue is unavailable. Keep the same request ID across attempts. AWS guidance on keeping database changes and handoffs in sync
Where does the work run?
Start with the part you can operate. A web host that can accept the report request might stop running code once it sends the response. Ask where a waiting job is stored, what keeps the worker available, and which service bills you for the computing time.
BullMQ for a worker you operate
Your app adds a job and your worker picks it up. BullMQ’s TypeScript package uses Redis by default. BullMQ v6 also documents PostgreSQL 13 or newer with the PostgreSQL client and a separate setup step to create its queue tables. Both setups need a worker that can keep running and a database you can maintain and recover. With Redis, configure persistence and keep the store from evicting queue records. Budget for the worker and storage. BullMQ PostgreSQL setup
Inngest for a workflow with saved steps
Inngest coordinates the steps while code runs on your host or in a connected worker you operate. It can remember the results of completed steps so a later retry can use them again. The file you build in one step needs storage the next step can reach. Pass a reference to that file between steps. The host’s timeout still applies to its code, and Inngest Cloud counts a run and its steps separately. Inngest runtime choices
Trigger.dev for separately deployed tasks
Deploy a TypeScript task separately from the web app. On Trigger.dev Cloud, a managed machine runs the task. Set how long it may compute and how many can run at once. An automatic retry is another attempt. A replay creates a new run and normally uses the current task version. Cloud bills for a run starting and for computing time on the chosen machine. Trigger.dev setup
Cloudflare Queues for Workers
If your app uses Workers, a Worker can put the report request on a Cloudflare queue and a consumer Worker can process it. Cloudflare documents that a message may be delivered more than once. Queue billing counts operations such as writes, reads and deletes, including reads on retry. Check the consumer Worker’s limits separately. Cloudflare delivery guarantees
Compare background job tools by where they run and what they repeat.
Why is the report still waiting?
A page timing out tells you what happened to the browser’s request. Check the app’s saved request and its job to find out what happened to the report. Record when the app accepted the request, when a worker started, when each attempt ended, and when the result became available. Use a short reference in the logs to find the report. Keep raw prompts, customer records, private file links and credentials out of those logs.
For a report with an AI-written summary, make the model step visible in the run history. The sequence below illustrates what you would look for in a test app with a fake slow service. It describes possible observations rather than measured performance.
| What you observe | What it tells you | What to check next |
|---|---|---|
| The page stops waiting. The saved request still says Waiting. | The app saved the request. It has yet to record a worker starting. | Check the queue handoff and how long the oldest job has waited. An unavailable worker and a slow model need different fixes. |
| The worker starts, then the summary call times out. | The worker got as far as the model. The call might still have produced a result or incurred a charge. | Check the recorded operation or the provider’s result lookup, where available. Reuse the same operation key when the service supports it. When the outcome is unclear and retrying could repeat a charge or send, stop for review. |
| A later attempt saves a finished file. The customer still sees Running. | The worker reached storage. The app has yet to record or show the result. | Reconcile the file with the original request. Check that the customer can open it before closing the incident. |
| The last attempt fails. | Automatic attempts are exhausted. | Show a clear failure state on the request and give the operator a way to investigate. |
Track waiting time, attempts, final failures and accessible results separately. Alert when reports keep waiting beyond the time you told customers to expect, or when a final failure needs someone to act. A weekly schedule needs its own completion check. A job that started on time can still fail before it saves a result.
When should the worker retry?
Retry failures that can reasonably clear, such as a service briefly refusing extra requests. Limit the number of attempts and wait longer between them when the dependency needs time to recover. Add some variation to those waits when many jobs can fail together. A job with missing required data needs a correction before another attempt. Check the provider’s error guidance for the operation. AWS guidance on timeouts, retries and side effects
Check which part repeats. A queue can give the whole job another attempt. A workflow can keep finished steps and repeat the failed step. Either can repeat an action that succeeded before its outcome was saved. Before a manual retry, inspect the request, the result and the code version the next attempt will use.
Where does a job go when it stops retrying?
Some services hold a failed job in a dashboard or in a queue’s failed state. Others can move it to a separate dead-letter queue, where you can inspect messages the usual worker could not finish. Check what your setup actually keeps, how long it keeps it, and what happens if you never configure a failure destination. Cloudflare dead-letter configuration
Keep an app-owned status and the reason an operator needs to investigate. A saved failure can tell you where the work stopped. It does not undo a file that was already written or a message that was already sent. After fixing the cause, verify the destination and what would repeat before restarting work.
How do two attempts leave one result?
Give each intended report one stable request ID. Use the same ID whenever you retry that report. A deliberately requested new report gets its own ID. Idempotent work produces the same intended result when the same operation is repeated. Give the app a rule it can enforce even if two workers finish together.
- Let the database enforce one result record per request ID. Scope the request to the customer who owns it. A check that reads first and writes later leaves room for two workers to pass the same check.
- Give the complete report file a stable name based on that ID. Use storage that can create the complete file only when the name is unused. If a second worker reaches the same write, it can reuse the existing file once it verifies it belongs to this request.
- If the worker saves the file and stops before updating the request, the next attempt can verify the saved file and finish the original record. Keep the file private and check the customer’s access before returning it.
Save the verified file reference and Ready together in one guarded database update. Keep track of attempts allowed to finish on that same request record. Before setting Failed, reconcile any complete file and check that no attempt can still finish. Have the database apply the change only if the request record still matches those checks. A retry or late failure must leave a Ready result in place.
PostgreSQL uniqueness · Amazon S3 write conditions
For a receipt or other outside action, the app’s unique row cannot prove whether another service did the work. Use that service’s documented operation key when it supports one, and check the outcome if a response is lost. Job-entry deduplication can have a shorter lifetime than your app’s records.
How do you test the whole job?
- Check every test connection
Confirm the app, worker, queue, database and file store are separate from production. Use a disposable account and fake bookings. Disconnect live email, payments, model credentials and automatic schedules. If any destination is unclear, stop before creating work.
- Request a report and close the page
Save one fake request and leave. Reopen the test app and check that it shows what happened. Open the finished file as its owner and verify a second ordinary test account cannot open it.
- Make a different fake report fail after saving
Cause one controlled failure after the test worker saves the file and before it marks success. Let the same request retry. Check that the retry reuses the saved file and leaves one usable report.
- Check that a late failure leaves a finished report alone
Start two attempts for the same new fake request. Let one complete, then make the other fail. Confirm the request still says Ready, points to one complete private file and opens it for its owner.
- Make another request reach final failure
Use a fake dependency that keeps failing until the attempts run out. Check that the report stops looking like work in progress. Find its reason in the test history and check what restarting the job would repeat.