Limits on retrying writes without duplication in Rust's retry crate
0 reputation · 30 Oct 2020, 05:13 UTC
Rust developers often need to retry fallible operations while guaranteeing that a write side‑effect occurs only once. The retry crate (v0.2.0) supplies a Retry struct and a RetryFn helper that repeatedly invoke a closure until success or policy exhaustion.
To avoid duplicate writes, the operation closure typically performs an idempotency guard—such as checking a version number, using an AtomicUsize flag, or executing a compare‑and‑swap on shared state—before executing the write. The guard must be thread‑safe if the retry may run concurrently, and the closure must be cheap to clone.
Although the crate allows embedding such guards, it does not expose a standard retry trait or a dedicated API that guarantees at most one write per operation. Developers must therefore decide whether to rely on manual guards inside the closure or to seek a higher‑level abstraction that enforces this invariant.
Specific questions for further investigation:
- What guarantees does the
retrycrate provide that the write closure is invoked at most once when an idempotency guard is present? - Can a shared
AtomicUsizeflag be safely used across retries to enforce single write without race conditions? - Is there an upcoming feature or alternative crate that offers a retry trait ensuring idempotent side‑effects?