Designs safe bounded retries and idempotency for HTTP calls and external side effects, especially payments and POST requests. Use when retrying a request, handling an ambiguous timeout, choosing Polly or Microsoft.Extensions.Http.Resilience policies, or preventing duplicate charges; for broker redelivery and outbox/inbox use messaging-patterns.
Installs into .claude/skills of the current project.
Sponsors
SSkills DirectorySkills Directory
Know which skills are safe — weekly.
SSkills DirectorySkills Directory
Is that skill safe to install?
Are you the author of Resilience Idempotency?
Add the live security badge to your README. It updates with every re-scan.
[](https://www.skillsdirectory.com/skills/k4ndev-resilience-idempotency)
---
name: resilience-idempotency
description: Designs safe bounded retries and idempotency for HTTP calls and external side effects, especially payments and POST requests. Use when retrying a request, handling an ambiguous timeout, choosing Polly or Microsoft.Extensions.Http.Resilience policies, or preventing duplicate charges; for broker redelivery and outbox/inbox use messaging-patterns.
---
# Resilience and Idempotency
## Purpose
Prevent retries and ambiguous network outcomes from repeating business effects, while recovering from transient failures within a bounded time and attempt budget.
## Scope
HTTP clients, payment/provider calls, non-broker external side effects, idempotency-key contracts, transient failure classification, and Polly or Microsoft.Extensions.Http.Resilience retry pipelines.
## When to use
- A caller retries an HTTP request, payment, or other externally visible operation.
- A timeout or connection loss leaves it unclear whether the remote side committed the operation.
- The user asks about an idempotency key, Polly `RetryAsync`, `ResiliencePipeline`, or `Microsoft.Extensions.Http.Resilience`.
## Do not use for
- Broker redelivery, consumer deduplication, transactional outbox/inbox, or dead-letter policy: use `messaging-patterns`.
- HTTP wire compatibility as the only question: use `api-contract-review`.
- Runtime-only diagnosis without a code/design change: use `dotnet-diagnostics`.
## Required context
Identify the HTTP method and business effect, downstream idempotency support and key scope/retention, request replayability, payload identity, transient/permanent failure semantics, all retry owners, rate limits and `Retry-After` behavior, attempt and total deadlines, and existing tests.
## Workflow
1. Trace the operation from the business intent through each client, handler, SDK, gateway, and downstream side effect. Record what can commit before a timeout or cancellation is observed.
2. Classify replay safety from the actual contract, not just the verb. Treat `POST` as non-idempotent by default; verify application semantics even for `PUT` and `DELETE`.
3. If the receiver supports idempotency, create or load one opaque key per logical business operation and durably associate it with that operation before the first attempt. Reuse that same key and the same logical payload for every attempt and after process restart.
4. Choose one retry owner. Use Polly v8 `ResiliencePipeline` or `Microsoft.Extensions.Http.Resilience` APIs that match the installed package version; make the request replayable, and set the same idempotency key on each attempt. Handle only confirmed transient outcomes, use bounded attempts with jittered backoff, honor `Retry-After`, and set both an overall deadline and an attempt timeout.
5. Make the receiver's deduplication atomic: uniquely scope the key, compare a stable request/payload identity, and retain the result/state long enough to cover the caller's retry and recovery window. Return the prior result for a completed duplicate, a conflict/status response for an in-flight duplicate, and `409` or `422` for the same key with a different payload.
6. On an ambiguous outcome without a receiver-side idempotency guarantee, reconcile by a stable business/provider reference or query operation status; do not blindly resend the mutation. Do not interpret cancellation or timeout as proof of rollback.
7. Test duplicate attempts, process restart, concurrent key claims, mismatched payloads, `Retry-After`, permanent failures, and the timeout-after-commit window. Verify attempts and total elapsed time stay within the intended budget.
## Decision rules
- `GET`/`HEAD` are usually safe to replay when the endpoint has no hidden side effects. `PUT`/`DELETE` are idempotent only when the implementation and contract preserve that property. `POST` is not safe to retry merely because a policy calls the failure transient.
- A retry is safe for a mutation only when the receiver contract makes repeated use of the same key deduplicate the business effect. Generate the key once outside the retry delegate; never make a new key per attempt.
- If a payment/provider API does not support a suitable key, use a durable operation record and reconciliation/status query. A local key alone cannot prevent a remote duplicate charge.
- Persist receiver-side key reservation and local business state atomically where they share a database. A database transaction cannot make an unrelated provider HTTP call atomic; use the provider's idempotency contract and a durable state machine/reconciliation instead.
- Return the recorded response for a completed same-key/same-payload request. For an in-flight duplicate, define a stable status endpoint or return a documented conflict with `Retry-After`; reject a same-key/different-payload request with `409` or `422`.
- Retry only transient outcomes that are safe for this operation, such as selected connection failures, `408`, `429`, and selected `5xx` responses. Do not retry validation errors, authorization failures, business declines, or caller cancellation. Respect provider-specific behavior rather than treating every `5xx` as proof that no effect occurred.
- Set a finite retry count, jittered backoff, per-attempt timeout, and total operation deadline. Honor `Retry-After` without retrying earlier than requested; if it exceeds the remaining deadline, return control for later reconciliation rather than violating the budget.
- Avoid nested retries in the HTTP handler, SDK, and persistence layer. Choose one retry owner and calculate the maximum aggregate attempts; EF Core `EnableRetryOnFailure` is for its database operation and does not make an external payment replay-safe.
- Use `IHttpClientFactory`/typed clients rather than constructing `HttpClient` per call (`PERF-01`). Give non-obvious retry/timeout values names or options (`STY-10`). For public rate-limited endpoints, apply the existing `SEC-16` guidance, including `429` with `Retry-After`. When reviewing against the supplied rulebook, use only those existing IDs when applicable; do not invent an idempotency/retry rule.
- An idempotency key is not an authorization credential. Keep it opaque and free of personal/payment data, scope and protect stored records, and avoid logging sensitive request content.
- Do not claim exactly-once effects from retries or a key alone. State the receiver's deduplication window and the remaining recovery/retention assumptions.
## Expected output format
State the operation's replay-safety classification, commit/timeout windows, key lifecycle and receiver contract, retry owner and transient predicate, attempt/backoff/deadline limits, reconciliation behavior, and failure-injection tests. Separate verified provider behavior from assumptions.
## Common mistakes
- Wrapping a payment `POST` in `RetryAsync(3)` without a receiver-supported idempotency key.
- Generating a fresh key for every retry, storing it only in memory, or assuming a timeout means the server did not commit.
- Retrying every exception/status, retrying permanent failures, or multiplying attempts through nested policies.
- Reusing a consumed `HttpRequestMessage`/stream body, ignoring `Retry-After`, or setting only a per-attempt timeout with no total deadline.
- Assuming an idempotency record and an unrelated provider charge are atomic because the caller uses a database transaction.
## Edge cases
- A server may commit and lose the response; a client retry must use the same key and the receiver must return the original result.
- A duplicate may arrive while the first request is still in progress; define a bounded wait/status response without running the side effect twice.
- Key expiry, provider-specific key scope, and regional failover can reopen a duplicate window; confirm these details and size retention accordingly.
- Streaming request bodies may not be replayable; buffer only when safe and bounded, or do not retry that request.
- A `429` or transient response can carry a server-directed delay that exceeds the caller's remaining deadline.
## Examples
See [retry and idempotency examples](examples.md) for an unsafe payment retry, a Polly v8 pipeline using one persisted key, and the receiver deduplication contract.
## Test scenarios
- Positive: “A payment `POST` times out after the provider may have charged it; use one idempotency key and reconcile safely.”
- Negative: “A RabbitMQ consumer needs duplicate-delivery handling and a transactional outbox” routes to `messaging-patterns`.
- Boundary: “The HTTP response schema changes but retry semantics do not” routes to `api-contract-review`.
- Failure injection: timeout after provider commit, same-key concurrent attempts, same key with a changed payload, transient `503`, `429` with `Retry-After`, and a permanent decline.
## Related skills
`messaging-patterns`, `api-contract-review`, `testing-quality`, `security-audit`, `performance-review`.