Skip to content

Read Models

Read models are shared Chronicle concepts. Querying, snapshots, watching, and consistency are documented in the shared Chronicle section.

Use watch/2 for changesets on a dedicated stream, subscribe/2 to share a watch between processes, or observe/2 for a page of instances. For a shared watch, reconnection does not replay changes missed while disconnected.

Chronicle.read_model/3, and Chronicle.ReadModels.get/3 which it calls, return {:ok, nil} when no instance exists for the key. That is also what you get right after an append whose projection hasn’t run yet, because Chronicle projects asynchronously:

case Chronicle.read_model(MyApp.ReadModels.AccountInfo, account_id) do
{:ok, nil} -> :not_found_or_not_projected_yet
{:ok, account} -> {:ok, account}
{:error, reason} -> {:error, reason}
end

When you need to read your own write, append with Chronicle.EventSequences.EventLog.append_and_wait_for_completion/3, which returns once the affected observers have processed the event. See Event sequences.

Chronicle.ReadModels.watch/2 subscribes the calling process to live changesets for a read model over the kernel’s server-streaming Watch RPC. Elixir has no IObservable/ async-iterable equivalent to the C# and TypeScript clients’ Watch<TReadModel>(), so this follows the same message-based idiom as Chronicle.Connections.Lifecycle.subscribe/1.

{:ok, watcher} = Chronicle.ReadModels.watch(MyApp.ReadModels.AccountInfo)
receive do
{:chronicle_read_model_changed, MyApp.ReadModels.AccountInfo, changeset} ->
# changeset is a %Chronicle.ReadModels.Changeset{} with :model_key, :read_model,
# :change_type (:added | :modified | :removed), :event_sequence_number, and more.
IO.inspect(changeset.read_model)
{:chronicle_read_model_watch_error, MyApp.ReadModels.AccountInfo, reason} ->
# The stream failed and the watch has ended — call watch/2 again to resume.
IO.inspect(reason)
end
Chronicle.ReadModels.unwatch(watcher)

Chronicle.ReadModels.subscribe/2 is a shared alternative to watch/2: every caller that subscribes to the same read model (same client, namespace, and event sequence) is served by one kernel stream instead of each caller opening its own. Read model reactors use it, so a reactor and an ad-hoc subscriber for the same read model share a stream automatically.

{:ok, watcher} = Chronicle.ReadModels.subscribe(MyApp.ReadModels.AccountInfo)
receive do
{:chronicle_read_model_changed, MyApp.ReadModels.AccountInfo, changeset} ->
IO.inspect(changeset.read_model)
end
Chronicle.ReadModels.unsubscribe(watcher)

On top of watch/2’s messages, a subscriber also gets:

  • {:chronicle_read_model_watch_ended, model_module, reason} — the shared stream failed. Every subscriber gets this once; the watcher reconnects on its own with exponential backoff (500 ms, doubling up to 30 s).
  • {:chronicle_read_model_watch_resumed, model_module} — the stream reconnected.

Changes made while the stream is down are not replayed. The shared watcher closes its stream once its last subscriber unsubscribes, or exits.

Chronicle.ReadModels.observe/2 sends the calling process the page of instances now and again each time it changes:

{:ok, observer} = Chronicle.ReadModels.observe(MyApp.ReadModels.AccountInfo, page_size: 25)
receive do
{:chronicle_read_model_instances, MyApp.ReadModels.AccountInfo, %{instances: accounts}} -> accounts
end
Chronicle.ReadModels.unobserve(observer)

An error arrives as {:chronicle_read_model_observe_error, model, reason} and ends the observation; call observe/2 again to resume. Use watch/2 or subscribe/2 for changesets rather than a page of instances.

Pass :filter/:sort to keep and order only matching instances in each delivered page — same shape as query/2’s filter and sort. total_count then counts the matches within that page, not the whole read model. observe_stream/2 wraps observe/2 as an Elixir Stream of %Chronicle.ReadModels.QueryResult{}:

MyApp.ReadModels.AccountInfo
|> Chronicle.ReadModels.observe_stream(page_size: 25)
|> Enum.each(fn result -> IO.inspect(result.instances) end)

A read-model session (created implicitly whenever you read with a :session_id) normally expires on its own. dehydrate_session/4 explicitly cleans one up once the caller is done with it, releasing the resources Chronicle held for it right away.

:ok =
Chronicle.ReadModels.dehydrate_session(
MyApp.ReadModels.AccountInfo,
account_id,
session_id
)

Chronicle.ReadModels.query/2 accepts :filter and :sort. All conditions must match; a condition is a value (equality) or one of {:gt, v}, {:gte, v}, {:lt, v}, {:lte, v}, {:ne, v}, {:in, [v]}, {:contains, v}:

{:ok, result} =
Chronicle.ReadModels.query(MyApp.ReadModels.Entry,
filter: [schema_id: "invoice", amount: {:gt, 100}],
sort: {:amount, :desc},
page_size: 20
)
result.total_count # the number of *matching* instances

An unknown field returns {:error, {:unknown_field, field}}.

The filter is applied by the client. The kernel’s query contract pages instances but cannot filter them, so the client pages through the read model and evaluates the filter itself. That is right for read models of modest size. A read model with more than :max_scan instances (default 10_000) returns {:error, {:scan_limit_exceeded, max_scan}} rather than a silently partial answer. For large data, give the question its own projection.

A Chronicle.ReadModels.Reactor reacts to changes of a read model rather than to events:

defmodule MyApp.Reactors.BalanceWatcher do
use Chronicle.ReadModels.Reactor, model: MyApp.ReadModels.AccountInfo
@impl true
def on_modified(account, _changeset) do
if account.balance < 0, do: Alerts.overdrawn(account)
:ok
end
end
{Chronicle.Client, read_model_reactors: [MyApp.Reactors.BalanceWatcher], ...}

on_added/2, on_modified/2 and on_removed/2 are all optional. The reactor subscribes through Chronicle.ReadModels.subscribe/2, so it shares its kernel stream with any other subscriber of the same read model, receives changes that happen while it runs, and re-establishes its subscription after a reconnect.

Decision reads and the Dynamic Consistency Boundary

Section titled “Decision reads and the Dynamic Consistency Boundary”

Chronicle.ReadModels.DecisionReads folds a read model at a key and carries forward the exact log position the fold stopped at, so a subsequent append can be guarded against anything else changing that read model first. See Dynamic Consistency Boundary.

A field whose default is a plain map (parameters: %{}) is registered as an object, its keys kept exactly as you wrote them. Nested structs keep their multi-word field names. Fields that default to nil, or hold a list of structs, are declared with types::

use Chronicle.ReadModels.ReadModel,
types: [lines: {:list, MyApp.OrderLine}, address: MyApp.Address, labels: :map]

See Chronicle.Serialization.