Skip to content

Cratis Chronicle: close the books

To close an accounting period in Cratis Chronicle, append a closing event that prevents further writes to that period’s stream. Then record the opening balance in the next period. The history retains both decisions, and the kernel enforces the closed scope for every client.

This recipe uses one event source per ledger and one stream per period. A stream groups that ledger’s events for a period, such as 2026-04. Start with a configured event log and a way to calculate the closing balance. Your application chooses the next period; Chronicle does not rotate streams for you.

  1. Append an event declared with [ClosesStream] to end the old period. The kernel accepts the closing fact and writes its closure before another append can interleave.
  2. Derive the next stream id in the application, for example 2026-05.
  3. Append an opening fact to the new stream with the balance carried over from the old period.

The closing event takes the stream id from its Period property. The event source id and stream type still come from the append, so the closure applies to this ledger’s named period:

using Cratis.Chronicle.Events;
using Cratis.Chronicle.Events.Constraints;
using Cratis.Chronicle.EventSequences;
[EventType]
[ClosesStream(EventStreamIdFrom = nameof(Period))]
public record CloseTheBooksBooksClosed(string Period, decimal ClosingBalance);
[EventType]
public record CloseTheBooksPeriodOpened(string Period, decimal OpeningBalance);
public class CloseTheBooksLedger(IEventLog eventLog)
{
static readonly EventStreamType _ledger = new("ledger");
public async Task<AppendResult> CloseApril(EventSourceId ledgerId, decimal closingBalance)
{
// The closing fact closes (ledger, "ledger", "2026-04") in the same step as the append.
var closing = await eventLog.Append(
ledgerId,
new CloseTheBooksBooksClosed("2026-04", closingBalance),
_ledger,
new EventStreamId("2026-04"));
if (!closing.IsSuccess) return closing;
// The application chooses the next stream id and records the carry-over as its first fact.
return await eventLog.Append(
ledgerId,
new CloseTheBooksPeriodOpened("2026-05", closingBalance),
_ledger,
new EventStreamId("2026-05"));
}
}

View C# snippet source on GitHub

After the first append succeeds, further appends to ledger / 2026-04 for that ledger are refused with StreamClosed. Other ledgers and the 2026-05 stream are unaffected. On success, the second append records May’s opening balance as April’s closing balance.

These are separate calls. If the second append fails or the application stops between them, April remains closed without May’s opening fact. Check the returned result before treating the rollover as complete, and make opening the new period safe to repeat.

The closing event and its closure prevent interleaving, but are not crash-atomic. If the kernel stops after writing the event and before writing the closure, the scope remains open until a constraint reindex rebuilds it. See Closing streams for the recovery boundary.

Producers that post entries need the current period. Read it from a model of the open period rather than from the clock, so a late-closing period keeps accepting entries until it is actually closed.

A materialized read model can lag the log. Right after a rollover, a producer may still read April as the current period. If April is closed, the kernel rejects the append with StreamClosed. Read the current period again before retrying; sending the same append to the same closed stream cannot succeed.

using Cratis.Chronicle.Events;
using Cratis.Chronicle.Events.Constraints;
using Cratis.Chronicle.EventSequences;
[EventType]
public record CloseTheBooksEntryPosted(string Period, decimal Amount);
public interface ICloseTheBooksCurrentPeriod
{
Task<string> For(EventSourceId ledgerId);
}
public class CloseTheBooksPosting(IEventLog eventLog, ICloseTheBooksCurrentPeriod currentPeriod)
{
public async Task<AppendResult> Post(EventSourceId ledgerId, decimal amount)
{
AppendResult result;
var attempts = 0;
do
{
// The current period comes from a read model of the open instance.
var period = await currentPeriod.For(ledgerId);
result = await eventLog.Append(
ledgerId,
new CloseTheBooksEntryPosted(period, amount),
new EventStreamType("ledger"),
new EventStreamId(period));
attempts++;
}
while (IsClosed(result) && attempts < 3);
return result;
}
static bool IsClosed(AppendResult result) =>
result.ConstraintViolations.Any(violation => violation.ConstraintType == ConstraintType.StreamClosed);
}

View C# snippet source on GitHub

The C# example tries at most three appends and returns the last result. If the current-period read model is still behind, all three may fail. Let the caller handle that result instead of retrying indefinitely. The closure keeps those late entries out of the finalized period.

If a closed period sometimes needs a correction, declare a reopening event with ReopenedBy. It is a fact in the log with its own reason, and it lifts only the closure that the closing event wrote. See Closing events. A period that an operator closed manually with CompleteStream stays closed.

The carry-over in this recipe is an ordinary event that your application defines and appends. A memento would capture the old stream’s state as the first event of the new one; Chronicle issue 1370 discusses that approach. This recipe does not depend on it.

Use Closing events to change the dimensions or configure EventStreamIdFrom. Closing streams explains the guarantees and limits. If the next rule you need is uniqueness across event sources, continue with Enforce a unique value.