expression
REST: replay a saved result for a duplicate operation
A request key can make a particular otherwise non-idempotent operation replayable when its server contract supports it. Scope the key to the authenticated actor and operation; compare normalized business input and replay the recorded result for the same key and input. This one-process sequential model deliberately omits persistence, atomic transactions, expiry, authorization and concurrency. It is not a production idempotency store. Standalone synthetic example. Expected stdout (not executed): created 1 replay 1 conflict created count: 1 Common mistake: Checking a key and performing a side effect in separate unprotected operations.
Formal Syntax & Structure
rust syntaxrust
(actor, operation, key) → saved request and resultWorking Example
Example Snippetrust
use axum::{Router, Json, body::{Body, to_bytes}, http::{Request, StatusCode}};use serde::{Deserialize, Serialize};use tower::ServiceExt;
#[tokio::main(flavor = "current_thread")]async fn main() { use std::collections::HashMap; let mut saved = HashMap::<(&str, &str, &str), (u32, u32)>::new(); let mut next_id = 1_u32; for quantity in [2, 2, 3] { let key = ("demo-actor", "create-item", "demo-key"); match saved.get(&key) { Some(&(original, id)) if original == quantity => println!("replay {id}"), Some(_) => println!("conflict"), None => { saved.insert(key, (quantity, next_id)); println!("created {next_id}"); next_id += 1; } } } println!("created count: {}", next_id - 1);}