Do separate read and write targets make CQRS?
The Customer CRUD sample uses separate targets to create a customer and list customers. Both targets still use the same in-memory customer table. CQRS (Command Query Responsibility Segregation) separates the model for recording changes from the model that serves queries.
Follow two targets in Customer CRUD
- Write a customer
- Customer page →
POST /api/customers→samples.customer.create→create()→ writes toInMemoryCustomerStore - Read the list
- Customer page →
GET /api/customers→samples.customer.list→list()→ reads fromInMemoryCustomerStore
In the Spring Boot sample, CoAkka Runtime delivers each request to the handler registered under its target and matches the reply. Both handlers use the same store instance. After creation finishes, the next read sees the new customer in that process.
When the application separates read and write models
| Question | Customer CRUD today | A CQRS design |
|---|---|---|
| Where does the call go? | Two targets lead to create() and list(). |
You can keep both targets. The change is in how the handlers read and write data. |
| Who decides which changes are valid? | The handler calls create() on the shared store. |
The write model enforces business rules and records changes. For example, it can allow tier upgrades only for eligible customers. |
| What does the screen read? | The list handler reads the same table that the create operation updated. | The read model serves queries. For example, a directory might include just the customer ID, display name, and tier for filtering on screen. |
| When does the change appear? | A read after the write finishes sees the new record in the running store. | When the directory updates asynchronously, an immediate read can still show old data. The application defines when the screen needs the updated result. |
Both models can use the same database. For example, the write model stores customer records and checks eligibility for upgrades. The read model queries a view designed for the directory. You can try this in one process.
If a background task updates the directory
The application might have recorded Gold while the directory still shows the previous tier. The app decides how to show that an update is pending and measures how long it takes the directory to catch up.
If an update must survive a restart, record the work in the same transaction as the customer change. A worker can read the committed job and send it through CoAkka Runtime. The directory handler must handle duplicate delivery safely. See how a transactional outbox recovers.
If the application stores change history as its source of truth, see how Event Sourcing rebuilds state from events.
Run the Customer CRUD sample
You need JDK 17 and a checkout of CoAkka Samples. Run the command from the repository root. The build uses published packages from Maven Central.
./gradlew -p coakka-runtime/scenarios/customer-crud/framework-registry :runtime:scenarios:customer-crud:spring-boot-starter-local:customer-app:bootRun
Command for Windows PowerShell
.\gradlew.bat -p coakka-runtime/scenarios/customer-crud/framework-registry :runtime:scenarios:customer-crud:spring-boot-starter-local:customer-app:bootRun
- Open
http://localhost:8082, create a test customer, and find the new entry in the list. - Open
http://localhost:8082/api/customers/runtimeto see the registered targets, includingsamples.customer.createandsamples.customer.list. - Stop the app with Ctrl+C. The demo keeps data in memory, so a new run starts with an empty table.