Matching Engine Service is the core microservice of the Stock Trading Simulation Platform.
It simulates a real-world stock exchange matching engine by maintaining an in-memory order book, matching BUY and SELL orders, and executing trades using price-time priority.
- 📊 In-memory order book (no database)
- ⚡ Real-time order matching (high performance)
- 💰 Price-time priority (FIFO for same price)
- 🔄 Partial order execution support
- 🧠 Separate order books per stock symbol
- 📡 Event-driven architecture using Redis Pub/Sub
- 📝 Trade generation after successful match
- 🔍 REST endpoint for testing (
/match) - 🧱 Clean layered architecture
- 📜 Detailed logging for debugging and tracing
- Java 17
- Spring Boot 3.x
- Redis (Docker)
- Lombok
- Jakarta Validation
com.stock.matching_engine_service
│
├── controller # REST APIs (testing)
├── service # Business logic
│ └── impl
├── entity # Trade entity
├── dto # OrderEventDto, TradeResponseDto
├── util # Comparators (Buy/Sell)
├── enums # OrderType
├── config # Redis configuration
└── MatchingEngineServiceApplication.java
git clone <your-repo-url>
cd matching-engine-service
docker run --name stock-redis \
-p 6379:6379 \
-d redis
server:
port: 8084
spring:
application:
name: matching-engine-service
redis:
host: localhost
port: 6379
logging:
level:
root: INFO
com.stock.matching_engine_service: DEBUG
mvn spring-boot:run
POST /api/v1/matching-engine/match
{
"orderId": 1,
"stockSymbol": "INFY",
"quantity": 10,
"price": 1500,
"orderType": "BUY"
}
order-events
trade-events
PUBLISH order-events "{\"orderId\":1,\"stockSymbol\":\"INFY\",\"quantity\":10,\"price\":1500,\"orderType\":\"BUY\"}"
PUBLISH order-events "{\"orderId\":2,\"stockSymbol\":\"INFY\",\"quantity\":10,\"price\":1400,\"orderType\":\"SELL\"}"
- Orders are received by Matching Engine
- Orders are added to order book
- Matching logic executes trade
- Trade event is published
- Sorted by highest price first
- FIFO for same price
- Sorted by lowest price first
- FIFO for same price
BUY price >= SELL price
- Execute minimum of buy & sell quantity
- Reduce quantities accordingly
- Remove fully executed orders
- Generate trade
POST http://localhost:8084/api/v1/matching-engine/match
docker exec -it stock-redis redis-cli
PUBLISH order-events "{\"orderId\":1,\"stockSymbol\":\"INFY\",\"quantity\":10,\"price\":1500,\"orderType\":\"BUY\"}"
Expected logs:
Received order from Redis
Processing order
Trade executed
- In-memory matching engine (no DB dependency)
- PriorityQueue for order book
- Comparator-based sorting (price-time priority)
- FIFO using timestamp
- Event-driven architecture using Redis Pub/Sub
- Clean separation of layers
- Logging using SLF4J
- Redis runs on port 6379
- Ensure container is running before starting service
- Thread-safe order book (ConcurrentHashMap, locks)
- WebSocket integration for real-time updates
- Persistent trade storage (MySQL)
- Kafka for scalable messaging
- Performance optimization (multi-threaded matching)
- Circuit breaker for fault tolerance
Vipul Singh