sayiir-persistence
Persistence layer for distributed workflow execution with checkpoint/restore capabilities.
Overview
This crate provides traits and implementations for persisting workflow execution state, enabling:
- Distributed execution: Multiple worker nodes can execute tasks from the same workflow
- Fault tolerance: Workflows can be resumed after crashes
- Task claiming: Atomic task claiming prevents duplicate execution
- Flexible backends: Implement custom storage (Redis, PostgreSQL, etc.)
Quick Start
use ;
use WorkflowSnapshot;
async
Implementing Custom Backends
To create a custom persistence backend (e.g., for Redis, PostgreSQL, or any other storage):
1. Add Dependencies
[]
= { . }
= { . }
= { . }
2. Implement PersistentBackend
use ;
use WorkflowSnapshot;
use ;
use async_trait;
use Duration;
Key Concepts
Snapshots
Snapshots capture the complete execution state of a workflow:
- Which tasks have completed
- Task outputs
- Current execution position
- Initial workflow input
Task Claims
Task claims enable distributed execution:
- Atomic claiming: Only one worker can claim a task
- Expiration: Claims can have TTLs to prevent stuck tasks
- Extension: Long-running tasks can extend their claims
- Release: Workers release claims when done
Backend Requirements
A production backend should provide:
- Durability: Snapshots survive process crashes
- Atomicity: Task claims must be atomic (use database transactions, Redis SETNX, etc.)
- Consistency: Multiple workers see consistent state
- Performance: Efficient querying for available tasks
Built-in Backends
InMemoryBackend
Provided for testing and development:
- No persistence across restarts
- Good for unit tests and prototyping
Architecture
┌─────────────────────┐
│ Workflow Runtime │
└──────────┬──────────┘
│
▼
┌─────────────────────┐
│ PersistentBackend │ ◄─── Trait you implement
│ (trait) │
└──────────┬──────────┘
│
┌─────┴─────┬───────────┬──────────────┐
▼ ▼ ▼ ▼
┌─────────┐ ┌─────────┐ ┌─────────┐ ┌─────────┐
│ Memory │ │ Redis │ │Postgres │ │ Your │
│ Backend │ │ Backend │ │ Backend │ │ Backend │
└─────────┘ └─────────┘ └─────────┘ └─────────┘