# IStateMachine

Package: [Microsoft.Orleans.Durable](/orleans/docs/api/csharp/microsoft.orleans.durable/) 10.0.0-alpha.1

[Source](https://github.com/dotnet/orleans/blob/6a0e937f544c35ffcf11b37e4c2b7de5a1580a03/src/Orleans.Durable/Journaling/IStateMachine.cs#L40)

```csharp
public interface IStateMachine
```

Defines the replay, snapshot, and acknowledgement protocol for a journal-backed state machine.

## Remarks

Implementations are owned by a single `Orleans.Durable.Journaling.JournaledStateManager` and are accessed from one logical thread at a time. They participate in the journaling lifecycle as follows:

- User code mutates in-memory state synchronously (typically by invoking codec helpers that both apply the mutation locally and emit the corresponding command to the journal).
- When the application requests a write, the journaled state manager calls [IStateMachine](/orleans/docs/api/csharp/microsoft.orleans.durable/orleans.durable.journaling.istatemachine/) to establish the state's persistence prerequisites, then [IStateMachine](/orleans/docs/api/csharp/microsoft.orleans.durable/orleans.durable.journaling.istatemachine/) (and occasionally [IStateMachine](/orleans/docs/api/csharp/microsoft.orleans.durable/orleans.durable.journaling.istatemachine/)) to materialize the pending changes, then flushes the journal to durable storage.
- [IStateMachine](/orleans/docs/api/csharp/microsoft.orleans.durable/orleans.durable.journaling.istatemachine/) is invoked when the durable write has been acknowledged. Implementations that need to know when state has actually been persisted (for example, to release waiters or trigger downstream notifications) should hook into this callback rather than treating in-memory mutations as durable.
- A failed write or delete permanently fences the manager and requests grain deactivation. Recovery calls [IStateMachine](/orleans/docs/api/csharp/microsoft.orleans.durable/orleans.durable.journaling.istatemachine/) before replaying durable entries, including when initialization is retried.

Application code prepares fallible work in operation-local data and stages only mutations which are safe to commit. Staged mutations are shared by all interleaved callers using the same manager. Storage acknowledgement establishes durability. A new activation creates a fresh manager and state instances; retrying failed initialization resets and replays the existing instances.

## Methods

- [OnRecoveryCompleted](/orleans/docs/api/csharp/microsoft.orleans.durable/orleans.durable.journaling.istatemachine/methods/onrecoverycompleted-ec2ecfb4/)
- [OnWriteCompleted](/orleans/docs/api/csharp/microsoft.orleans.durable/orleans.durable.journaling.istatemachine/methods/onwritecompleted-5ee0375e/)
- [PrepareWriteAsync(CancellationToken)](/orleans/docs/api/csharp/microsoft.orleans.durable/orleans.durable.journaling.istatemachine/methods/preparewriteasync-system-threading-cancellationtoken-d1de03ea/)
- [ReplayEntry(JournalEntry, JournalReplayContext)](/orleans/docs/api/csharp/microsoft.orleans.durable/orleans.durable.journaling.istatemachine/methods/replayentry-orleans-durable-journaling-journalentry-orleans-durable-journaling-j-e75261c2/)
- [Reset(JournalStreamWriter)](/orleans/docs/api/csharp/microsoft.orleans.durable/orleans.durable.journaling.istatemachine/methods/reset-orleans-durable-journaling-journalstreamwriter-a0cda5c6/)
- [WritePendingEntries(JournalStreamWriter)](/orleans/docs/api/csharp/microsoft.orleans.durable/orleans.durable.journaling.istatemachine/methods/writependingentries-orleans-durable-journaling-journalstreamwriter-0b8cb41b/)
- [WriteSnapshot(JournalStreamWriter)](/orleans/docs/api/csharp/microsoft.orleans.durable/orleans.durable.journaling.istatemachine/methods/writesnapshot-orleans-durable-journaling-journalstreamwriter-f5c11b06/)
