Interactive Rust + WebAssembly Fault-Injection Lab
How to retry failed API calls without charging a customer twice
When a payment gateway or AI agent tool call times out, the caller cannot know whether the request failed on the way there or on the way back. Giving up loses the order; retrying blindly charges the card twice. Below, a deterministic simulator runs an $80 checkout 10,000 times across three architectures while injecting packet drops, duplicate deliveries, 503 outages, and mid-step crashes.
Built by Steven Mays, Lead AI Engineer · Jump to the 6 production protections ↓
10,000-Checkout Benchmark & Fault Simulator
3 × 10,000 checkouts, run at build
Never retries
sends at most onceCalls each service once. Skips rejected steps and abandons the order if the worker crashes.
3,131 of 10,000 broke a check
6,869 completed cleanly
Checkout #67 · after 1 fault
Charged, never shipped
$80.00 charged · 0 shipments
Retries blindly
sends at least onceRetries failed calls until they succeed, but keeps state in memory with no idempotency keys.
2,932 of 10,000 broke a check
7,068 completed cleanly
Checkout #67 · after 1 fault
Charged twice
$160.00 charged · 1 shipment
Retries safely
exactly-once effectWrites progress to a durable log before each call, sends an idempotency key, and compensates on failure.
0 of 10,000 broke a check
9,946 completed cleanly 51 recovered via DLQ 3 refunded or cancelled
Checkout #67 · after 1 fault
Charged once, shipped once
$80.00 charged · 1 shipment
A broken check is a bad ending, such as a double charge, a paid order that never shipped, or a shipment nobody paid for.
Injected fault rates
of service calls hit a fault. Drag any slider to re-run all 30,000 simulations.
Why a single crash breaks naive architectures: Checkout #67
Every checkout runs 4 service calls: Stock → Card → Ship → Email. In Checkout #67, the orchestrator crashes right after payments charges the card $80, before receiving the HTTP response. Watch how each retry strategy handles recovery:
Never retries
sends at most once
It acknowledged the job before doing the work, so after the crash the queue never redelivers it. The customer stays charged $80 and nothing ships.
Charged, never shipped $80.00 charged · 0 shipments
Retries blindly
sends at least once
The queue redelivers the order after the crash, but progress was only in memory. It restarts from step 1 and charges the card $80 a second time.
Charged twice $160.00 charged · 1 shipment
Retries safely
exactly-once effect
Its write-ahead log shows the charge was sent but never answered. It resends with the same idempotency key (ee31), so payments deduplicates it.
Charged once, shipped once $80.00 charged · 1 shipment
Step-by-step execution trace for checkout #67
Each map holds all 10,000 checkouts for one strategy, and a red square broke a check. Click or drag across a map to trace any checkout. All 3 strategies face the exact same fault schedule at the same call sites. Click any protection chip below to inspect its sequence diagram and Rust implementation.
injected fault step that violated an invariant
Never retries
- 40 msinventory commits reserve_stock
- 80 mspayments commits charge_card
- 80 msorders crashes before it reads the charge_card reply
- 80 msthe run ends unfinished
Retries blindly
- 40 msinventory commits reserve_stock
- 80 mspayments commits charge_card
- 80 msorders crashes before it reads the charge_card reply
- 580 msorders restarts and starts over: its progress was only in memory
- 580 msorders resends reserve_stock with no key
- 620 msinventory commits reserve_stock
- 620 msorders resends charge_card with no key
- 660 mspayments commits charge_card
- 700 msshipping commits create_shipment
- 740 msemail commits send_confirmation
- 740 msthe run ends complete
Retries safely
- 40 msinventory commits reserve_stock
- 80 mspayments commits charge_card
- 80 msorders crashes before it reads the charge_card reply
- 580 msorders restarts and resumes from its progress log← progress log
- 580 msorders resends charge_card with the same key ee314642← idempotency key
- 620 mspayments drops a repeat of charge_card · key ee314642← idempotency key
- 660 msshipping commits create_shipment
- 700 msemail commits send_confirmation
- 700 msthe run ends complete
Raw event log and injected fault schedule for checkout #67
Never retries · sends at most once
Retries blindly · sends at least once
Retries safely · exactly-once effect
Ledger invariant audit across all 10,000 checkouts
After every run, the model checker inspects all four service ledgers (inventory, payments, shipping, email) against 5 financial invariants. Checkouts that violate multiple rules are counted under the first violation.
| Outcome & Invariant Rule | Never retries | Retries blindly | Retries safely |
|---|---|---|---|
| Went wrong | 3,131 | 2,932 | 0 |
| Charged twice Invariant: The card is charged at most once. | 176 | 1,270 | 0 |
| Shipped without payment Invariant: If the order shipped, the card was charged exactly once. | 455 | 0 | 0 |
| Never shipped, money or stock held Invariant: If the order didn’t ship, the net charge is $0 and the stock is released. | 909 | 0 | 0 |
| Emailed twice Invariant: At most 1 confirmation goes out. | 155 | 521 | 0 |
| Order lost Invariant: Every run ends complete, compensated, or in the dead-letter queue. | 347 | 0 | 0 |
| Stock, shipment, or email count wrong Invariant: Each ending leaves the ledgers it promises. | 1,089 | 1,141 | 0 |
| Passed every check | 6,869 | 7,068 | 10,000 |
| Charged once, shipped once Every step took effect once. | 6,869 | 7,068 | 9,946 |
| Refunded or cancelled A step failed for certain, so the checkout undid its work. | 0 | 0 | 3 |
| Waiting for a person A step’s outcome is unknown, so the checkout parks it for a person to finish. | 0 | 0 | 51 |
Automatic counterexample shrinking (shrink())
When a strategy fails a check, the Rust engine runs delta-debugging on its first failing checkout—stripping away injected faults one by one until removing any remaining fault would make the checkout pass.
Never retries
Charged twice in checkout 8
Rule: The card is charged at most once.
- charge_card delivery 1: duplicate delivery
Retries blindly
Charged twice in checkout 8
Rule: The card is charged at most once.
- charge_card delivery 1: duplicate delivery
Retries safely
No checkout fails a check.
Production Architecture
The 5 protections that turn at-least-once retries into an exactly-once effect
Real networks cannot guarantee exactly-once packet delivery: any HTTP request or RPC can drop before reaching the server, time out after committing, or arrive twice. The Retries safely architecture achieves an exactly-once side effect by combining at-least-once retries with five cooperating mechanisms—and exposes the single assumption that can still break it (key eviction).
01 Idempotent Receiver
- Fault
- Timeout after commit, duplicate delivery
- Mechanism
- Idempotency key on every call, so the receiver drops repeats
02 Write-Ahead Log
- Fault
- Crash after a step commits
- Mechanism
- Record each step in a durable log, and resume from the log
Reference: Write-Ahead Log (Joshi)03 Timeouts
- Fault
- Slow or failing service
- Mechanism
- An 800 ms deadline on every reply
Reference: Stability Patterns (Nygard)04 Backoff and retry budget
- Fault
- Retries pile up
- Mechanism
- Exponential backoff with random jitter, and at most 3 tries per step
05 Saga
- Fault
create_shipmentgets a 503 on all 3 tries- Mechanism
- Compensating transactions:
refund_charge, thenrelease_stock
06 Dead Letter Channel
- Fault
- A compensation can't be delivered, a step's outcome is unknown, or the confirmation can't be sent
- Mechanism
- A queue where a person finishes the run
Reference: Dead Letter Channel (Hohpe, Woolf)
Under the Hood
How the Rust + WebAssembly model checker works
Everything on this page is computed by a zero-dependency Rust state machine compiled to a 26 KB wasm32 binary. There are no pre-recorded traces, no server calls, and no LLM mocks.
1. Deterministic fault streams
Each checkout number (0..9,999) is a deterministic seed. When a strategy sends a message, the network hashes (seed, step_index, delivery_number) through splitmix64 to draw faults. All three strategies face identical faults at identical call sites, and simulated exponential backoff advances a virtual clock without shifting the fault draw.
2. 3,937,500 exhaustive CI proofs
Browser sliders sample 10,000 random seeds in ~7 ms, while the Rust test suite exhaustively model-checks the safe strategy across all 2 × 5⁹ = 3,906,250 two-fault forward schedules and 2 × 5⁶ = 31,250 compensation schedules—proving zero invariant violations across the bounded fault space.
3. 11 architectural mutants
To verify that no piece of the safe orchestrator is dead weight, the test suite runs 11 mutated variants (e.g., fresh key per retry, logging Done before the RPC returns, compensating after an Unknown timeout, or reversing undo order) and asserts that the model checker finds a concrete counterexample for every mutant.
The 4-service checkout Saga
The orchestrator (orders) executes an $80.00 checkout across four independent services. Because the services do not share a database or a distributed two-phase commit lock, earlier steps must be undone via compensating actions if a later step rejects the order:
- 1.
reserve_stock→inventoryundo:release_stockLocks 1 unit of inventory for the order.
- 2.
charge_card→paymentsundo:refund_chargeCaptures $80.00 on the customer’s card.
- 3.
create_shipment→shippingpivot step (not undoable)Dispatches the physical warehouse fulfillment.
- 4.
send_confirmation→emailpivot step (not undoable)Sends the receipt after fulfillment commits.
Primary literature & references
Every mechanism modeled in the Rust engine traces directly to production systems papers and engineering guides:
- Idempotent Receiver , Enterprise Integration Patterns (Hohpe, Woolf)
- Idempotent Receiver and Write-Ahead Log , Patterns of Distributed Systems (Joshi)
- Stability Patterns , Release It!, 2nd edition (Nygard)
- Addressing Cascading Failures , Site Reliability Engineering (Google)
- Handling Overload , Site Reliability Engineering (Google)
- Sagas , SIGMOD 1987 (Garcia-Molina, Salem)
- Saga , Microservices Patterns, chapter 4 (Richardson)
- Dead Letter Channel , Enterprise Integration Patterns (Hohpe, Woolf)
I'm Steven Mays, a Lead AI Engineer with 15+ years building production software and AI agent infrastructure. I built this deterministic fault-injection engine in Rust and WebAssembly to demonstrate how production orchestrators guarantee exactly-once side effects over unreliable networks.