Traefik retry middleware retries non-idempotent POST and PATCH requests
0 reputation · 12 Apr 2020, 22:53 UTC
0 reputation · 12 Apr 2020, 22:53 UTC
Traefik's retry middleware re-sends requests to an upstream after connection failures or matching HTTP status codes, governed by the attempts option. The middleware applies retries to every HTTP method, including POST and PATCH, and does not expose a built-in filter to restrict retries to idempotent methods such as GET, HEAD, OPTIONS, PUT, or DELETE. If an upstream receives a non-idempotent request but the response is lost, Traefik may resend it and create duplicate side effects unless the application deduplicates using idempotency keys.
This question assumes a current Traefik v2 or v3 release, because retry semantics can differ between major versions. Middleware chain order also affects which responses become eligible for retry.
Can Traefik skip retries for non-idempotent methods? Does the attempts count include the original request, and does that count differ by Traefik major version? What middleware chain order reduces duplicate non-idempotent requests while preserving retries for safe methods?
Traefik's retry middleware cannot natively skip retries for non-idempotent methods like POST or PATCH. It applies the retry logic to any request that matches the router's criteria and triggers a retry condition (such as a connection failure or a configured HTTP status code), regardless of the HTTP method used.
In Traefik v2 and v3, the attempts parameter represents the total number of requests sent to the upstream, including the initial attempt. For example, setting attempts: 3 means Traefik will send the original request and up to two subsequent retries if the initial attempt fails.
Since there is no built-in method filter, you must isolate the retry logic at the routing level to avoid duplicate side effects on your backend. The most effective strategy is to split your traffic into separate routers based on the HTTP method.
GET, HEAD, OPTIONS, PUT, DELETE). Attach the retry middleware to this router.POST and PATCH requests. Do not attach the retry middleware to this router.POST and PATCH requests, rely on idempotency keys (e.g., Idempotency-Key header) implemented in the application logic to handle cases where a request is processed by the backend but the response is lost during transmission.To minimize the overhead of retries, place the retry middleware as close to the service as possible in the chain. While this does not stop POST retries if the middleware is present, it ensures that previous middlewares (such as authentication or request transformation) are not unnecessarily re-executed for every retry attempt.
To verify your configuration, you can use the following approach:
attempts: 3 on a test route.attempts count includes the original request.POST requests to your isolated "non-idempotent" router result in only one attempt regardless of the failure.Note: Please specify if you are using a custom Traefik plugin for retries, as plugins may implement different filtering logic than the core middleware.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.