Skip to content

Event sourcing in Kotlin and Java

Event sourcing keeps business facts as events and derives the state an application reads from them. For an employee record, that means retaining EmployeeHired instead of storing only the current employee details. Reprocessing the recorded history lets you rebuild a view or create another one, as long as the events contain the information it needs.

Cratis Chronicle is an event-sourcing database and processing runtime. Its JVM client, io.cratis:chronicle, works from Kotlin and Java. Annotate an event data class or record with @EventType and a read model with @ReadModel; reducers turn events into that model. The client discovers artifacts on your classpath and registers them with the kernel, Chronicle’s server, when you connect. Kotlin uses suspending calls for kernel access, and Java has blocking bridges. A Spring Boot starter handles the connection, while chronicle-testing provides an in-process test library.

The server checks uniqueness and other configured constraints at append time. It also provides the shared event store for the .NET, TypeScript, and Elixir clients over a gRPC/protobuf boundary. Storage is pluggable: MongoDB is the default, with PostgreSQL, SQL Server, SQLite, and in-memory providers implemented in the kernel. Using the JVM client does not mean running the event store inside the JVM.

Replace <version> with the current release from Maven Central:

build.gradle.kts
dependencies {
implementation("io.cratis:chronicle:<version>")
// Or, in a Spring Boot application:
implementation("io.cratis:chronicle-spring-boot-starter:<version>")
}

This excerpt follows one hiring fact into an employee view. EmployeeStateReducer copies the event’s first name and title into EmployeeState, the read model; it leaves the hiring event unchanged:

@EventType
data class EmployeeHired(
val firstName: String = "",
val lastName: String = "",
val title: String = ""
)
@ReadModel
data class EmployeeState(
val id: String = "",
val firstName: String = "",
val title: String = ""
)
@Reducer
class EmployeeStateReducer {
fun employeeHired(event: EmployeeHired) =
EmployeeState(
firstName = event.firstName,
title = event.title)
}

Start a local development kernel using Get started with Chronicle, then connect and wait for the first registration pass before appending. Registration prepares the artifacts; it does not wait for future events to be processed. The code below keeps the client alive while the reducer builds the model, waiting up to ten seconds before failing on timeout.

import kotlinx.coroutines.delay
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withTimeout
fun main() = runBlocking {
val client = ChronicleClient(ChronicleOptions.development())
try {
val store = client.getEventStore("MyApp")
store.awaitRegistration()
store.eventLog.append("employee-1", EmployeeHired("Ada", "Lovelace", "Engineer"))
val ada = withTimeout(10_000) {
var state = store.readModels.getInstanceByKey(EmployeeState::class, "employee-1")
while (state == null) {
delay(100)
state = store.readModels.getInstanceByKey(EmployeeState::class, "employee-1")
}
state
}
println(ada)
} finally {
client.dispose()
}
}

The employee-1 key connects the append to the read-model lookup. Use a fresh event store for this exercise so that key has no older model: otherwise the loop could return the old state before the new event is processed. The ten-second limit is a demo timeout, not a processing-time guarantee. If it expires, inspect reducer registration and failed partitions instead of treating the append as a completed projection.

Chronicle imports are omitted; the Kotlin/Java client docs show the full setup, including the Spring Boot starter, which connects for you.

Java uses annotated records for events and blocking bridges in place of suspending calls; the client docs show that setup separately.

Event sourcing is useful when you need to explain how an employee record changed or build a new view from earlier facts. The reducer is replaceable processing logic; the recorded facts are the input it has to work with. Keeping that history means maintaining event contracts as the application changes.

A current-state table is simpler if the application only needs today’s employee details. With event sourcing, you also maintain read-model processing and account for its asynchronous updates. When to use event sourcing discusses that choice. To try the JVM path, continue with the Kotlin/Java client docs for the full setup and Java examples.

Want the command and query side too? See CQRS in Kotlin and Java.

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