Prompt
How do I build an app-to-app workflow that retries on failure
Latest observation
To build an app-to-app workflow that retries on failure, you generally want three pieces:
- A trigger/entry app
- A workflow/orchestration layer
- A retry strategy with idempotency + error handling
Here’s a practical pattern that works well.
1) Define the workflow between apps
Example:
- App A sends a request or event
- Workflow service receives it
- App B performs the action
- Workflow records success/failure and retries if needed
This can be implemented with:
- REST API calls
- Webhooks
- Message queues like SQS, Pub/Sub, RabbitMQ, Kafka
- Workflow engines like Temporal, AWS Step Functions, Azure Logic Apps, Camunda, n8n, Zapier, etc.
If reliability matters, prefer queue + worker or a workflow engine over a simple synchronous HTTP chain.
2) Make the operation idempotent
Retries can cause duplicates unless the target app can safely accept the same request more than once.
Use:
- Idempotency keys on requests
- A unique operation ID
- Deduplication in App B
Example:
- App A sends
operation_id=12345 - App B stores processed IDs
- If retry sends the same
operation_id, App B returns the previous result instead of repeating the action
This is critical.
3) Add retry policy
Use a retry strategy with:
- Max attempts: e.g. 3–5
- Exponential backoff: wait longer after each failure
- Jitter: randomize delay to avoid retry storms
- Retry only on transient failures:
- timeouts
- 429 rate limits
- 5xx errors
- network errors
Do not retry immediately for:
- validation errors
- authentication failures
- bad input
- permanent 4xx errors
Example backoff:
- 1st retry: after 1s
- 2nd: after 2s
- 3rd: after 4s
- add random jitter like ±20%
4) Store workflow state
Track each step:
pendingin_progressfailedsucceededdead_lettered
Store:
- request payload
- attempt count
- last error
- timestamps
- correlation/operation ID
This lets you:
- resume after crashes
- inspect failures
- audit what happened
5) Use a dead-letter queue or failure path
After retries are exhausted:
- move the job to a dead-letter queue
- mark it for manual review
- alert operators
- optionally send a notification to the user
This prevents infinite retry loops.
6) Recommended architecture patterns
Simple but effective
App A → Queue → Worker → App B
- Worker retries failed jobs
- Queue stores messages durably
- Dead-letter queue catches exhausted retries
More advanced
App A → Workflow Engine → App B
- Orchestrates multiple steps
- Supports retries, compensation, timeouts, state persistence
7) Example pseudo-flow
- App A creates job
job-789 - Job is written to queue with
operation_id=job-789 - Worker picks it up and calls App B
- App B times out or returns 500
- Worker retries with backoff
- If success, mark complete
- If all retries fail, send to dead-letter queue and alert
8) Example HTTP retry logic
If you’re calling App B directly, your client should:
- set a timeout
- catch transient errors
- retry with backoff
- include idempotency key
Pseudo-code:
for attempt in range(5):
try:
response = call_app_b(
payload=data,
headers={"Idempotency-Key": operation_id},
timeout=5
)
if response.status_code in (200, 201):
break
if response.status_code not in (429, 500, 502, 503, 504):
raise PermanentError(response.text)
except TransientError:
pass
sleep(backoff_with_jitter(attempt))
else:
send_to_dead_letter_queue(data)
9) Operational best practices
- Set timeouts on all network calls
- Log correlation IDs across apps
- Use monitoring and alerting
- Limit retries to avoid overload
- Protect against duplicate side effects
- Test failure scenarios intentionally
10) If you want the easiest reliable implementation
Use:
- Queue for transport
- Worker for execution
- Idempotency key for deduplication
- Exponential backoff
- Dead-letter queue for final failures
That gives you a robust app-to-app workflow with retries.
If you want, I can also provide:
- a reference architecture diagram
- example implementation in Python/Node.js
- or a setup using AWS / GCP / Azure / Temporal / n8n