Event sourcing in Elixir with Cratis Chronicle
Cratis Chronicle stores event history for Elixir applications through the cratis_chronicle client. Use it when you need to record what happened and derive a read model, a view of those events shaped for your application. The client runs under your application’s OTP supervision tree and connects to the Chronicle kernel, the server that stores and processes events.
In the account example below, opening an account records an immutable event rather than overwriting an account row. A projection runs in the kernel to turn that event into an account read model. Elixir reducers offer a different path: they fold events into read models in your own process.
If you are choosing a language before starting, the Chronicle client selector also covers .NET, TypeScript, and Kotlin/Java. Follow each client’s setup and capability documentation for that language.
Install
Section titled “Install”You need Elixir 1.18 or later. Add cratis_chronicle to your dependencies in mix.exs, replacing <version> with a version from Hex. Use 3.5.1 or later; 3.5.0 has defects affecting read models, reducers, constraints, and seeding.
defp deps do [ {:cratis_chronicle, "~> <version>"} ]endmix hex.info cratis_chronicle prints the current version and the matching dependency line. Then run mix deps.get.
A taste
Section titled “A taste”This example records an account being opened and reads the projected account. It assumes a supervised Mix application named :my_app and a Chronicle development kernel on localhost:35000. Get started with Chronicle covers local hosting; follow the Elixir client guide for the complete Mix project and connection setup.
The event type gives the account-opening fact a stable identity. The read model’s from declaration tells the kernel how to project it; otp_app: :my_app lets the client discover these modules in your application.
defmodule MyApp.Events.AccountOpened do use Chronicle.Events.EventType, id: "account-opened"
# Typed defaults: the client derives the event's JSON schema from them. defstruct owner: "", balance: 0end
defmodule MyApp.ReadModels.Account do use Chronicle.ReadModels.ReadModel
defstruct id: "", owner: "", balance: 0
# owner and balance are mapped by name; id comes from the event source id. from MyApp.Events.AccountOpened, set: [id: :event_source_id]end
defmodule MyApp.Application do use Application
@impl true def start(_type, _args) do children = [ {Chronicle.Client, # Development-only credentials for the local development kernel. connection_string: "chronicle://chronicle-dev-client:chronicle-dev-secret@localhost:35000", event_store: "my-app", otp_app: :my_app} ]
Supervisor.start_link(children, strategy: :one_for_one, name: MyApp.Supervisor) endendThe connection string carries the development kernel’s well-known credentials. Never use them outside local development. The Elixir client skips TLS certificate validation by default; set skipTlsValidation=false and configure trusted certificates for a deployed kernel.
Then, with the application running (for example in iex -S mix), wait for the client to register and append:
alias Chronicle.Connections.Lifecycle
# The client connects and registers in the background; until then, calls return# {:error, :not_connected}.:ok = Lifecycle.wait_until(Lifecycle.name_for(Chronicle.Client), :registered)
:ok = Chronicle.append("account-42", %MyApp.Events.AccountOpened{owner: "Alice", balance: 1000})
# Projections run asynchronously: this can be {:ok, nil} until the projection catches up.{:ok, account} = Chronicle.read_model(MyApp.ReadModels.Account, "account-42")The append’s success does not mean the projection has caught up, so an immediate read can return no model or an older value. In an automated check, wait for the expected account state with a bounded timeout; inspect the projection or failed partition if that timeout expires.
Where to go next
Section titled “Where to go next”Looking for another language? See Chronicle in your language for the .NET, TypeScript, Kotlin/Java, and Go clients.