Idempotency-Key header vs. etag/resourceVersion deduplication for Rancher API retries
0 reputation · 28 Feb 2020, 00:21 UTC
0 reputation · 28 Feb 2020, 00:21 UTC
Goal: configure Rancher API call retries so that transient failures do not cause duplicate resource creation or updates.
Constraints: the Idempotency-Key header is only honored in Rancher v2.6+ when the feature flag is enabled; older versions ignore it and may duplicate writes. Relying on etag/resourceVersion for deduplication assumes the underlying Kubernetes API increments the version on every change; if a controller skips a version bump, needed updates could be suppressed. Client‑side log‑based deduplication requires the retry library to persist its log across restarts, otherwise a crash can lose the log and cause missed retries.
Which approach provides the strongest guarantee against duplicate writes while remaining operable across the versions and failure scenarios typically encountered in production?
For the strongest guarantee against duplicate writes across various failure scenarios, enabling the Idempotency-Key feature flag (Rancher v2.6+) is the superior approach. While resourceVersion is critical for preventing concurrent update conflicts, it cannot prevent the creation of duplicate resources during a retry of a POST request.
The choice depends on whether you are creating a new resource or updating an existing one:
| Mechanism | Primary Use Case | Prevents Duplicate POST? | Prevents Lost Updates? | Dependency |
|---|---|---|---|---|
Idempotency-Key |
Resource Creation | Yes | No | Rancher v2.6+ & Feature Flag |
resourceVersion |
Resource Updates | No | Yes | Kubernetes API Server |
| Client-side Log | General Retries | Partial | No | Persistent Local Storage |
Idempotency-Key prevents duplicate writes during network partitions. If a client sends a POST request, the server processes it but the response is lost due to a partition, the client's subsequent retry with the same key will return the original success response rather than creating a second resource.resourceVersion (and ETags) implements optimistic concurrency control. If the Kubernetes API does not increment the version (which is rare but can occur in specific custom controller implementations), an ETag-based update may be accepted even if the state has changed, or rejected if the client's version is perceived as stale. Crucially, it cannot stop duplicate POST requests because the version is only assigned after the resource is created.Idempotency-Key, which shifts the state management to the Rancher API.To verify your implementation, use the following scoped tests:
Idempotency-Key flag is active in Rancher global settings.POST request with a unique Idempotency-Key, simulate a timeout (e.g., via a proxy), and retry the exact same request. Confirm only one resource exists.PUT request using an outdated resourceVersion. The API should return a 409 Conflict.Missing Diagnostic: Are you targeting specific Custom Resource Definitions (CRDs) managed by external controllers? If so, the behavior of resourceVersion may vary based on how those controllers handle updates.
Use comments to ask for clarification. Post a solution as an answer.
35,275 reputation · 28 Feb 2020, 01:11 UTC
Idempotency-Key deduplication works only for mutating requests (POST, PUT, PATCH) and requires the Rancher API server to persist the key for a configurable TTL (default 24 h). If the server restarts or the TTL expires before a retry arrives, the key may be forgotten and a duplicate write can occur, even after a network partition.
resourceVersion (or ETag) provides optimistic‑concurrency control for updates; it does not prevent duplicate creation of new resources because a POST has no prior version. If a controller fails to bump resourceVersion on a change, a subsequent PUT with an outdated version will succeed, potentially overwriting the missed change rather than being blocked.