Aggregate replay or CQRS projection
Operations asks a question the model was not built for: orders shipped per carrier per day. Replay every aggregate on request, or build a projection from the whole history and keep it current. What each costs, and why a checkpoint must not trust commit order.
Operations asks a question nobody planned for: how many orders FASTLINE and AIRCO shipped each day. order-service has no table to ask. An order is its stream of events, and the aggregate loads one stream at a time to decide a command. The answer is in the events, and there are two ways to get it: replay every Order aggregate when the question arrives, or build a table from the whole history now and keep it current.
Learning objectives
- Fold every aggregate in the store to answer a question the write model was not built for
- Keep a projection current with a checkpointed poller instead of a read-time replay
- Order a checkpoint by commit visibility instead of insertion position
- Judge when a replay’s cost against the budget justifies building a projection instead
Why this matters
When operations asks how many orders each carrier shipped per day, the write model can only answer by paying the full cost of the event store on every request, and that cost grows as the business succeeds. Building a second model instead introduces a new way to get a wrong or stale answer: a bug repeated into every row, or a checkpoint that skips a committed order with no error to show for it. The choice is a bet on how often the question gets asked against how much staleness and complexity the business can tolerate.
Pattern: Projection rebuilt from history
The problem
The write model answers one question well: may this command happen to this order. “Shipments per carrier per day” touches every stream, and nothing indexes it.
Replay answers it with no new moving part: read the events, fold each aggregate, count. It is correct by construction, because it uses the fold the commands use. Its cost is the size of the store, paid on every request. A snapshot bounds the load of one aggregate and does nothing for a question about all of them.
The other answer is a second model for reading. Greg Young splits a service into a command side and a query side, and lets the query side be built from the events, denormalised and eventually consistent. Martin Fowler warns that this adds risky complexity to most systems. A projection is worth it only when the question is asked often enough, and a replay is slow enough, to justify a second model that can be wrong.
The solution
A projection is a read model built by folding the event history in order, kept current by applying each new event once, and discarded and rebuilt from the first event when its logic or its question changes.
Three decisions make it safe. The events stay the only record: a projection is a cache of a fold, so losing it costs time and never data, and the Azure Architecture Center calls a materialized view completely disposable for that reason. The projector keeps a checkpoint, committed with the rows it produced, so a crash repeats nothing and loses nothing. And the checkpoint advances only past events that no late commit can precede.
How Quarkus models it
OrderAggregate is an event-sourced order aggregate: it folds OrderPlaced, OrderPaid, OrderShipped, OrderDelivered and OrderCancelled into a status, a carrier and the instant of the shipment. An order cannot be cancelled after it ships, so one that reached SHIPPED or DELIVERED is a shipped order.
ReplayReport reads every event stream by stream and folds one aggregate at a time:
try (var events = store.replayStream()) {
events.forEach(event -> {
if (!event.streamId().equals(current[0])) {
count(order[0], counts);
current[0] = event.streamId();
order[0] = OrderAggregate.start();
}
order[0].apply(event.payload(), event.occurredAt());
});
}
replayStream() is a Panache projection to a record. A stream of entities keeps every row in the persistence context until the transaction ends; a projection leaves nothing managed. The day of a shipment is its UTC day, in the replay and in the projection alike.
CarrierDailyProjector is a Quarkus scheduler method. Each tick runs batches until the events it may read are used up:
@Scheduled(every = "${projection.poll}", concurrentExecution = Scheduled.ConcurrentExecution.SKIP)
void tick() {
int read;
do {
read = batch.run(batchSize);
} while (read == batchSize);
lag.set(batch.lagEvents());
}
SKIP stops a slow tick from overlapping the next in one instance, and no further, so ProjectionBatch also locks the checkpoint row with PESSIMISTIC_WRITE: a second instance waits and then reads past the first. One batch is one @Transactional method: lock the checkpoint, read up to projection.batch events, add the counts to carrier_daily_shipments, move the checkpoint.
The read is native SQL, because the xid8 comparison and pg_snapshot_xmin are not mapped by the entity:
SELECT position, tx_id::text::bigint AS tx, type, payload->>'carrier', (occurred_at AT TIME ZONE 'UTC')::date
FROM orders.events
WHERE (tx_id, position) > (CAST(CAST(:t AS text) AS xid8), :p)
AND tx_id < pg_snapshot_xmin(pg_current_snapshot())
ORDER BY events.tx_id, events.position
LIMIT :lim
The events table has a tx_id xid8 NOT NULL DEFAULT pg_current_xact_id() column, the transaction that wrote the row. GET /reports/shipments-per-carrier?source=replay or source=projection serves either path; the projection answer also reports lagEvents, the committed events past the checkpoint, and a gauge of the same number leaves for Prometheus.
The same question, two ways
Both paths return the same lines: a carrier, a UTC day and a count. A SQL query over the raw events, grouping the OrderShipped payloads of streams that were not cancelled, is the oracle for both. A replay that disagrees with it has a wrong fold; a projection that disagrees with the replay has a wrong projector or has not caught up.
A new question differs between them. A new replay is code, and the answer is exact at once. A new projection is code plus a rebuild from the first event, and until it has read them all it undercounts without saying so in the rows. lagEvents says so, and a rebuild is finished when it reaches 0.
What each answer costs
Both are timed inside the service, so the HTTP round trip is not in the numbers. The store is loaded cumulatively to 1,000, 10,000 and 100,000 orders, which is 3,600, 36,000 and 360,000 events. At each size three loads are discarded and 11 are timed, alternating replay and projection read so that drift affects both. A rebuild is timed from the moment the projection is emptied to the moment the checkpoint reaches the newest event; the one-second tick bounds it at the two smaller sizes, and four runs gave 0.3 to 1.0 s there and 1.3 to 2.0 s at 100,000 orders. The database runs on the compose stack of an 8-core laptop, and each figure is the median of three runs from fresh containers.
| Replay every aggregate | Projection | |
|---|---|---|
| Read at 1,000 / 10,000 / 100,000 orders | 15 / 77 / 586 ms | 0.78 / 0.61 / 0.46 ms |
| Freshness | The committed state at the read | One scheduler tick, about 1 s at the default |
| A new question | Code only, exact at once | Code plus a rebuild: 781 ms / 766 ms / 1.8 s |
| Storage | None | 27 rows at every size, against an events table of 1.1 / 10 / 103 MB |
| What can go wrong | The read cost grows with the store | Lag, a commit-order gap, a projector bug repeated until a rebuild |
The replay grows in step with the store, at about 1.6 microseconds per event at 100,000 orders, while the projection read scans the same 27 rows at every size. At 1,000 orders the difference is 14 ms and a replay is the better choice, because there is nothing to keep correct. At 100,000 a dashboard that refreshes every few seconds pays more than half a second of database and CPU each time for an answer that changed by a handful of rows. The projection read is about 1,300 times faster.
Freshness is the projection’s price. A command reaches the projection within a tick, and a run of 50 commands at the one-second default gave a median of 994 ms. A question that must see the command it just committed belongs to the replay or to the aggregate.
Commit order is not position order
The obvious checkpoint is the last position read: WHERE position > :last. It loses rows without a trace.
A position is assigned when a row is inserted, and a commit happens later. Transaction A inserts position 41 and keeps working. Transaction B inserts 42 and commits. The projector reads 42 and stores it as its checkpoint. A commits, its row becomes visible at 41, behind a checkpoint that never goes back. The order A shipped is not counted, and the log shows no error.
The batch query closes the gap with two conditions. Rows are read in (tx_id, position) order, and only those below pg_snapshot_xmin(pg_current_snapshot()), the lowest transaction id still running. Every transaction below that id has finished, so no row can appear later before the checkpoint. While A is open nothing past A is read and lagEvents stays at 1 or more. When A commits, both rows are read in the next batch.
The price is that one long transaction holds the projection back, and any open transaction in the database counts, an idle-in-transaction session included. The stall is visible, because the report shows lagEvents at 1 or more while the transaction is held.
Build, run, observe
cd examples/17-aggregate-replay-or-projection
mvn -B verify
./verify.sh
verify.sh loads three store sizes and compares an aggregate replay with a projection that a checkpointed poller keeps current. Watch the projection equal the replay at every size and answer a read much faster at 100,000 orders. It prints 22/22 checks passed.
Try one change and rerun:
PROJECTION_WATERMARK=off ./verify.sh(demo code): the projection moves past an open transaction and never counts that order.PROJECTION_POLL=5s ./verify.sh: a command reaches the projection only within a tick, now 5 seconds.POST /demo/projection/rebuild(demo code): the projection is emptied and its checkpoint rewound, andlagEventsfalls back to 0.
When not to use it
When the question is rare or the history is small, replay. A report that runs once a day over 10,000 orders costs 77 ms, and a projection for it adds a table, a checkpoint, a timer and a rebuild to keep correct. Measure the replay against the budget of the read before building anything.
A projection serves the questions it was built for. A new question needs a new fold and a rebuild from the first event, so the store must keep that history for as long as such questions can arrive. A bug in the fold is repeated into the rows until the rebuild. The Azure Architecture Center advises serving the last complete view while a new one is built; this projection undercounts for the length of the rebuild.
When a command must see its own write, read the aggregate: a projection is eventually consistent, and the gap is a tick plus a batch. A read model fed from Kafka topics has other problems: lag, and the order between topics. A projector that reads the event store itself has commit order inside one database.
Common pitfalls
- Checkpointing by position instead of by commit visibility skips a row from a transaction that committed after a later one, with no error to mark the gap.
- Treating a projection as current the moment it is built ignores the rebuild time a new question needs before its counts can be trusted.
- Expecting a command’s own write to show up in a projection immediately ignores the tick and batch delay built into every read.
- Holding a long-running transaction open stalls every projection batch behind it, since no row past an open transaction can be read yet.
🤖 Explore with your agent “In examples/17-aggregate-replay-or-projection, ask for shipments per carrier through ReplayReport and through CarrierDailyProjector, then find where projection_checkpoint and the tx_id watermark decide which events a batch reads. Set PROJECTION_WATERMARK=off, hold one order’s ShipOrder transaction open while another order ships and commits, and state which of the two the projection never counts and why.” Check: with the watermark both orders are counted once the held transaction commits and the projection equals the replay; with it off the projection stays one order short.
References
- Greg Young, CQRS Documents, 2010: the command and query sides, and a read model built from the events
- Martin Fowler, CQRS, 2011, and Event Sourcing, 2005
- Microsoft Azure Architecture Center, CQRS pattern and Materialized View pattern: views regenerated by replaying history, and staleness while a view is rebuilt
- Alex Lawrence, Message- and Event-Driven Systems with CQRS and Event Sourcing, Addison-Wesley, 2026, chapters 5 and 6
- Adam Bellemare, Building Event-Driven Microservices, 2nd ed., O’Reilly, 2025, chapter 9: Deterministic Stream Processing
- Martin Kleppmann and Chris Riccomini, Designing Data-Intensive Applications, 2nd ed., O’Reilly, 2026
- Quarkus, Scheduler reference (
concurrentExecution) and Hibernate ORM with Panache (projections andstream()) - PostgreSQL documentation, Transaction ID and Snapshot Information Functions (
pg_current_xact_id,pg_current_snapshot,pg_snapshot_xmin,xid8), SELECT (FOR UPDATE) and Set Returning Functions (generate_series) - Aggregate root and the event store
- Snapshots
- CQRS and projections