How mrkrinetic/matchforge works
Purpose and Runtime
A Java 21 / Spring Boot 3 real-time game matchmaking service. It accepts player queue requests, asynchronously pairs compatible players, and delivers match notifications over WebSockets. The application starts at src/main/java/com/matchmaker/MatchmakingEngineApplication.java; the bundled browser client is src/main/resources/static/index.html.
Ingress: REST Queue Join
PlayerController exposes the queue-join API (POST /api/queue/join) and delegates publishing to PlayerService. Player requests carry identity and matching attributes such as MMR, region, mode, and ping, represented by Player.
• src/main/java/com/matchmaker/player/PlayerController.java
• src/main/java/com/matchmaker/player/PlayerService.java
• src/main/java/com/matchmaker/player/Player.java
Durable Queue Boundary: Kafka
Queue joins are serialized as JSON and published to Kafka topic player-matchmaking-queue. Kafka decouples request handling from Redis queue mutation and provides the durable asynchronous ingress boundary. Producer/consumer setup is centralized in:
• src/main/java/com/matchmaker/config/KafkaConfig.java
• src/main/java/com/matchmaker/matchmaking/MatchmakingKafkaConsumer.java
• src/main/resources/application.yml
Queue Materialization: Kafka to Redis
MatchmakingKafkaConsumer consumes player events and writes player state into Redis. Redis is the live matchmaking store: per-mode/per-region sorted sets (queue:{mode}:{region}), ordered by join time, plus player JSON lookup records. This boundary allows workers to operate on current queue state rather than Kafka records.
• src/main/java/com/matchmaker/matchmaking/MatchmakingKafkaConsumer.java
• src/main/java/com/matchmaker/config/RedisConfig.java
• src/main/java/com/matchmaker/util/JsonUtil.java
Matching Scheduler and Domain Rules
MatchmakingWorker runs on a configurable schedule (default 3 seconds), scans configured region/mode queues, and asks MatchmakingService to validate and form pairs. Compatibility requires identical region, both pings within the limit, and an MMR tolerance that expands with queue wait time. A successful match removes players from the queue and persists match-related Redis records.
• src/main/java/com/matchmaker/matchmaking/MatchmakingWorker.java
• src/main/java/com/matchmaker/matchmaking/MatchmakingService.java
• src/main/java/com/matchmaker/matchmaking/Match.java
Event Fan-out: Redis Pub/Sub to WebSocket
After a match is formed, the service publishes a match event to Redis Pub/Sub channel matchmaking:events. MatchEventListener receives it and routes JSON to each matched player’s active WebSocket session. Redis Pub/Sub is transient notification transport; Redis match records remain the durable-in-memory result state.
• src/main/java/com/matchmaker/notification/MatchEventListener.java
• src/main/java/com/matchmaker/config/RedisConfig.java
• src/main/java/com/matchmaker/matchmaking/MatchmakingService.java
Client Session Boundary
A WebSocket endpoint at /ws?player_id=X binds a connection to a player ID. WebSocketHandler manages connection lifecycle, while WebSocketSessionManager keeps the process-local player-to-session registry used by notification delivery. Consequently, WebSocket routing is instance-local and would need shared session routing for multi-instance deployment.
• src/main/java/com/matchmaker/config/WebSocketConfig.java
• src/main/java/com/matchmaker/notification/WebSocketHandler.java
• src/main/java/com/matchmaker/notification/WebSocketSessionManager.java
Infrastructure
docker-compose.yml provisions Kafka and Redis for local operation. Spring configuration supplies broker/Redis endpoints plus supported modes, regions, worker cadence, and matching thresholds.
• docker-compose.yml
• src/main/resources/application.yml
• pom.xml
Main entry points: MatchmakingService.java MatchmakingService · PlayerController.java PlayerController · RedisConfig.java Redis matchmaking state · index.html Browser client
Components 17
Client & Ingress
- Browser client
src/main/resources/static/index.html - PlayerController · Handles POST /api/queue/join.
src/main/java/com/matchmaker/player/PlayerController.java - PlayerService
src/main/java/com/matchmaker/player/PlayerService.java - Player · Carries identity, MMR, region, mode, and ping.
src/main/java/com/matchmaker/player/Player.java
Async Queue Pipeline
- Kafka queue topic · Topic: player-matchmaking-queue.
src/main/java/com/matchmaker/config/KafkaConfig.java - MatchmakingKafkaConsumer
src/main/java/com/matchmaker/matchmaking/MatchmakingKafkaConsumer.java - Redis matchmaking state · Stores per-mode/region queues, players, and match records.
src/main/java/com/matchmaker/config/RedisConfig.java
Match Formation
- MatchmakingWorker · Scans queues on the configured cadence.
src/main/java/com/matchmaker/matchmaking/MatchmakingWorker.java - MatchmakingService · Expands MMR tolerance by queue wait time and validates region and ping.
src/main/java/com/matchmaker/matchmaking/MatchmakingService.java - Match
src/main/java/com/matchmaker/matchmaking/Match.java
Notification Delivery
- Redis Pub/Sub events · Channel: matchmaking:events.
src/main/java/com/matchmaker/config/RedisConfig.java - MatchEventListener
src/main/java/com/matchmaker/notification/MatchEventListener.java - WebSocketHandler · Accepts /ws?player_id=X connections.
src/main/java/com/matchmaker/notification/WebSocketHandler.java - WebSocketSessionManager · Player-to-session routing is process-local.
src/main/java/com/matchmaker/notification/WebSocketSessionManager.java
Other components
- Spring Boot application
src/main/java/com/matchmaker/MatchmakingEngineApplication.java - Local infrastructure · Provisions Kafka and Redis.
docker-compose.yml - Runtime configuration · Defines endpoints, queues, worker cadence, modes, regions, and thresholds.
src/main/resources/application.yml
Connections 21
- Browser client → PlayerController: POST queue join
- Browser client → WebSocketHandler: WebSocket connection
- PlayerController → PlayerService: delegates
- PlayerController → Player: binds request
- PlayerService → Kafka queue topic: publishes JSON player
- Kafka queue topic → MatchmakingKafkaConsumer: consumes
- MatchmakingKafkaConsumer → Redis matchmaking state: materializes queue state
- MatchmakingWorker → Redis matchmaking state: scans live queues
- MatchmakingWorker → MatchmakingService: requests pairing
- MatchmakingService → Redis matchmaking state: removes players and stores match
- MatchmakingService → Match: forms
- MatchmakingService → Redis Pub/Sub events: publishes match event
- Redis Pub/Sub events → MatchEventListener: delivers event
- MatchEventListener → WebSocketSessionManager: looks up player sessions
- WebSocketHandler → WebSocketSessionManager: registers sessions
- WebSocketSessionManager → Browser client: sends match notification
- Spring Boot application → PlayerController: hosts
- Spring Boot application → MatchmakingWorker: runs scheduler
- Local infrastructure → Kafka queue topic: provisions
- Local infrastructure → Redis matchmaking state: provisions
- Runtime configuration → Spring Boot application: configures