Background Jobs
How to run reliable HTTP background jobs
Build a background receipt workflow with durable job submission, safe retries, and a checklist for diagnosing delivery failures.
By SendMQ 6 min read
What is an HTTP background job?
An HTTP background job queues a request that runs separately from the request that created it. For example, after checkout saves an order, SendMQ can call your receipt endpoint while the customer sees their order confirmation. The handler loads the saved order and sends the receipt.
The useful boundary is the moment the order becomes durable. Checkout should finish once the order is committed and there is a durable record that a receipt must be sent. The receipt endpoint can then load the order separately. This keeps a slow email provider from holding up checkout and gives your team a job to inspect when a customer says a receipt is missing.
Use the same pattern for work such as generating a report, refreshing a search index, or notifying a fulfillment service. Start with a single task that already has a clear business identifier. For receipts, the order ID can identify the work. If your application allows a deliberate resend, give the resend its own operation ID so it can be tracked separately.
How do you avoid losing a job after checkout?
Write the order and a pending receipt record in the same database transaction. This is the transactional outbox pattern. A small background process scans pending records, submits jobs to SendMQ, and records the returned job ID. If checkout rolls back, neither record is saved. If a process crashes after checkout commits, the pending record is still available for another submission attempt.
There is still a gap between SendMQ accepting a job and your application saving the job ID. The submitter could time out or exit during that gap. Keep the original idempotency key and request fields alongside the pending record so another process can resend the same request. This also helps if two submitters try to claim the same record.
Track the age of the oldest pending record and the number of consecutive submission failures. A growing outbox points to a problem before SendMQ accepts jobs, such as an expired credential or an application worker that has stopped running. Delivery history is the place to look after SendMQ has returned a job ID.
How do you enqueue a job safely?
Send a POST to /v1/jobs with a scoped workspace API key and an Idempotency-Key tied to the business operation. If the request times out, resend the same payload with the same key. An identical request returns the existing job with Idempotency-Replayed: true. A changed request with the same key returns 409.
Persist the job ID from the 202 response next to the order or outbox record. The response means SendMQ accepted the job for processing. To learn what happened afterward, inspect the job and its attempts. Keeping the order ID and job ID together gives a support engineer a clear path from a customer report to the corresponding delivery.
In production, use a publicly reachable HTTPS endpoint on port 443. Pass the order ID and load the fields your handler needs from your own database. A small payload limits how much customer data crosses services and lets the handler read the committed state.
curl https://sendmq.com/v1/jobs \
-H "Authorization: Bearer $SENDMQ_API_KEY" \
-H "Content-Type: application/json" \
-H "Idempotency-Key: receipt-order-123" \
-d '{
"url": "https://api.example.com/tasks/send-receipt",
"method": "POST",
"body": {"order_id": "123"}
}'Which failures are retried?
SendMQ treats 2xx as success and retries timeouts, connection errors, HTTP 408, 429, and 5xx within the configured attempt limit. Open the job’s attempt history to see whether the destination returned an error or failed to respond.
Return 429 when the endpoint is temporarily overloaded or 5xx when a dependency is unavailable. SendMQ uses exponential backoff with jitter and can honor Retry-After within the configured maximum backoff. A malformed request can return 400 so the team can correct the request. A redirect is a delivery failure, so configure the final HTTPS URL directly.
Choose your retry policy around how long the work stays useful and how your destination recovers. Several rapid attempts during a database restart can use up the available attempts before it recovers. Longer backoff can help for short dependency outages. For time-sensitive business work, have your handler check the current state and expiry before performing a delayed action.
How do you prevent the receipt from being sent twice?
Treat delivery as at least once. Your handler might send the receipt and then lose the connection before SendMQ sees the successful response. SendMQ may deliver the request again. The X-SendMQ-Delivery-Id header stays stable across attempts of the same job, which lets your handler recognize a delivery it already accepted.
For work entirely inside your database, record the delivery ID under a unique constraint and commit it in the same transaction as the business update. If the update fails, the delivery record should roll back with it. If two copies arrive at once, the unique constraint arbitrates the claim. Once the update is durably committed, a duplicate can receive 2xx without performing it again.
Sending the receipt through an external email provider crosses a second service boundary. A database transaction in your application cannot roll back a send the provider has already accepted. When the provider supports idempotency, pass a business key such as receipt-order-123, then store the provider result. If a provider request times out, look up the operation if possible or retry using the same key. Without an idempotency or lookup mechanism, an ambiguous outcome needs a reconciliation policy before another send.
Use the provider’s accepted message ID and your application’s receipt state to distinguish acceptance by the provider from delivery to the recipient’s mailbox. That distinction matters when support investigates a job whose HTTP attempt succeeded but whose email was later bounced.
key = receipt_key(order_id)
if receipt_store.has_provider_result(key):
return 204
result = email_provider.send(receipt, idempotency_key=key)
receipt_store.record_provider_result(key, result.id)
return 204What should you inspect when delivery fails?
Start with the job ID you saved at submission. Use the Jobs tab or GET /v1/jobs/{id}/attempts with a key that has jobs:read. Read the attempts in order, looking at the response status, error, and latency. A series of fast 429 responses suggests throttling. A 400 immediately after an endpoint deployment suggests a request or validation mismatch. A timeout calls for checking the destination logs to find out whether the handler finished.
Correlate the SendMQ job ID or delivery ID with your application logs. Log identifiers and the processing stage, such as receipt loaded or provider accepted, so someone can reconstruct the sequence without opening the request body. Use the business record or provider’s record to check for an already completed side effect before manually retrying a failed job.
Fix the cause, then retry a single failed or canceled job and inspect the new attempt. If you have a backlog after an outage, start with a small batch to confirm that the destination can absorb the work. Close the incident when the expected business result is visible in your own application, such as a receipt record containing the provider’s message ID.
What should you test before going live?
In a test workspace, place a sample order and follow it from the outbox row to SendMQ and then to the receipt record. Make a test endpoint return 503 once before succeeding, and confirm the two attempts share a delivery ID. Simulate a timeout after the provider accepts the send and confirm that a retry resolves to the same operation.
Pause the submitter, create a sample order, and restart it. Confirm that the pending record is picked up after restart. Submit the same request twice with the same key and confirm that you get back the same job ID. Finally, test a permanent validation error and check that the logs and attempt history tell an engineer what to fix.