Skip to content

CQRS in Kotlin and Java with Arc on Spring Boot

CQRS (Command Query Responsibility Segregation) gives state-changing operations and read operations separate models. Registering an author is a command with the inputs needed to register them; listing authors is a query that returns a view for the reader. You can keep both in one application over one database. Neither an event store nor separate services are required.

Cratis Arc brings this model to Kotlin and Java on Spring Boot, published to Maven Central as io.cratis:arc. A command is an annotated class with a handle method, and a query is a static method on a @ReadModel. Kotlin Symbol Processing (KSP) generates HTTP endpoints and typed TypeScript clients at compile time, so you do not write a controller per operation. Cratis Chronicle adds event sourcing when you need a history of changes.

The examples below register and list authors using MongoDB, then show the event-returning command. If you are weighing the design before choosing a framework, read CQRS explained.

Capability How it looks in Arc Publication
Commands @Command class with handle; regular, suspend (Kotlin) or CompletionStage (Java) handlers Artifacts published
Queries and read models @ReadModel with static or @JvmStatic companion query methods; GET and RFC QUERY Artifacts published
Live queries Kotlin Flow, JDK Flow.Publisher or RxJava 3 return types; SSE and WebSocket Artifacts published
Validation Jakarta Bean Validation, plus reusable Arc validators Artifacts published
Authorization @Authorize, @Roles and @AllowAnonymous on commands and queries Artifacts published
Persistence Spring Data JPA and MongoDB integrations Artifacts published
Typed frontend TypeScript proxies generated at build time Artifacts published
Event sourcing Chronicle through io.cratis:cratis; handle returns an event; optional Artifacts published

The artifacts listed above are published. Arc on the JVM does not claim full parity with Arc on .NET. The shared capability matrix and the JVM parity reference record each row, the evidence behind it, and what is not planned. One example: Arc on the JVM generates model-bound endpoints only, not controller-based ones.

RegisterAuthor carries the author’s identifier and name as properties. Its handle method receives AuthorRepository from Spring through @FromServices and inserts the author. This version uses Spring Data MongoDB, with no Chronicle dependency:

import io.cratis.arc.artifacts.Command
import io.cratis.arc.artifacts.FromServices
import java.util.UUID
@Command
data class RegisterAuthor(val id: UUID, val name: String) {
fun handle(@FromServices authors: AuthorRepository) {
authors.insert(Author(id, name))
}
}

The query side is a read model with a static method. Arc serves it over HTTP and generates the client types:

import io.cratis.arc.artifacts.FromServices
import io.cratis.arc.artifacts.ReadModel
import org.springframework.data.mongodb.repository.MongoRepository
import java.util.UUID
interface AuthorRepository : MongoRepository<Author, UUID>
@ReadModel
data class Author(val id: UUID, val name: String) {
companion object {
@JvmStatic
fun allAuthors(@FromServices authors: AuthorRepository): List<Author> = authors.findAll()
}
}

The command inserts through AuthorRepository, and the query reads from it. Separating them does not mean duplicating the database: it gives the registration input and author list their own contracts. To run these excerpts, add the Gradle setup and Spring Boot application class from the Kotlin or Java getting started guide. KSP runs before compilation and reports invalid model declarations as ARCKSP compiler errors.

Validation, authorization and live queries

Section titled “Validation, authorization and live queries”

Validation and authorization are declared on the model, not inside handle. Authorization annotations go on the command or read-model class or on the operation:

@Authorize(policy = "activeSubscription")
@Roles("member")
@Command
class UpdateProfile {
fun handle(): Unit = Unit
}

An observable query returns a stream. Arc then serves the current value and each change over SSE or WebSocket. In Kotlin, return a Flow:

@ReadModel
@AllowAnonymous
data class TaskView(val id: String, val title: String) {
companion object {
@JvmStatic
fun all(@FromServices repository: TaskRepository): Flow<List<TaskView>> =
repository.observeAll()
}
}

In Java, the same method returns a java.util.concurrent.Flow.Publisher. A source that only emits holds no current value, so a plain GET snapshot answers 202 Not Ready. Kotlin uses MutableStateFlow and Java uses Arc’s ObservableState<T> to hold state. The observable queries guide shows both.

io.cratis:arc has no Chronicle dependency. When you want events as the source of truth, add io.cratis:cratis, the single dependency for an event-sourced Spring Boot application. Then handle returns the fact, and Arc appends it:

import io.cratis.arc.artifacts.Command
import io.cratis.arc.artifacts.CommandKey
import io.cratis.chronicle.events.EventType
@Command
data class RegisterAuthor(@CommandKey val id: String, val name: String) {
fun handle(): AuthorRegistered = AuthorRegistered(name)
}
@EventType
data class AuthorRegistered(val name: String)

An event-returning command needs a stable @CommandKey so Arc knows whose event history to append to. Without one, Arc returns a validation error and does not guess the event source. A Chronicle projection or reducer builds the read model asynchronously. The query can keep its shape, but an immediate read after the command may still return the earlier state.

Follow the Chronicle integration guide to configure the starter and a compatible kernel, then run the samples. For the event store itself, see event sourcing in Kotlin and Java.

Use it when reads and writes have different shapes, load or rules, when a frontend needs typed and live data, or when you may want event sourcing later without changing the query contract.

For a single form over a single table, Spring MVC with Spring Data may be enough. Separate command and query types become extra code to maintain if the two operations have no different requirements. Coming from Spring MVC compares the approaches. Arc on the JVM targets Spring Boot servlet hosting and needs JDK 17; check the getting started guide for the Kotlin compiler version before adding its build tooling.

Is there a CQRS framework for Spring Boot?

Section titled “Is there a CQRS framework for Spring Boot?”

Yes. Arc on the JVM provides commands, queries, validation, authorization and generated TypeScript clients for Spring Boot, in Kotlin and Java. It does not claim full parity with Arc on .NET; the parity reference lists the gaps.

No. CQRS is a design choice. Arc handles commands and queries without an event store, over Spring Data JPA or MongoDB. Add Chronicle only if you want event sourcing.

Can I use CQRS in Java without event sourcing?

Section titled “Can I use CQRS in Java without event sourcing?”

Yes. io.cratis:arc has no Chronicle dependency. The same slice works over JPA or MongoDB, as the example above shows.

Yes. Java uses records or classes with annotations, CompletionStage and Flow.Publisher return types, and blocking bridges. Some contracts need an adapter bean in Java; the capability matrix names them.

Arc generates TypeScript proxies from the same compile-time metadata, for the same @cratis/arc packages used with the other backends. See TypeScript proxies.