Skip to content

Reactor OnceOnly behavior during replay and recovery

The OnceOnly attribute skips a reactor handler during replay in Cratis Chronicle. Use it when replaying history should not resend a notification or repeat an external integration call. It does not limit the handler to one invocation: initial catch-up and recovery of a failed partition can still call it.

using Cratis.Chronicle.Events;
using Cratis.Chronicle.Reactors;
[EventType]
public record OnceOnlyOrderPlaced(string OrderId);
public class OnceOnlyOrderReactor : IReactor
{
[OnceOnly]
public void SendNotification(OnceOnlyOrderPlaced @event)
{
// This code will only execute once when the event is first processed,
// and will be skipped during replay.
}
}

View C# snippet source on GitHub

In this example, replay skips the marked handler. Ordinary event delivery still invokes it.

Replay re-delivers historical events after an observer rewind, redaction or revision. OnceOnly suppresses the marked handler for those replay deliveries.

In C#, renaming a reactor or moving its namespace changes its default id. A new observer catches up from the beginning as ordinary observation by default, so OnceOnly handlers can repeat side effects. Preserve the existing id explicitly before refactoring; see Reactor identity and renaming. For a new reactor that should skip history on its first registration, use StartAtTail.

Recovery re-delivers a failed partition’s event as ordinary observation, so a OnceOnly handler can run again. The previous attempt may have failed before or after its side effect completed. The attribute cannot tell which happened and is not an exactly-once guarantee.

Use a delivery identity to recognize that repeated delivery. It supplies a stable key for an application-owned receipt or a receiver’s idempotency key. The receipt must be atomic with the effect, or the receiver must deduplicate requests; checking and recording a key around an external call still leaves a failure window.

In C#, the built-in handlers derive idempotency keys for returned events. On a kernel that supports idempotent appends, repeating the same keys and output does not append another copy. This protects the append, while [OnceOnly] skips the handler during replay. Neither prevents an external call from repeating during ordinary recovery.

If replay should deliberately append changed output, review the effect epoch. Raising it permits new appends for the same input. If replay should do no work for this handler, keep [OnceOnly].

Put OnceOnly on a method when only that handler should be skipped during replay. The reactor remains replayable, and its other handlers can process replayed events.

Put it on the class when none of the reactor’s handlers should replay. The whole reactor registers as non-replayable. This does not stop ordinary recovery or initial catch-up, so choose the placement by what replay should do, not by an expectation of one-time execution.