A comprehensive Event Sourcing and CQRS implementation for order management using Java, Spring Boot, and modern architectural patterns.
- Event Sourcing: Complete audit trail of all order changes
- CQRS Pattern: Separate command and query responsibilities
- Event Store: Persistent storage of all domain events
- Snapshot Mechanism: Performance optimization for large event streams
- Event Replay: Reconstruct order state from historical events
- RESTful API: Full CRUD operations for order management
- Event Timeline Dashboard: Visualize order event history
- Real-time Updates: Event-driven architecture for real-time state changes
- All state changes are captured as events
- Complete audit trail of order modifications
- Event replay capabilities for state reconstruction
- Snapshot mechanism for performance optimization
- Commands: Handle write operations (CreateOrder, AddItem, UpdateStatus)
- Queries: Handle read operations (GetOrder, GetAllOrders)
- Separate data models for read and write operations
- Optimized for different performance characteristics
OrderCreatedEvent: When a new order is createdOrderItemAddedEvent: When items are added to an orderOrderStatusChangedEvent: When order status changes
- Java 17+
- Spring Boot 3.2.0
- Spring Data JPA
- H2 Database (in-memory for development)
- Jackson for JSON serialization
- Maven for dependency management
src/main/java/com/ordersystem/
βββ OrderManagementApplication.java # Main application class
βββ domain/ # Domain models
β βββ Order.java # Order entity
β βββ OrderItem.java # Order item entity
β βββ OrderStatus.java # Order status enum
β βββ OrderAggregate.java # Order aggregate root
βββ eventsourcing/ # Event sourcing infrastructure
β βββ Event.java # Base event class
β βββ EventStore.java # Event store interface
β βββ JpaEventStore.java # JPA event store implementation
β βββ EventEntity.java # Event database entity
β βββ SnapshotStore.java # Snapshot store interface
β βββ JpaSnapshotStore.java # JPA snapshot store implementation
β βββ Snapshot.java # Snapshot class
β βββ SnapshotEntity.java # Snapshot database entity
β βββ events/ # Domain events
β βββ OrderCreatedEvent.java
β βββ OrderItemAddedEvent.java
β βββ OrderStatusChangedEvent.java
βββ cqrs/ # CQRS implementation
β βββ Command.java # Command interface
β βββ Query.java # Query interface
β βββ CommandHandler.java # Command handler interface
β βββ QueryHandler.java # Query handler interface
β βββ commands/ # Command classes
β β βββ CreateOrderCommand.java
β β βββ AddOrderItemCommand.java
β β βββ UpdateOrderStatusCommand.java
β βββ queries/ # Query classes
β β βββ GetOrderQuery.java
β β βββ GetAllOrdersQuery.java
β βββ handlers/ # Command and query handlers
β βββ OrderCommandHandler.java
β βββ OrderQueryHandler.java
βββ api/ # REST API controllers
βββ OrderController.java # Order management API
βββ EventTimelineController.java # Event timeline API
- Java 17 or higher
- Maven 3.6 or higher
-
Clone the repository
git clone https://github.com/imaad/event-sourcing-order-management.git cd event-sourcing-order-management -
Build the project
mvn clean install
-
Run the application
mvn spring-boot:run
-
Access the application
- Application: http://localhost:8080
- H2 Console: http://localhost:8080/h2-console
- API Documentation: Available at
/api/orders
POST /api/orders
Content-Type: application/json
{
"customerId": "CUST001",
"customerName": "John Doe"
}POST /api/orders/{orderId}/items
Content-Type: application/json
{
"productId": "PROD001",
"productName": "Laptop",
"price": 999.99,
"quantity": 1
}PUT /api/orders/{orderId}/status
Content-Type: application/json
{
"status": "CONFIRMED"
}GET /api/orders/{orderId}GET /api/orders?customerId=CUST001&status=CONFIRMED&page=0&size=10GET /api/events/timeline/{orderId}GET /api/events/timeline/{orderId}/from/{fromVersion}GET /api/events/timeline/{orderId}/range/{fromVersion}/{toVersion}GET /api/events/replay/{orderId}The application uses the following configuration (see application.properties):
- Database: H2 in-memory database
- Port: 8080
- Event Store: JPA-based with JSON serialization
- Snapshot Interval: Every 10 events
- Max Events per Replay: 1000
mvn testmvn verifyThe system includes a web-based event timeline dashboard that provides:
- Real-time Event Visualization: See events as they occur
- Order State Reconstruction: Replay events to see order evolution
- Event Filtering: Filter events by type, date, or version
- Snapshot Viewing: View snapshots for performance optimization
- Complete Audit Trail: Every change is recorded as an event
- Temporal Queries: Query order state at any point in time
- Event Replay: Reconstruct order state from historical events
- Debugging: Easy to debug by replaying events
- Compliance: Full compliance with audit requirements
- Snapshots: Periodic snapshots reduce replay time
- Event Batching: Efficient event storage and retrieval
- CQRS: Separate read/write models for optimal performance
- Async Processing: Non-blocking event processing
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
Imaad - GitHub Profile
- Spring Boot team for the excellent framework
- Event Sourcing community for architectural patterns
- CQRS community for command/query separation patterns
Note: This is a demonstration project showcasing Event Sourcing and CQRS patterns. For production use, consider additional security, monitoring, and scalability features.