The Query Side
14. What is a projection?
A projection is code that listens to changes on the write side and updates a read model. The read model is the stored result, such as an order_summary table shaped for one screen.
Read models are disposable. If one is wrong, fix the code and rebuild it from the source of truth.
15. How do read models stay up to date?
Three common ways:
- Same transaction: the command handler updates the read table too. Consistent at once, but only within one database.
- Events after commit: the write side stores events or an outbox row, and a relay publishes them to projections.
- Change data capture: a tool such as Debezium reads the database log and streams changes out.
Publishing to a broker straight from the handler, outside the transaction, loses events when the process dies between the two writes.
16. The user saves, then sees old data. How do you handle that?
Pick by how much the lag matters:
- Return the new version from the command, and have the query wait until the read model reaches it.
- Update the UI from the command's own data, then refresh later.
- Push a notification when the projection catches up.
- For the few screens that must be exact, read from the write side.
The worst option is doing nothing and hoping the lag stays under a second.
17. How do you make a projection safe to replay?
Store a checkpoint, the position of the last applied event, in the same transaction as the read model rows. On restart, continue after the checkpoint. Skip anything at or below it.
void handle(Recorded r) {
if (r.position() <= checkpoint) return; // already applied: safe to replay
switch (r.event()) {
case AccountOpened e -> balances.put(e.accountId(), 0L);
case Deposited e -> balances.merge(e.accountId(), e.cents(), Long::sum);
}
checkpoint = r.position(); // save with the row, in one transaction
}
Tested. We ran this projection on Java 25 and fed it the same two events twice. The balance stayed at 5,000 cents, not 10,000, and the checkpoint stayed at 2.
If the checkpoint lives somewhere else, such as a broker offset, a crash between the two writes applies an event twice.
18. How do you rebuild a read model without downtime?
Build the new version next to the old one. Create a new table, run the projection from position zero into it, and let it catch up to live. Then switch queries to the new table and drop the old one.
For large histories, plan for hours, not minutes. Rebuild speed is a real design limit: batching writes and turning off indexes during the rebuild both help.
19. How do you choose a store for a read model?
By the query it serves. A relational table for filtered, sorted lists. A search engine such as OpenSearch for full-text and facets. A key-value store for lookups by ID. A document store for nested views read in one go.
Start with a table in the same database. Add a second engine only when a query needs it, since each one is another thing to run and rebuild.
20. How is a read model different from a cache?
A cache is a copy of answers that can vanish at any time and be refetched from the source. A read model is a maintained view that queries depend on. It is built by the projection, not filled on a cache miss.
So a read model needs what a cache does not: a checkpoint, a rebuild process and monitoring of its lag.
21. Can one read model combine data from several services?
Yes. A projection can subscribe to events from several services and build one view, such as an order page with customer and shipping status. That keeps the screen to one query.
Each source arrives with its own lag and order. The view must handle an order event that arrives before the customer it points to, often by storing a placeholder.