Skip to content

Eventual Consistency in Projections

Chronicle’s materialized projections are eventually consistent: a successful event append stores the event, but the read model may still show the previous state. Design the query and the screen for that gap, or explicitly wait when a caller needs to read its own change. There is no fixed catch-up time.

An append and a read-model update are separate work. Chronicle stores the event before reporting a successful append. The projection processes it asynchronously and writes the resulting state to its sink. Processing may finish before or after the caller receives the append result; the append does not wait for it.

For example, creating a book can succeed while a query for that book still returns no instance. Show that the command was accepted without claiming the query result is already available.

Materialized projections consume stored events independently of the append call. The same separation applies to reducers and reactors that run as observers.

Event Appended

Event Stored

Append Operation Returns

Background Processor

Projection Updated

Reducer Executed

Observer Notified

A slow or failed projection does not undo an event already stored in the log. It does delay the view that depends on that projection. This lets appends proceed without waiting for each view, while making observer progress and failures part of operating the application.

Observers process events in order within a partition and retry failed processing. A retry can deliver an event again, so do not treat processing as exactly once. If retries are exhausted, a failed partition needs attention before its read model can catch up. See observer retry configuration.

A successful append does not guarantee an updated materialized view, a maximum delay, or a common position across different projections. Do not rely on processing order across independent partitions. Replaying a projection also takes time; registration or a replay request is not proof that its container is ready for queries.

Return the append outcome or the new book’s identifier, then let the query show progress toward the updated view. In this example, the immediate query in CreateBookAndReturn can run before the projection has created the book:

using Cratis.Chronicle.Events;
using Cratis.Chronicle.EventSequences;
using Cratis.Chronicle.ReadModels;
[EventType]
public record EcBookCreated(string Title, string Author);
public record EcBookInventory(Guid Id, string Title, string Author);
public class EcBookService(IEventLog eventLog, IReadModels readModels)
{
// Good — fire and forget: don't wait for the projection before returning
public async Task<EventSourceId> CreateBook(string title, string author)
{
var bookId = EventSourceId.New();
await eventLog.Append(bookId, new EcBookCreated(title, author));
return bookId;
}
// Problematic — expecting immediate consistency
public async Task<EcBookInventory> CreateBookAndReturn(string title, string author)
{
var bookId = EventSourceId.New();
await eventLog.Append(bookId, new EcBookCreated(title, author));
// The projection may not have run yet — this can return a stale or default instance
return await readModels.GetInstanceById<EcBookInventory>(bookId);
}
}

View C# snippet source on GitHub

In the .NET client, .Watch<TReadModel>() emits read-model changes so the screen can update when the projection processes the event. Subscribe before appending and keep the subscription alive for as long as the screen needs updates:

using System.Reactive.Linq;
using Cratis.Chronicle.Events;
using Cratis.Chronicle.EventSequences;
using Cratis.Chronicle.ReadModels;
[EventType]
public record EcWatchBookCreated(string Title, string Author);
public record EcWatchBookInventory(Guid Id, string Title, string Author);
public class EcWatchBookService(IEventLog eventLog, IReadModels readModels)
{
public IObservable<ReadModelChangeset<EcWatchBookInventory>> WatchBookChanges() =>
readModels.Watch<EcWatchBookInventory>();
public async Task CreateBookAndWatch(string title, string author)
{
var bookId = EventSourceId.New();
// Subscribe before appending so the update is observed once the projection catches up
using var subscription = readModels.Watch<EcWatchBookInventory>()
.Where(changeset => changeset.ModelKey.Value == bookId.Value)
.Subscribe(changeset => Console.WriteLine($"Book projection updated: {changeset.ReadModel?.Title}"));
await eventLog.Append(bookId, new EcWatchBookCreated(title, author));
}
}

View C# snippet source on GitHub

The example scopes the subscription to a method. In an application, let the screen or service own its lifetime; returning from that method disposes the subscription and does not wait for an update.

Use Chronicle’s watch API for projection changes. If another application writes directly to the sink, check that database’s notification mechanism separately; the projection watch API is not a general feed of external database writes. Polling a query can refresh a screen, but it does not protect a business decision from concurrent changes.

Keep the command’s append outcome distinct from the query’s projected state. Separating those contracts makes the lag visible instead of hiding it behind a command that promises a current view:

using Cratis.Chronicle.Events;
using Cratis.Chronicle.EventSequences;
using Cratis.Chronicle.ReadModels;
[EventType]
public record EcCqsBookCreated(string Title);
public record EcCqsBook(Guid Id, string Title);
// Commands — fire and forget, never return projected state
public class EcCqsBookCommandHandler(IEventLog eventLog)
{
public Task Create(EventSourceId bookId, string title) =>
eventLog.Append(bookId, new EcCqsBookCreated(title));
}
// Queries — always read from projections
public class EcCqsBookQueryHandler(IReadModels readModels)
{
public Task<EcCqsBook> GetBook(EventSourceId bookId) =>
readModels.GetInstanceById<EcCqsBook>(bookId);
}

View C# snippet source on GitHub

If a caller must read its own append from a materialized view, use the .NET append result’s WaitForCompletion() and check its success, timeout, and failures before querying. Waiting does not make the append and query atomic. Observing appends explains which observers the wait covers.

For an event-source-keyed instance, a passive projection can compute the answer from history at read time instead. Neither a fresh read nor waiting after an append protects a rule checked before it: use constraints or decision reads for that concurrency boundary.

Exercise the screen while a read model is missing or stale, and test what happens when an observer fails or a wait times out. Include a replay in operational checks: a newly registered definition can be valid while its historical rebuild is still running.

Choose read-model consistency based on what the caller needs to know. For a screen, an accepted command followed by a watched update may be enough. For a response that needs the new stored view, wait and handle failure. Before deploying changed mappings, review the registration lifecycle so you also account for rebuilding existing data.