Cookbook & Example Gallery

Real-world, battle-tested recipe patterns showing how to construct, test, and operate statecharts with xstate-statemachine-rs.

Every recipe includes:

  • The architectural statechart topology (JSON / diagrams).
  • Full, copy-pasteable Rust implementation.
  • Unit and integration tests demonstrating lifecycle behavior.
  • Failure handling and production best practices.

Recipes Index

  1. Traffic Light Controller
    Core Engine & Basics. Simple sequential state machine with entry/exit actions, typed enum events, and transition assertions.

  2. Hierarchical Order Fulfillment
    Compound States & Context. Multi-step e-commerce workflow with nested payment states, inventory reservation, and root-level cancellation handling.

  3. Resilient Circuit Breaker & Exponential Backoff
    Resilience Patterns. Protecting fragile downstream HTTP services using xstate-patterns with failure counters, half-open probe testing, and decorrelated jitter.

  4. Distributed E-Commerce Saga
    Distributed Transactions. Orchestrating multi-service payments, warehouse reservations, and shipments with automated compensating rollback actions.

  5. Durable Persistence & Deadlines
    Crash Recovery & Storage. Storing statecharts in PostgreSQL and SQLite with optimistic concurrency control, idempotency tracking, and surviving server restarts.

  6. Event-Driven Architecture & Transactional Outbox
    CloudEvents & Brokers. Dispatching and consuming events over NATS and RabbitMQ with transactional outbox relay guarantees and dead-letter queues.

  7. Axum HTTP Microservice & Live Inspector
    Web API & Observability. Exposing statechart actors over REST and Server-Sent Events (SSE), with real-time visual inspection in Stately Studio.

  8. LLM Agent Tool Loop with Guardrail Budgets
    AI Agents & Safety. Constrained autonomous agent loop with strict per-state tool allow-lists, token budget guards, and human-in-the-loop approvals.

  9. WebSocket Reconnection & Heartbeat
    Network Resilience. Client connection manager with exponential backoff, decorrelated jitter, and ping/pong health monitoring.

  10. Stripe Webhook Payment Gateway
    E-Commerce & Payments. Idempotent Stripe webhook receiver with signature validation, invoice payment handling, and duplicate deduplication.

  11. Form Wizard & Multi-Step Onboarding
    UI & User Flows. Multi-step signup form with back/next navigation, step invariant validation, and context persistence.

  12. Feature Flag Progressive Rollout
    DevOps & Release Engineering. Canary release progression with automated health monitoring and instant automated traffic rollback.