# IStateMachine

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

[NuGet package](https://www.nuget.org/packages/Microsoft.Orleans.Journaling) | [Source](https://github.com/dotnet/orleans/blob/8efd4352592e66dffd235c823bfb83ecb3945320/src/Orleans.Journaling/IStateMachine.cs#L38)

```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.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.journaling/orleans.journaling.istatemachine/) (and occasionally [IStateMachine](/orleans/docs/api/csharp/microsoft.orleans.journaling/orleans.journaling.istatemachine/)) to materialize the pending changes, then flushes the journal to durable storage.
- [IStateMachine](/orleans/docs/api/csharp/microsoft.orleans.journaling/orleans.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 journal operation permanently fences the manager and requests grain deactivation. A new manager initializes new state instances by calling [IStateMachine](/orleans/docs/api/csharp/microsoft.orleans.journaling/orleans.journaling.istatemachine/) and replaying durable entries.

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; recovery takes place in a fresh manager and state instances.

## Methods

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