Enterprise-grade, event-sourced Order Book implementation using CQRS (Command Query Responsibility Segregation) and Event Sourcing patterns with Axon Framework.
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β API Gateway / Load Balancer β
βββββββββββββββββββββββββββββββββββ¬ββββββββββββββββββββββββββββββββββββββββββββ
β
βββββββββββββββ΄ββββββββββββββ
β β
βΌ βΌ
βββββββββββββββββββββ βββββββββββββββββββββ
β Command Service β β Query Service β
β (Port 8081) β β (Port 8082) β
β β β β
β β’ Place Orders β β β’ Get Orders β
β β’ Update Status β β β’ List Orders β
β β’ Cancel Orders β β β’ Statistics β
βββββββββββ¬ββββββββββ βββββββββββ¬ββββββββββ
β β
β βββββββββββββββββββ β
β β RabbitMQ β β
βββββΊβ Event Broker ββββββ€
β β β β
β βββββββββββββββββββ β
β β
βΌ βΌ
βββββββββββββββββββββ βββββββββββββββββββββ
β MongoDB β β MongoDB β
β Event Store β β Read Model β
β β β β
β β’ Domain Events β β β’ Order Views β
β β’ Snapshots β β β’ Indexes β
βββββββββββββββββββββ βββββββββββββββββββββ
Event Sourcing : Complete audit trail of all order state changes
CQRS Pattern : Optimized read and write paths for scalability
Axon Framework : Production-ready event sourcing and DDD support
MongoDB : Flexible document store for events and read models
RabbitMQ : Reliable async event distribution
OpenAPI 3.0 : Interactive API documentation with Swagger UI
Observability : Prometheus metrics, health checks, structured logging
Docker Ready : Full containerization with docker-compose
order-book/
βββ common/ # Shared module
β βββ src/main/java/
β βββ rise/trader/orderbook/common/
β βββ command/ # Command definitions
β βββ event/ # Event definitions
β βββ domain/ # Domain value objects
β βββ query/ # Query definitions
β βββ exception/ # Shared exceptions
β βββ validation/ # Business rule validators
β
βββ command-service/ # Write side (CQRS Command)
β βββ src/main/java/
β βββ rise/trader/orderbook/command/
β βββ aggregate/ # Axon aggregates
β βββ api/ # REST controllers & DTOs
β βββ config/ # Configuration classes
β βββ exception/ # Exception handlers
β
βββ query-service/ # Read side (CQRS Query)
β βββ src/main/java/
β βββ rise/trader/orderbook/query/
β βββ api/ # REST controllers & DTOs
β βββ config/ # Configuration classes
β βββ domain/ # Read model entities
β βββ projection/ # Event projections
β βββ repository/ # MongoDB repositories
β
βββ docker/ # Docker configurations
βββ docs/ # Documentation
βββ pom.xml # Parent POM
Component
Technology
Version
Language
Java
17
Framework
Spring Boot
3.2.2
Event Sourcing
Axon Framework
4.9.3
Database
MongoDB
7.0
Message Broker
RabbitMQ
3.12
Build Tool
Maven
3.9+
API Docs
SpringDoc OpenAPI
2.3.0
Containerization
Docker
24+
Java 17 or later
Maven 3.9 or later
Docker and Docker Compose (for local development)
# Clone the repository
git clone https://github.com/prasadus92/order-book.git
cd order-book
# Start infrastructure (MongoDB, RabbitMQ)
docker-compose up -d mongodb rabbitmq
# Build and run services
./mvnw clean package -DskipTests
docker-compose up -d
# Start infrastructure
docker-compose up -d mongodb rabbitmq
# Build the project
./mvnw clean install
# Run Command Service
./mvnw spring-boot:run -pl command-service
# Run Query Service (in another terminal)
./mvnw spring-boot:run -pl query-service
Once the services are running, access the Swagger UI:
Command Service Endpoints
Method
Endpoint
Description
POST
/api/v1/orders
Place a new order
PUT
/api/v1/orders/{id}/status
Update order status
DELETE
/api/v1/orders/{id}
Cancel an order
GET
/api/v1/orders/{id}/events
Get order event history
Method
Endpoint
Description
GET
/api/v1/orders
List all orders (paginated)
GET
/api/v1/orders/{id}
Get order by ID
GET
/api/v1/orders/pending
Get pending orders
GET
/api/v1/orders/user/{userId}
Get orders by user
GET
/api/v1/orders/statistics
Get order statistics
ββββββββββββ βββββββββββββ ββββββββββββββββββββ ββββββββββ
β PLACED βββββΊβ SUBMITTED βββββΊβ PARTIALLY_FILLED βββββΊβ FILLED β
ββββββββββββ βββββββββββββ ββββββββββββββββββββ ββββββββββ
β β β
β β β
βΌ βΌ βΌ
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β CANCEL_SUBMITTED β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β
βΌ
βββββββββββββ
β CANCELLED β
βββββββββββββ
curl -X POST http://localhost:8081/api/v1/orders \
-H " Content-Type: application/json" \
-d ' {
"account": {
"userId": "user-123",
"exchangeAccountId": "binance-456"
},
"symbol": {
"exchange": "BINANCE",
"baseAsset": "BTC",
"quoteAsset": "USD"
},
"quantity": 1.5,
"side": "BUY",
"limitPrice": 50000.00
}'
curl http://localhost:8082/api/v1/orders/{orderId}
curl -X PUT http://localhost:8081/api/v1/orders/{orderId}/status \
-H " Content-Type: application/json" \
-d ' {
"status": "SUBMITTED"
}'
curl -X DELETE http://localhost:8081/api/v1/orders/{orderId}? reason=User%20requested
Variable
Description
Default
MONGODB_URI
MongoDB connection URI
mongodb://localhost:27017/orderbook
RABBITMQ_HOST
RabbitMQ host
localhost
RABBITMQ_PORT
RabbitMQ port
5672
RABBITMQ_USERNAME
RabbitMQ username
guest
RABBITMQ_PASSWORD
RabbitMQ password
guest
# Run all tests
./mvnw test
# Run tests with coverage report
./mvnw verify
# Coverage reports will be in target/site/jacoco/
# Build optimized JARs
./mvnw clean package -Pprod
# Build Docker images
docker build -t order-book-command:latest -f docker/Dockerfile.command .
docker build -t order-book-query:latest -f docker/Dockerfile.query .
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.