Skip to content

Event sourcing in Go with Cratis Chronicle

Cratis Chronicle stores event history for Go applications through github.com/cratis/chronicle.go. Use the client to record typed business events and read their history instead of keeping only the latest state. The example below registers a customer event, appends it to Chronicle, and prints its position in the event log.

A Go struct describes the event payload; Chronicle’s kernel, the server that stores and processes events, records it with its source identity. Projections turn that history into read models for your queries, while reactors run event handlers. Check the Go client’s limits for the workflow you need. The .NET, TypeScript, Kotlin/Java, and Elixir clients have their own APIs and setup.

Area Current source and limits
Typed events, history, and appends Explicit registration, event reads, single and atomic batch appends; inspect both transport errors and append outcomes
Reactors, reducers, and projections Handler, versioning, and projection-shape restrictions apply; reducers fold events into read models in your process
Read-model watches, which stream updates Models marked with data classifications cannot be watched; an interrupted watch terminates
Normal-session recovery Reconnects and replays registration; does not retry ambiguous appends or prove observers have caught up

The client docs explain each workflow. Current source uses contracts 19.123.0; that is not a supported kernel range. Decision reads use a read model to guard a later write against concurrent changes. They require exactly kernel 19.123.0 or 19.123.0-development and protocol 19.123.0, plus the documented shape and ownership conditions. The older append example below uses its own version pair. Generated contracts are public packages in the Go module, so no .NET checkout is needed.

The client requires Go 1.26 or later. The example on this page targets v0.11.0, not the latest source. Select that tag with:

Terminal window
go get github.com/cratis/chronicle.go@v0.11.0

Check the Go package page for what has been published before you depend on it.

For this v0.11.0 example, start the 19.32.3 development kernel and wait for https://localhost:35000/health to report Healthy. The command publishes port 35000 on all interfaces, so use it only on an isolated development machine. The image accepts well-known development credentials.

Terminal window
docker run --rm --name chronicle-go -p 35000:35000 cratis/chronicle:19.32.3-development

This program matches the v0.11.0 quick start. RegisterEvent assigns the struct a stable event type ID; "customer-42" identifies the customer whose history receives the event. The deferred Close releases the client’s resources when the operation finishes.

package main
import (
"context"
"errors"
"fmt"
"log"
"time"
chronicle "github.com/cratis/chronicle.go"
"github.com/cratis/chronicle.go/events"
)
type CustomerRegistered struct {
Name string `json:"name"`
}
func main() {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
if err := appendCustomer(ctx); err != nil {
log.Fatal(err)
}
}
func appendCustomer(ctx context.Context) (err error) {
registry := chronicle.NewRegistry()
if _, err = chronicle.RegisterEvent[CustomerRegistered](registry,
events.WithID("customer-registered")); err != nil {
return err
}
client, err := chronicle.NewClient(chronicle.WithDevelopmentDefaults(),
chronicle.WithRegistry(registry))
if err != nil {
return err
}
defer func() { err = errors.Join(err, client.Close()) }()
store, err := client.EventStore(ctx, "customers")
if err != nil {
return err
}
result, err := store.EventLog().Append(ctx, "customer-42", CustomerRegistered{Name: "Ada"})
if err != nil {
return err
}
if err = result.Err(); err != nil {
return err
}
fmt.Printf("Appended event at position %d\n", *result.Position)
return nil
}

A successful run prints Appended event at position followed by the stored event’s position. Append can return a transport error, while result.Err() reports a rejected append outcome; the program checks both before reporting success.

WithDevelopmentDefaults explicitly permits the kernel’s self-signed certificate. Production TLS validates by default, so configure real roots and credentials. When adding retry or concurrency handling:

  • An append transport error may hide a committed event, so never retry blindly.
  • Default optimistic concurrency leaves empty history unchecked. Use NoMatchingEvent when the first append must be protected.

If you also need an HTTP application around your events, Cratis Arc separates commands that change state from queries that read it. Arc.Go (github.com/cratis/arc.go) brings that approach to Go, with hosting and command/query pipelines. Its source also includes TypeScript proxy generation and optional Chronicle integration; the Arc.Go capability ledger explains the supported subsets and remaining limits.

The root-module tag v0.1.1 is selected with go get github.com/cratis/arc.go@v0.1.1. A root-module tag does not release the separate integration or tools modules. Check those modules’ installation requirements before following an integration or generation guide.

For domain-value types and shared Go utilities, Fundamentals for Go provides a separate starting point. Its v0.3.2 tag is selected with go get github.com/cratis/fundamentals.go@v0.3.2; it does not require a Chronicle kernel.

Looking for another language? See Chronicle in your language for the .NET, TypeScript, Kotlin/Java, and Elixir clients.