PostTradeClearingPlatform
A distributed, event-driven post-trade clearing and settlement platform that simulates a production-grade stock exchange clearing backend.
Accepts trade submissions from market participants
Validates trades (instrument, accounts, quantity)
Updates buyer and seller positions atomically
Performs final settlement of confirmed trades
Publishes events across the system via Apache Kafka
Notifies participants of settlement outcomes
Maintains reference data for instruments and participants
Handles corporate actions (dividends, stock splits)
Style: Event-Driven Microservices
Communication: REST (client) + Kafka (internal)
Consistency: Strong consistency for settlement via transactional writes
Scalability: Horizontal β each service scales independently
Participants
β
βΌ
API Gateway (8080)
β
βΌ
MarketIntegrationGateway (8088)
β
βΌ
Kafka Event Bus
β
ββββββΌβββββββ¬βββββββββββββββ
βΌ βΌ βΌ βΌ
Trade Position ReferenceData CorporateAction
Svc Svc Svc Svc
β
βΌ
Clearing Service
β
βΌ
Notification Service
Service
Port
Responsibility
api-gateway
8080
Auth, routing, rate limiting
market-integration-gateway
8088
Protocol translation, event publishing
trade-service
8081
Trade validation, persistence, event emission
position-service
8082
Position updates, oversell protection
clearing-service
8083
Settlement finalization
reference-data-service
8085
Instruments and participant management
corporate-action-service
8086
Dividends and stock splits (skeleton)
notification-service
8084
Participant notifications
Layer
Technology
Language
Java 21
Framework
Spring Boot 3.3
Gateway
Spring Cloud Gateway
Messaging
Apache Kafka
Database
PostgreSQL
Cache
Redis
Identity
Keycloak (OAuth2 + OIDC)
Migrations
Flyway
Metrics
Prometheus + Grafana
Logging
Fluentd β Elasticsearch β Kibana
Infrastructure
Docker Compose
Topic
Producer
Consumer(s)
trade-submitted
TradeService
PositionService
trade-validated
PositionService
PositionService, ClearingService
position-updated
PositionService
ClearingService
settlement-completed
ClearingService
NotificationService
notification-events
NotificationService
β
reference-data-events
ReferenceDataService
β
corporate-action-events
CorporateActionService
β
trade-dead-letter
All (DLQ)
Monitoring
Partition key: accountId β ensures ordered processing per account.
Java 21
Docker + Docker Compose
Maven 3.9+
cd infrastructure/docker
docker compose up -d
mvn clean install -DskipTests
cd trade-service
mvn spring-boot:run
Submit a Trade (Quick Test)
# Get token from Keycloak
TOKEN=$( curl -s -X POST http://localhost:8180/realms/clearing/protocol/openid-connect/token \
-H " Content-Type: application/x-www-form-urlencoded" \
-d " client_id=clearing-client" \
-d " client_secret=secret" \
-d " username=user" \
-d " password=password" \
-d " grant_type=password" \
| jq -r ' .access_token' )
# Submit trade to API Gateway
curl -X POST http://localhost:8080/api/v1/trades \
-H " Authorization: Bearer $TOKEN " \
-H " Content-Type: application/json" \
-d ' {
"instrumentId": "AAPL",
"buyerAccountId": "ACC100",
"sellerAccountId": "ACC200",
"quantity": 100,
"price": 150.00
}'
Phase
Focus
Status
0
Mono-repo + build setup
β
Done
1
Docker infrastructure
β
Done
2
Database schema + Flyway
β
Done
3
Shared libraries + event contracts
β
Done
4
Backend services (Trade, Position)
π In Progress
5
Security (Keycloak + OAuth2)
β
Done
6
Observability (Prometheus + ELK)
β
Done
7
Failure handling + DLQ
β
Done
8
Frontend (Participant Portal)
β³ Later
9
Performance optimization
β³ Pending
10
Code review + documentation
β³ Pending