intermediateDesigning Commands, Events & Queries

What does the @TargetAggregateIdentifier annotation do on a command, and why doesn't a query need an equivalent annotation?

@TargetAggregateIdentifier marks the field on a command that tells Axon which specific aggregate instance the command should be routed to and handled by — for example, the customerId field on a command meant for one particular customer. Axon uses that field's value to find, or in a creation command's case start, the exact matching aggregate instance. Queries don't need an equivalent annotation because they aren't routed to an aggregate at all; aggregates only ever handle commands and their own events. A query is instead routed to whichever component — typically a projection reading from a fast, pre-built read model — actually knows how to answer that question, and no aggregate ever gets involved in answering a read.

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 Why does a strict, tense-based naming convention for commands, events, and queries matter more on a real team than it might seem?← Back to all Event-Driven Microservices questions