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 once

Calls 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 once

Retries 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 effect

Writes 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.

Fault scenarios:

Injected fault rates

13% of service calls hit a fault.

Fault rates, as a share of calls

A service fails

5%

503, nothing commits

0%

key store loses a key

Retries safely trusts each service to remember its keys. A forgotten key lets a retry repeat the step.

The network fails

4%

timeout after commit

2%

duplicate delivery

0%

lost before commit

The orchestrator fails

2%

crash after commit

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. 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

  1. 40 msinventory commits reserve_stock
  2. 80 mspayments commits charge_card
  3. 80 msorders crashes before it reads the charge_card reply
  4. 80 msthe run ends unfinished

Retries blindly

  1. 40 msinventory commits reserve_stock
  2. 80 mspayments commits charge_card
  3. 80 msorders crashes before it reads the charge_card reply
  4. 580 msorders restarts and starts over: its progress was only in memory
  5. 580 msorders resends reserve_stock with no key
  6. 620 msinventory commits reserve_stock
  7. 620 msorders resends charge_card with no key
  8. 660 mspayments commits charge_card
  9. 700 msshipping commits create_shipment
  10. 740 msemail commits send_confirmation
  11. 740 msthe run ends complete

Retries safely

  1. 40 msinventory commits reserve_stock
  2. 80 mspayments commits charge_card
  3. 80 msorders crashes before it reads the charge_card reply
  4. 580 msorders restarts and resumes from its progress log← progress log
  5. 580 msorders resends charge_card with the same key ee314642← idempotency key
  6. 620 mspayments drops a repeat of charge_card · key ee314642← idempotency key
  7. 660 msshipping commits create_shipment
  8. 700 msemail commits send_confirmation
  9. 700 msthe run ends complete
Raw event log and injected fault schedule for checkout #67

Never retries · sends at most once

1 fault in this runcharge_card delivery 1: crash after commitlog0 msorders sends reserve_stock #140 msinventory commits reserve_stock40 msinventory replies ok to reserve_stock40 msorders sends charge_card #180 mspayments commits charge_card80 msorders crashes before it reads the charge_card reply80 msthe run ends unfinished

Retries blindly · sends at least once

1 fault in this runcharge_card delivery 1: crash after commitlog0 msorders sends reserve_stock #140 msinventory commits reserve_stock40 msinventory replies ok to reserve_stock40 msorders sends charge_card #180 mspayments commits charge_card80 msorders crashes before it reads the charge_card reply580 msorders restarts580 msorders sends reserve_stock #2620 msinventory commits reserve_stock620 msinventory replies ok to reserve_stock620 msorders sends charge_card #2660 mspayments commits charge_card660 mspayments replies ok to charge_card660 msorders sends create_shipment #1700 msshipping commits create_shipment700 msshipping replies ok to create_shipment700 msorders sends send_confirmation #1740 msemail commits send_confirmation740 msemail replies ok to send_confirmation740 msthe run ends complete

Retries safely · exactly-once effect

1 fault in this runcharge_card delivery 1: crash after commitlog0 msorders sends reserve_stock #1 · key 51865e2d40 msinventory commits reserve_stock40 msinventory replies ok to reserve_stock40 msorders sends charge_card #1 · key ee31464280 mspayments commits charge_card80 msorders crashes before it reads the charge_card reply580 msorders restarts580 msorders sends charge_card #2 · key ee314642620 mspayments drops a repeat of charge_card · key ee314642620 mspayments replies ok to charge_card620 msorders sends create_shipment #1 · key 0b4a834d660 msshipping commits create_shipment660 msshipping replies ok to create_shipment660 msorders sends send_confirmation #1 · key 0615888c700 msemail commits send_confirmation700 msemail replies ok to send_confirmation700 msthe run ends complete

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.

Checkouts by outcome, for each retry strategy
Outcome & Invariant RuleNever retriesRetries blindlyRetries safely
Went wrong3,1312,9320
Charged twice Invariant: The card is charged at most once.1761,2700
Shipped without payment Invariant: If the order shipped, the card was charged exactly once.45500
Never shipped, money or stock held Invariant: If the order didn’t ship, the net charge is $0 and the stock is released.90900
Emailed twice Invariant: At most 1 confirmation goes out.1555210
Order lost Invariant: Every run ends complete, compensated, or in the dead-letter queue.34700
Stock, shipment, or email count wrong Invariant: Each ending leaves the ledgers it promises.1,0891,1410
Passed every check6,8697,06810,000
Charged once, shipped once Every step took effect once.6,8697,0689,946
Refunded or cancelled A step failed for certain, so the checkout undid its work.003
Waiting for a person A step’s outcome is unknown, so the checkout parks it for a person to finish.0051

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
    References: Idempotent Receiver (Hohpe, Woolf) · Idempotent Receiver (Joshi)
  • 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
    References: Addressing Cascading Failures (Google) · Handling Overload (Google)
  • 05 Saga

    Fault
    create_shipment gets a 503 on all 3 tries
    Mechanism
    Compensating transactions: refund_charge, then release_stock
    References: Sagas (Garcia-Molina, Salem) · Saga (Richardson)
  • 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. 1. reserve_stock → inventory undo: release_stock

    Locks 1 unit of inventory for the order.

  2. 2. charge_card → payments undo: refund_charge

    Captures $80.00 on the customer’s card.

  3. 3. create_shipment → shipping pivot step (not undoable)

    Dispatches the physical warehouse fulfillment.

  4. 4. send_confirmation → email pivot 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:

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.

About me → Get in touch →