Pattern Catalog
12 patterns for every workflow.
A quick-reference catalog of battle-tested Ranvier patterns. Each card shows a minimal code example and links to the full example in the repository.
1. Basic Pipeline
BeginnerChain transitions linearly to build a processing pipeline.
let axon = Axon::<String, String, String>::new("pipeline")
.then(ValidateInput)
.then(ProcessData)
.then(FormatOutput);
let report = axon.execute(input, &(), &mut bus).await;
// Local demo: inspect domain and durability together before retry.
match report {
ExecutionReport::Unpersisted { result: Outcome::Next(output), .. } => println!("{output}"),
_ => eprintln!("Execution requires review before retry"),
}Examples: hello-world, typed-state-tree
2. Resource Injection
BeginnerInject shared dependencies (DB pools, config) via Resources.
impl Transition<UserId, User> for FetchUser {
type Error = String;
type Resources = DbPool;
async fn run(&self, id: UserId, pool: &DbPool, _bus: &mut Bus)
-> Outcome<User, String> {
Outcome::Next(pool.query_user(&id).await)
}
}Examples: db-example, multitenancy-demo
3. Bus Capability
BeginnerPass request-scoped context through the pipeline via the type-indexed Bus.
bus.insert(RequestId("req-abc".into()));
bus.insert(TenantId::new("tenant-1"));
// Inside transition
let tenant = bus.read::<TenantId>();
let req_id = bus.read::<RequestId>();Examples: bus-capability-demo, multitenancy-demo
4. Custom Errors
IntermediateUse thiserror + serde for structured, matchable domain errors.
#[derive(Error, Serialize, Deserialize)]
enum OrderError {
#[error("item not found: {0}")]
ItemNotFound(String),
#[error("payment declined: {0}")]
PaymentDeclined(String),
#[error("branch payload could not be encoded")]
EncodingFailed,
}Examples: custom-error-types, order-processing-demo
5. Branching
IntermediateUse Outcome::Branch for conditional flow divergence.
if order.total > 10_000 {
return match serde_json::to_value(&order) {
Ok(payload) => Outcome::Branch("high_value".into(), Some(payload)),
Err(_) => Outcome::Fault(OrderError::EncodingFailed),
};
}
Outcome::Next(order)Example: order-processing-demo
6. Retry + DLQ
IntermediateAutomatic retry with exponential backoff and dead-letter queue fallback.
axon.with_dlq_policy(DlqPolicy::RetryThenDlq {
max_attempts: 3,
backoff_ms: 100,
})
.with_dlq_sink(InMemoryDlqSink::new());Example: retry-dlq-demo
7. Persistence
AdvancedPersist workflow state for fault recovery and checkpoint/resume.
let mut bus = Bus::new();
bus.insert(handle.clone());
bus.insert(PersistenceTraceId::new("trace-001"));
bus.insert(PersistenceAutoComplete(false));
let report = axon.execute(input, &(), &mut bus).await;
// AutoComplete(false) records events without certifying terminal completion.
let events_recorded = matches!(&report,
ExecutionReport::Recorded { completion: CompletionConfirmation::NotRequested, .. }
) && report.cleanup().is_none_or(|cleanup| cleanup.is_complete());
if !events_recorded {
eprintln!("Execution requires reconciliation before retry");
}
// The caller still owns terminal completion; this is not durable workflow success.Example: state-persistence-demo
8. Compensation
AdvancedRegister compensation hooks for automatic rollback on faults (saga pattern).
impl CompensationHook for RefundPayment {
async fn compensate(&self, ctx: CompensationContext)
-> anyhow::Result<()> {
println!("Refunding trace={}", ctx.trace_id);
Ok(())
}
}Example: state-persistence-demo (phase 3)
9. Tenant Isolation
IntermediatePer-tenant data boundaries via TenantId from the multitenancy extension.
bus.insert(TenantId::new("tenant-a"));
// Inside transition
let tenant = bus.read::<TenantId>()
.ok_or_else(|| "missing tenant".to_string())?;
let data = store.list(tenant.as_str()).await;Example: multitenancy-demo
10. Unit Testing
BeginnerTransitions are async functions — test them directly or test the full pipeline.
#[tokio::test]
async fn test_pipeline() {
let axon = Axon::<String, String, String>::new("greeting").then(BuildGreeting);
let mut bus = Bus::new();
let report = axon.execute("World".into(), &(), &mut bus).await;
assert!(matches!(report,
ExecutionReport::Unpersisted { result: Outcome::Next(ref greeting), .. }
if greeting == "Hello, World!"
));
}Example: testing-patterns (7 tests)
11. Circuit Breaker
AdvancedApplication-level circuit breaker using shared atomic state.
let failures = self.failure_count.load(Ordering::SeqCst);
if failures >= self.threshold {
return Outcome::Fault("circuit open".to_string());
}Example: retry-dlq-demo (demo 5)
12. Timeout
IntermediateWrap slow operations with tokio::time::timeout.
match timeout(Duration::from_millis(500), slow_op(&input)).await {
Ok(Ok(result)) => Outcome::Next(result),
Ok(Err(e)) => Outcome::Fault(e.to_string()),
Err(_) => Outcome::Fault("timed out".to_string()),
}Example: retry-dlq-demo (demo 3-4)