Skip to content

Close Cratis Chronicle event streams

Close a stream in Cratis Chronicle when that part of an event source’s history must stop accepting appends. You can close one month’s transactions for an account while leaving other months open. Use a closing event when the closure is a domain fact, or CompleteStream when it is an explicit operation. Closing streams explains the mechanism and its limits.

A closure matches up to four routing values: event source id, event source type, event stream type and event stream id. The definition supplies the source type and declared stream type; your application supplies the instance ids. An append is refused when it matches every value set on the closure. Setting all four lets you close account A’s Account / Transactions / 2026-04 stream without affecting other accounts or months.

When a domain fact ends the stream, declare it with [ClosesStream] on the event type. The kernel records the closure after the durable append in the same processing turn. By default the closed scope is the appended event’s event source id, stream type and stream id, so append the closing event through the definition, to the stream it ends:

using Cratis.Chronicle.Events;
using Cratis.Chronicle.Events.Constraints;
using Cratis.Chronicle.EventSequences;
[EventType]
public record MoneyDeposited(decimal Amount);
// Appending this event closes the scope it is appended to: its event source id, stream type and stream id.
[EventType]
[ClosesStream]
public record MonthClosed(decimal ClosingBalance);
// AccountEventSource is the definition from the previous example.
public class MonthEndWithAnEvent(IEventLog eventLog)
{
public async Task<bool> CloseAndTryAgain(EventSourceId accountId, string month, decimal balance)
{
await eventLog.Append<AccountEventSource>(
accountId,
new MonthClosed(balance),
eventStream: "Transactions",
eventStreamId: month);
var late = await eventLog.Append<AccountEventSource>(
accountId,
new MoneyDeposited(10m),
eventStream: "Transactions",
eventStreamId: month);
// The late deposit is refused. The next month's stream is unaffected.
return late.ConstraintViolations.Any(violation => violation.ConstraintType == ConstraintType.StreamClosed);
}
}

View C# snippet source on GitHub

After MonthClosed is accepted, the example’s next deposit to that account and month is refused with a StreamClosed constraint violation. Other months and accounts remain open. The default [ClosesStream] scope does not include the event source type, so another definition using the same source id, stream type and stream id also matches. To choose other dimensions, derive the stream id from event content, or declare a reopening event, see ReopenedBy, EventStreamIdFrom and the other options in Closing events.

For an ad-hoc close that no domain event represents, build a ClosedStreamScope and call CompleteStream. Take the names from the definition so that they cannot drift from what the appends write:

using Cratis.Chronicle;
using Cratis.Chronicle.Events;
using Cratis.Chronicle.EventSequences;
using Cratis.Chronicle.EventSources;
[EventSource("Account")]
[EventStream("Transactions")]
public class AccountEventSource : IEventSource;
public class MonthEnd(IEventStore eventStore)
{
public async Task<bool> CloseMonth(EventSourceId accountId, string month)
{
// Take the names from the definition, so a rename cannot leave a closure behind with the old name.
var definition = eventStore.EventSources.GetFor(typeof(AccountEventSource));
var transactions = definition.FindStream("Transactions")!;
var scope = new ClosedStreamScope(
EventSourceId: accountId,
EventSourceType: definition.EventSourceType,
EventStreamType: transactions.EventStreamType,
EventStreamId: month);
var closed = false;
var result = await eventStore.EventLog.CompleteStream(scope);
result.Switch(
_ => closed = true,
error => closed = error == CompleteStreamError.AlreadyCompleted);
return closed;
}
}

View C# snippet source on GitHub

definition.EventSourceType and transactions.EventStreamType provide the stored names for the scope. The account id and month select the instance. The example treats AlreadyCompleted as a successful outcome, so a repeated close does not fail its caller.

Leaving EventSourceType out broadens this closure to every source type with the same account id, stream type and stream id. To refuse the close if a matching event arrived after your last known position, pass expectedTailSequenceNumber. Scoped closing shows that check.

  • There is no typed CompleteStream<TSource>. You build the scope from the definition’s values.
  • A closing event and its closure index update are not crash-atomic. If a crash interrupts the update, an explicit reindex can repair the missing closure. See Closing events.
  • Closing a stream does not touch the definition. A closure is stored by value, so renaming a stream later leaves the old closure on the old name. See Questions and pitfalls.
  • A scope that is only the default stream, with no event source id, is refused with DefaultStreamCannotBeCompleted. Appends without a stream go to the default stream, so close the whole event source by id if you want those closed.

Use Scoped closing to choose the exact range to close, or follow a process that closes its own stream. Dynamic Consistency Boundary connects closure to guarded decisions; Dynamic Consistency Boundary in Chronicle explains the broader consistency model.