Cratis Chronicle passive projections: compute state on demand
A passive projection in Cratis Chronicle computes a read model from events when you request it, rather than maintaining a stored result. Use it for source-local lookups when you want to read from event history without waiting for a background observer to update a read-model store.
Defining a passive projection
Section titled “Defining a passive projection”Call .Passive() on the projection builder. Keep the event mappings you would use for a materialized projection:
using Cratis.Chronicle.Projections;
public class DecPassiveUserSummaryProjection : IProjectionFor<DecPassiveUserSummary>{ public void Define(IProjectionBuilderFor<DecPassiveUserSummary> builder) => builder .Passive() .From<DecPassiveUserCreated>() .From<DecPassiveUserUpdated>();}Kotlin does not support this workflow yet.Java does not support this workflow yet.defmodule MyApp.Projections.DecPassiveUserSummaryProjection do use Cratis.Chronicle.Projections.Projection, model: MyApp.ReadModels.DecPassiveUserSummary, passive: true
alias MyApp.Events.{DecPassiveUserCreated, DecPassiveUserUpdated}
from DecPassiveUserCreated from DecPassiveUserUpdatedendimport { IProjectionBuilderFor, IProjectionFor, projection } from '@cratis/chronicle';
@projection()class DecPassiveUserSummaryProjection implements IProjectionFor<DecPassiveUserSummary> { define(builder: IProjectionBuilderFor<DecPassiveUserSummary>): void { builder .passive() .autoMap() .from(DecPassiveUserCreated) .from(DecPassiveUserUpdated); }}The declaration disables background materialization. ReadModels.GetInstanceById instead reconstructs the requested instance from its event history.
A read model declaring [EventStream<TSource>(stream)] is also passive, even without .Passive(). It stays keyed by event source ID but requires a stream-aware instance read; a key-only read is refused rather than mixing stream IDs. See Read models scoped to one event stream.
Using passive projections
Section titled “Using passive projections”Pass the event source ID to GetInstanceById through the event store’s ReadModels API:
using Cratis.Chronicle;
public class DecPassiveUserService(IEventStore eventStore){ public Task<DecPassiveUserSummary> GetUserSummary(string userId) => eventStore.ReadModels.GetInstanceById<DecPassiveUserSummary>(userId);}Kotlin does not support this workflow yet.Java does not support this workflow yet.defmodule MyApp.UserService do def get_user_summary(user_id) do Cratis.Chronicle.ReadModels.get_instance_by_id(MyApp.ReadModels.DecPassiveUserSummary, user_id) endendimport { ChronicleClient } from '@cratis/chronicle';
class DecPassiveUserService { constructor(private readonly client: ChronicleClient) {}
async getUserSummary(userId: string): Promise<DecPassiveUserSummary | null> { const store = await this.client.getEventStore('MyStore'); return store.readModels.findInstanceById(DecPassiveUserSummary, userId); }}Read model definition
Section titled “Read model definition”The read model remains an ordinary record or class. This example holds a user’s summary:
public record DecPassiveUserSummary( string Name, string Email, int LoginCount, DateTimeOffset LastLoginAt);Kotlin does not support this workflow yet.Java does not support this workflow yet.defmodule MyApp.ReadModels.DecPassiveUserSummary do use Cratis.Chronicle.ReadModels.ReadModel
defstruct [:name, :email, login_count: 0, last_login_at: nil]endclass DecPassiveUserSummary { name = ''; email = ''; loginCount = 0; lastLoginAt = new Date();}Event definitions
Section titled “Event definitions”The creation and update events supply the user’s name and email. Matching properties map automatically; use explicit mappings when the names differ:
using Cratis.Chronicle.Events;
[EventType]public record DecPassiveUserCreated(string Name, string Email);
[EventType]public record DecPassiveUserUpdated(string Name, string Email);
[EventType]public record DecPassiveUserLoggedIn(DateTimeOffset LoginTime);Kotlin does not support this workflow yet.Java does not support this workflow yet.defmodule MyApp.Events.DecPassiveUserCreated do use Cratis.Chronicle.Events.EventType, id: "dec-passive-user-created"
defstruct [:name, :email]end
defmodule MyApp.Events.DecPassiveUserUpdated do use Cratis.Chronicle.Events.EventType, id: "dec-passive-user-updated"
defstruct [:name, :email]end
defmodule MyApp.Events.DecPassiveUserLoggedIn do use Cratis.Chronicle.Events.EventType, id: "dec-passive-user-logged-in"
defstruct [:login_time]endimport { eventType } from '@cratis/chronicle';
@eventType()class DecPassiveUserCreated { name = ''; email = '';}
@eventType()class DecPassiveUserUpdated { name = ''; email = '';}
@eventType()class DecPassiveUserLoggedIn { loginTime = new Date();}How it works
Section titled “How it works”When you call GetInstanceById on a passive projection:
- Chronicle retrieves the subscribed event types for the requested event source ID, starting at the beginning of its history.
- It applies the projection mappings in memory and returns the computed instance.
- A later read repeats that work from the start rather than reusing the previous read-model state.
There is no persistent read-model sink, meaning no storage destination containing a materialized result. A read can include events you have just successfully appended without waiting for an observer, but it does not reserve that state against another writer. If the result drives a decision that must hold under concurrent writes, use a decision-consistent read.
By-ID reads select one event source. A cross-source join or custom-key aggregation cannot generally reconstruct the same answer as its materialized counterpart this way. See consistency models before making an existing projection passive.
When to use passive projections
Section titled “When to use passive projections”Choose a passive projection when each lookup concerns one event source and replaying its history fits the request’s latency requirements. Infrequent reads over short histories are a good fit: you avoid maintaining a stored result that callers rarely use.
Keep a projection materialized when it joins other sources, aggregates under a custom key, or serves frequent reads over long histories. Passive mode moves the work to read time; it does not remove that work.
Performance considerations
Section titled “Performance considerations”Read cost grows with the number of relevant events for the source. A reactor or command that reads a passive model on every event repeats the full fold each time, so measure that path with representative histories.
For the trade-off between computing from events, reading stored state, and waiting for an observer, continue with consistency models.