|
| 1 | +--- |
| 2 | +title: What is crucible/durable |
| 3 | +description: A host-side durable-execution runtime that records every nondeterministic value a state instance consumes and replays them to rebuild it byte-identically after a crash. |
| 4 | +sidebar: |
| 5 | + order: 1 |
| 6 | +--- |
| 7 | + |
| 8 | +<!-- IMAGE-SLOT: durable-overview-replay (a sky-squid smith re-pouring a recorded molten stream back into the same mold so the casting comes out identical after the forge was relit; ember/copper on steel) 16:9 --> |
| 9 | + |
| 10 | +`crucible/durable` is the host-side **durable-execution runtime** for the |
| 11 | +[`state`](/crucible/start/introduction/) kernel. `state` is a pure statechart |
| 12 | +engine: firing an event is a deterministic function of the instance's recorded |
| 13 | +inputs. durable makes a running instance survive a process crash by recording |
| 14 | +every nondeterministic value the instance consumes (clock readings, |
| 15 | +invoked-service results, actor outcomes) and persisting each step before it is |
| 16 | +acknowledged. Recovery **replays** those recorded values back through the same |
| 17 | +pure transition function, so a recovered instance reaches exactly the |
| 18 | +configuration, context, and history of a run that never crashed, without |
| 19 | +re-invoking any external source. |
| 20 | + |
| 21 | +The runtime is **additive** over the kernel. It consumes seams the kernel already |
| 22 | +reserves (`Snapshot.Journal`, the `EffectEnvelope.EffectID` correlation slot, and |
| 23 | +the injectable `Clock` / `ServiceRunner` / `ActorSystem` drivers) and requires no |
| 24 | +change to the kernel, which stays pure and stdlib-only. Heavy dependencies |
| 25 | +(database drivers, cloud SDKs) never enter this module: persistent backends |
| 26 | +implement the `Store` interface out of tree. |
| 27 | + |
| 28 | +## Guarantees |
| 29 | + |
| 30 | +- **Deterministic replay.** A recovered instance is byte-identical to one that |
| 31 | + never crashed, because recovery replays the recorded driving events and |
| 32 | + nondeterministic results rather than re-executing their sources. |
| 33 | +- **Exactly-once effects.** A domain effect is applied exactly once over the |
| 34 | + instance's lifetime (the live run plus any number of recoveries), even though |
| 35 | + the replay loop is at-least-once. Each effect carries a deterministic |
| 36 | + `EffectID` and is deduplicated through the `Store`'s dispatch set. |
| 37 | +- **Durability across restart.** Every `Fire` step is write-ahead appended to the |
| 38 | + `Store` before it is acknowledged, so a crash after a successful `Fire` never |
| 39 | + loses the step. Periodic checkpoints bound the tail that recovery replays. |
| 40 | + |
| 41 | +## The shape of it |
| 42 | + |
| 43 | +Wire a `Runner` around a machine and a `Store`. `Start` creates a fresh durable |
| 44 | +instance; `Fire` drives it; `Recover` rebuilds it from the `Store` after a crash. |
| 45 | + |
| 46 | +```go |
| 47 | +runner := durable.NewRunner(machine, durable.NewMemStore()) |
| 48 | + |
| 49 | +// Start a fresh instance: persists a baseline checkpoint. |
| 50 | +h, err := runner.Start(ctx, "order-42", OrderInput{ /* ... */ }) |
| 51 | + |
| 52 | +// Drive it. Each Fire write-ahead appends a Record before acknowledging. |
| 53 | +_, err = h.Fire(ctx, "submit") |
| 54 | + |
| 55 | +// ...process crashes, comes back up... |
| 56 | + |
| 57 | +// Recover purely from the Store: load the latest checkpoint, replay the tail. |
| 58 | +h, err = durable.Recover(ctx, machine, store, "order-42") |
| 59 | +_, err = h.Fire(ctx, "confirm") // continues recording from the live tip |
| 60 | +``` |
| 61 | + |
| 62 | +For a hot path firing many events in sequence, keep the `Handle` from `Start` or |
| 63 | +`Recover` and call `Handle.Fire` directly to avoid a `Store` round-trip per step. |
| 64 | +For a stateless handler that fires a single event per request, `Runner.Fire` |
| 65 | +loads, replays, fires, and re-records in one call. |
| 66 | + |
| 67 | +## The seams |
| 68 | + |
| 69 | +Each source of nondeterminism is isolated behind an injectable driver, recorded |
| 70 | +the first time, and replayed verbatim on recovery: |
| 71 | + |
| 72 | +| Seam | Wire with | Recorded as | |
| 73 | +| --- | --- | --- | |
| 74 | +| **Clock** (timers) | `WithRunnerClock` | `JournalClockRead`, replayed so timers fire at the same recorded instants, wall-clock-independent; armed deadlines survive checkpoint compaction | |
| 75 | +| **Invoked services** | `WithServiceRegistry` + `Handle.RunService` | `JournalServiceResult`; the service runs once, then recovery replays its result through the kernel's settle seam | |
| 76 | +| **Child-machine actors** | `WithActorPalette` + `Handle.DeliverToActor` | `JournalActorMessage`; the behavior runs once, then recovery re-fires the recorded parent transition | |
| 77 | +| **Domain effects** | `WithEffectHandler` | dispatch set, applied exactly once via deterministic `EffectID` dedup | |
| 78 | + |
| 79 | +Use `WithCheckpointEvery(n)` to tune how often a full snapshot is written: a |
| 80 | +shorter interval bounds recovery replay, a longer one cuts checkpoint cost. |
| 81 | + |
| 82 | +## Stores |
| 83 | + |
| 84 | +`Store` is the persistence seam. A durable instance is an ordered log of |
| 85 | +`Record`s (one per `Fire` step) layered over periodic full-snapshot checkpoints. |
| 86 | +Two stdlib-only reference implementations ship in-tree: |
| 87 | + |
| 88 | +- **`MemStore`** (`NewMemStore`): in-memory, thread-safe, not durable across |
| 89 | + restarts. For tests, examples, and single-process development. `WithHistory` |
| 90 | + retains the full record history, enabling time-travel below. |
| 91 | +- **`FileStore`** (`NewFileStore`): on-disk, a directory of per-instance |
| 92 | + subdirectories, each an append-only journal, an atomic checkpoint, an |
| 93 | + idempotency ledger, and a dispatched-effect log. Each append flushes to stable |
| 94 | + storage; each checkpoint uses write-temp-plus-rename for crash-safe atomicity. |
| 95 | + Use it for durability across restarts without a database. |
| 96 | + |
| 97 | +Persistent database backends (PostgreSQL, DynamoDB, and the like) implement |
| 98 | +`Store` out of tree, so their drivers never burden this module's dependency or |
| 99 | +vulnerability surface. |
| 100 | + |
| 101 | +## Time-travel reader |
| 102 | + |
| 103 | +`StateAt` reconstructs an instance's state as of any recorded step, read-only: |
| 104 | +restoring the start baseline and replaying recorded events forward to the target |
| 105 | +step, running no service, re-instantiating no actor, reading no wall clock, and |
| 106 | +dispatching no effect: |
| 107 | + |
| 108 | +```go |
| 109 | +view, err := durable.StateAt(ctx, machine, store, "order-42", 3) |
| 110 | +// view.Snapshot(), view.Instance(), view.Step(): detached and safe to read |
| 111 | +``` |
| 112 | + |
| 113 | +Time-travel needs the full record history through the target step. A `Store` opts |
| 114 | +in by implementing `HistoryStore` (the in-tree `MemStore` does so under |
| 115 | +`WithHistory`); `StateAt` otherwise falls back to the latest checkpoint plus tail. |
| 116 | + |
| 117 | +## A note on serialized payloads |
| 118 | + |
| 119 | +Events, service done-data, actor done-data, and actor messages are recorded as |
| 120 | +their JSON form. A parent reducer that type-asserts a non-JSON Go type from |
| 121 | +`AssignCtx.Event` observes the JSON-decoded shape on a replayed `onDone`. A |
| 122 | +typed-codec option to carry the concrete Go value across the journal boundary is |
| 123 | +reserved for a later, additive change. |
0 commit comments