Idempotency and delivery semantics
Quick answer
An idempotent operation can be repeated without adding another intended state change. Delivery guarantees describe message transport, not automatically the uniqueness of business effects.
Duplicates and request identity
At-most-once delivery can lose messages; at-least-once delivery can repeat them. A client-scoped idempotency key and payload identity help recognize repeated requests and reject conflicting reuse. Persist the outcome and state change atomically where possible; an in-memory map cannot survive a restart and does not guarantee exactly-once external side effects. External systems may need their own idempotency support.
use std::collections::HashMap;fn submit<'a>(seen: &mut HashMap<&'a str, &'a str>, key: &'a str, body: &'a str) -> &'static str { match seen.get(key) { Some(old) if *old == body => "repeat", Some(_) => "conflict", None => { seen.insert(key, body); "accepted" } }}fn main() { let mut seen = HashMap::new(); println!("{}", submit(&mut seen, "client1:a", "task")); println!("{}", submit(&mut seen, "client1:a", "task")); println!("{}", submit(&mut seen, "client1:a", "other"));}Expected stdout (not executed):
acceptedrepeatconflictThe example models deduplication only, not a durable outcome store.
use std::collections::HashSet;fn main() { let mut seen = HashSet::new(); let count = [1, 2, 1, 3].into_iter().filter(|id| seen.insert(*id)).count(); println!("{count}");}Expected stdout (not executed):
3Four deliveries represent three distinct IDs in this bounded fixture.
Common mistake
Do not discard the idempotency record before the retry horizon without a defined expiration policy.
Try it yourself
Design a database unique constraint scoped to tenant and key, and describe the response after a lost acknowledgement.
Static examples for Rust 1.85.0, edition 2024. Run them locally in a disposable project; ByteMind execution support and these outputs have not been verified.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Idempotency and delivery semantics — practice quiz
Choose one answer per question. Code examples are predictions; review explanations after attempting them.
A request is repeated after the service restarts. Which design allows durable duplicate recognition?
Idempotency and delivery semantics — example 1. Read the code; do not execute it for this question. Assume the lesson’s stated setup, compatible dependencies and toolchain, and a fresh disposable directory for file-creating examples. ```rust use std::collections::HashMap; fn submit<'a>(seen: &mut HashMap<&'a str, &'a str>, key: &'a str, body: &'a str) -> &'static str { match seen.get(key) { Some(old) if *old == body => "repeat", Some(_) => "conflict", None => { seen.insert(key, body); "accepted" } } } fn main() { let mut seen = HashMap::new(); println!("{}", submit(&mut seen, "client1:a", "task")); println!("{}", submit(&mut seen, "client1:a", "task")); println!("{}", submit(&mut seen, "client1:a", "other")); } ``` What standard output should the program produce if its run succeeds? Ignore Cargo and diagnostic messages.
Idempotency and delivery semantics — example 2. Read the code; do not execute it for this question. Assume the lesson’s stated setup, compatible dependencies and toolchain, and a fresh disposable directory for file-creating examples. ```rust use std::collections::HashSet; fn main() { let mut seen = HashSet::new(); let count = [1, 2, 1, 3].into_iter().filter(|id| seen.insert(*id)).count(); println!("{count}"); } ``` What standard output should the program produce if its run succeeds? Ignore Cargo and diagnostic messages.