Evento Framework
  • Introduction
  • Architecture Overview
    • Distributed Systems & Microservices
    • Recative Manifesto & Reactive Principles
    • State-of-the-art Patterns
      • DDD (Domain Driven Design)
      • CQRS (Command Query Responsability Separation)
      • Event Sourcing
      • Messaging
      • Saga
  • GETTING STARTED
    • Quick Start
    • TodoList - RECQ Tutorial
      • Problem Description and Requirement Gathering
      • RECQ Payload Design
      • RECQ Components Design
      • Set up your Development Environment
      • RECQ Payload Evento Implementation
        • Domain Commands
        • Domain Events
        • Views
        • Queries
      • RECQ Components Evento Implementation with Spring Data
        • TodoListAggregate
        • TodoList Model with Spring Data
        • TodoListProjector
        • TodoListProjection
        • TodoList Invoker
      • Expose the RECQ architecture with Spring Web
      • Test Your App
    • Extend TodoList - Handle Complexity Tutorial
      • Unique identifier generation
      • Extends behaviors with Observer and Services
      • Cross Domain Consistency with Sagas
      • Handle Real time data updates with MQTT and Save-Notify Pattern
  • RECQ Patterns
    • RECQ Patterns
    • RECQ System Pattern
      • Component
      • Message Gateway
      • System State Store
    • RECQ Communication Pattern
      • Component to Component
      • Component to System State Store
      • System State Store to Component
    • RECQ Component Pattern
      • Aggregate
      • Projector
      • Projection
      • Service
      • Invoker
      • Saga
      • Observer
  • Evento Framework
    • Evento Framework Introcution
    • Payload and Messages
      • Command
        • Domain Command
        • Service Command
      • Event
        • Domain and Service Event
      • Query and View
    • @Component
      • @Aggregate
        • Aggregate State
        • @AggregateCommandHandler
        • @EventSourcingHandler
      • @Projector
        • Projector @EventHandler
      • @Projection
        • @QueryHandler
      • @Service
        • @CommandHandler
      • @Invoker
      • @Saga
        • SagaState
        • @SagaEventHandler
      • @Observer
    • Dead Event Queues
    • EventoBundle
      • EventoServerMessageBusConfiguration
      • ConsumerStateStore
        • InMemoryConsumerStateStore
        • PostgresConsumerStateStore
        • MysqlConsumerStateStore
      • Context
      • TracingAgend and @Track
        • SentryTracingAgent
      • Autoscaling Protocol
        • ThreadCountAutoscalingProtocol
      • Injector and @Component
  • Evento Server
    • Evento Server Introduction
    • SetUp Evento Server
      • Advanced Options
      • Evento Event Store Modes: APES and CPES
    • Evento Server Cluster
    • Bundle Deploy Script
  • EVENTO GUI
    • Explore RECQ Systems Visually
    • GUI Auth
    • Payload Catalog
    • Component Catalog
    • Bundle Catalog
    • Cluster Status (Experimental)
    • Flows
      • Performance Evaluation
    • Application Graph
    • System State Store
  • Evento CLI
    • Evento CLI Introduction
    • Update Version
    • Publish
Powered by GitBook
On this page
  1. Evento Framework
  2. @Component

@Aggregate

The Power of Aggregates (@Aggregate)

This chapter dives into the heart of the RECQ architecture within Evento - the Aggregate, represented by the @Aggregate annotation. Here, you'll discover how to define and manage the core unit of consistency in your domain model.

Understanding Aggregates

Imagine a complex domain entity, perhaps an "Order" in an e-commerce system. This entity likely consists of related sub-entities, such as order items, customer information, and shipping details. In the RECQ architecture, these related entities are grouped together under an Aggregate, forming a single unit of consistency.

The @Aggregate annotation in Evento empowers you to define this Aggregate. By marking a class with @Aggregate, you're essentially telling Evento that this class represents the central entity within a cluster of associated objects.

Here are some key characteristics of Aggregates:

  • Single Unit of Consistency: An Aggregate Rootas a single unit of consistency. This means that all changes to the associated entities within the Aggregate must happen atomically. This ensures data integrity within the domain.

  • Encapsulated State: The Aggregate encapsulates the current state of all its associated entities. This state is typically represented by an Aggregate State object (discussed in a dedicated chapter).

  • Event Sourcing: Aggregates are the foundation for Event Sourcing, the technique where all changes to the state are captured as a sequence of immutable events. These events provide a complete history of the Aggregate's state transitions.

The @Aggregate Annotation

The @Aggregate annotation is used to mark a class as an Aggregate Root within the Evento Framework. Here's the breakdown of its definition and key aspects:

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Component
public @interface Aggregate {
    /**
     * Retrieves the snapshot frequency for an annotated aggregate class.
     *
     * @return The snapshot frequency for the aggregate class. Returns -1 if no snapshot frequency is specified.
     */
    int snapshotFrequency() default -1;
}

Usa il codice con cautela.content_copy

  • @Retention(RetentionPolicy.RUNTIME): This ensures that the annotation information is retained at runtime, allowing Evento to access it during application execution.

  • @Target(ElementType.TYPE): This specifies that the annotation can only be applied to class declarations.

  • @Component: This indicates that the annotated class is a component within the Evento framework.

  • snapshotFrequency (default -1): This optional parameter allows you to define the frequency at which a snapshot of the current Aggregate State is persisted. A value of -1 signifies that no automatic snapshotting is configured.

Implementing Aggregates with @Aggregate

@Aggregate(snapshotFrequency = 100)
public class DemoAggregate {
    // ... (code omitted for brevity)
}

In this example, the DemoAggregate class is annotated with @Aggregate. This signifies that it represents the Aggregate Root for the domain concept of "Demo". Additionally, the annotation includes a parameter snapshotFrequency set to 100.

Snapshot Frequency Explained

Event Sourcing provides a complete history of an Aggregate's state changes. However, replaying a large number of events to rebuild the current state can be computationally expensive, especially for long-lived Aggregates.

The snapshotFrequency parameter in the @Aggregate annotation addresses this challenge. It allows you to define the frequency at which a snapshot of the current Aggregate State is persisted. This snapshot captures the state of the Aggregate at a specific point in time.

Here's how snapshotting works in Evento:

  1. Event Sourcing: All changes to the Aggregate state are captured as events.

  2. Snapshotting: After a certain number of events (defined by snapshotFrequency), a snapshot of the current state is persisted.

  3. State Reconstruction: When retrieving the current state of an Aggregate, Evento first checks for the latest snapshot. If a snapshot exists, it serves as the starting point.

  4. Event Replay: Only events that occurred after the last snapshot are replayed to reconstruct the most recent state.

By leveraging snapshots, you significantly improve the performance of event replay, especially for Aggregates with a long history.

In the provided example, snapshotFrequency is set to 100. This means that after every 100 events are applied to the DemoAggregate, a snapshot of its current state will be persisted.

The next chapters will provide a more granular look into the building blocks of an Aggregate:

  • Aggregate State: This chapter explores the object responsible for holding the current state of the Aggregate.

  • AggregateCommandHandler: We'll discover how commands are handled and processed within an Aggregate.

  • Event Sourcing Handler: This chapter dives into how events are applied to update the Aggregate State.

By mastering these concepts, you'll be equipped to design and implement robust and scalable microservices using the RECQ architecture and Evento Framework's @Aggregate annotation.

Previous@ComponentNextAggregate State

Last updated 1 year ago