intermediateBuilding the Query Side: Projections

What is a projection, and how does its job differ from an aggregate's job?

A projection is a Spring component with two responsibilities: reacting to events by keeping a simple, plain database table (the read model) up to date, and answering queries by reading directly from that same table. An aggregate, by contrast, is responsible for deciding whether commands are valid and producing events — it never stores current state directly and instead recomputes it by replaying its full event history on demand. A projection never replays anything; it maintains one always-current row per entity by applying events as they arrive, which is what makes reading through a projection a fast, ordinary database lookup instead of a replay operation. In short, the aggregate is the write side, optimized for correctness and auditability, while the projection is the read side, optimized purely for fast, simple queries.

Ready to master this question?

Generate a complete walkthrough — background, the full answer in plain language, a working code example explained line by line, a real-world scenario, common mistakes, and how this same question gets asked in different ways.

Sign in to generate a response

Next Step

Continue to In a complete event-driven system, what guarantees the event store and application database stay consistent when a command handler needs to both persist state and publish an event?← Back to all Event-Driven Microservices questions