# mrkrinetic/matchforge architecture

> Architecture diagram of the GitHub repository mrkrinetic/matchforge, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-07-17). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/mrkrinetic/matchforge
- Repository: https://github.com/mrkrinetic/matchforge

## Overview

#### 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: [`src/main/java/com/matchmaker/matchmaking/MatchmakingService.java`](https://github.com/mrkrinetic/matchforge/blob/main/src/main/java/com/matchmaker/matchmaking/MatchmakingService.java) (MatchmakingService), [`src/main/java/com/matchmaker/player/PlayerController.java`](https://github.com/mrkrinetic/matchforge/blob/main/src/main/java/com/matchmaker/player/PlayerController.java) (PlayerController), [`src/main/java/com/matchmaker/config/RedisConfig.java`](https://github.com/mrkrinetic/matchforge/blob/main/src/main/java/com/matchmaker/config/RedisConfig.java) (Redis matchmaking state), [`src/main/resources/static/index.html`](https://github.com/mrkrinetic/matchforge/blob/main/src/main/resources/static/index.html) (Browser client)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_ingress["Client &amp; Ingress"]
  node_browser["Browser client<br/>static client<br/>[index.html]"]
  node_controller["PlayerController<br/>REST API"]
  node_player_service["PlayerService<br/>queue publisher<br/>[PlayerService.java]"]
  node_player["Player<br/>request model<br/>[Player.java]"]
end

subgraph group_queue["Async Queue Pipeline"]
  node_kafka["Kafka queue topic<br/>durable event bus<br/>[KafkaConfig.java]"]
  node_kafka_consumer["MatchmakingKafkaConsumer<br/>event consumer"]
  node_redis[("Redis matchmaking state<br/>queue and result store<br/>[RedisConfig.java]")]
end

subgraph group_matching["Match Formation"]
  node_worker["MatchmakingWorker<br/>scheduled worker"]
  node_matching_service["MatchmakingService<br/>matching rules"]
  node_match["Match<br/>domain model<br/>[Match.java]"]
end

subgraph group_delivery["Notification Delivery"]
  node_redis_events["Redis Pub/Sub events<br/>notification channel<br/>[RedisConfig.java]"]
  node_event_listener["MatchEventListener<br/>event router"]
  node_websocket_handler["WebSocketHandler<br/>connection endpoint"]
  node_session_manager["WebSocketSessionManager<br/>local session registry"]
end

node_application{{"Spring Boot application<br/>Java runtime"}}
node_infrastructure["Local infrastructure<br/>Docker Compose<br/>[docker-compose.yml]"]
node_runtime_config["Runtime configuration<br/>Spring config<br/>[application.yml]"]

node_browser -->|"POST queue join"| node_controller
node_browser -->|"WebSocket connection"| node_websocket_handler
node_controller -->|"delegates"| node_player_service
node_controller -->|"binds request"| node_player
node_player_service -->|"publishes JSON player"| node_kafka
node_kafka -->|"consumes"| node_kafka_consumer
node_kafka_consumer -->|"materializes queue state"| node_redis
node_worker -->|"scans live queues"| node_redis
node_worker -->|"requests pairing"| node_matching_service
node_matching_service -->|"removes players and stores match"| node_redis
node_matching_service -->|"forms"| node_match
node_matching_service -->|"publishes match event"| node_redis_events
node_redis_events -->|"delivers event"| node_event_listener
node_event_listener -->|"looks up player sessions"| node_session_manager
node_websocket_handler -->|"registers sessions"| node_session_manager
node_session_manager -->|"sends match notification"| node_browser
node_application -->|"hosts"| node_controller
node_application -->|"runs scheduler"| node_worker
node_infrastructure -.->|"provisions"| node_kafka
node_infrastructure -.->|"provisions"| node_redis
node_runtime_config -.->|"configures"| node_application

click node_browser "https://github.com/mrkrinetic/matchforge/blob/main/src/main/resources/static/index.html"
click node_controller "https://github.com/mrkrinetic/matchforge/blob/main/src/main/java/com/matchmaker/player/PlayerController.java"
click node_player_service "https://github.com/mrkrinetic/matchforge/blob/main/src/main/java/com/matchmaker/player/PlayerService.java"
click node_player "https://github.com/mrkrinetic/matchforge/blob/main/src/main/java/com/matchmaker/player/Player.java"
click node_kafka "https://github.com/mrkrinetic/matchforge/blob/main/src/main/java/com/matchmaker/config/KafkaConfig.java"
click node_kafka_consumer "https://github.com/mrkrinetic/matchforge/blob/main/src/main/java/com/matchmaker/matchmaking/MatchmakingKafkaConsumer.java"
click node_redis "https://github.com/mrkrinetic/matchforge/blob/main/src/main/java/com/matchmaker/config/RedisConfig.java"
click node_worker "https://github.com/mrkrinetic/matchforge/blob/main/src/main/java/com/matchmaker/matchmaking/MatchmakingWorker.java"
click node_matching_service "https://github.com/mrkrinetic/matchforge/blob/main/src/main/java/com/matchmaker/matchmaking/MatchmakingService.java"
click node_match "https://github.com/mrkrinetic/matchforge/blob/main/src/main/java/com/matchmaker/matchmaking/Match.java"
click node_redis_events "https://github.com/mrkrinetic/matchforge/blob/main/src/main/java/com/matchmaker/config/RedisConfig.java"
click node_event_listener "https://github.com/mrkrinetic/matchforge/blob/main/src/main/java/com/matchmaker/notification/MatchEventListener.java"
click node_websocket_handler "https://github.com/mrkrinetic/matchforge/blob/main/src/main/java/com/matchmaker/notification/WebSocketHandler.java"
click node_session_manager "https://github.com/mrkrinetic/matchforge/blob/main/src/main/java/com/matchmaker/notification/WebSocketSessionManager.java"
click node_application "https://github.com/mrkrinetic/matchforge/blob/main/src/main/java/com/matchmaker/MatchmakingEngineApplication.java"
click node_infrastructure "https://github.com/mrkrinetic/matchforge/blob/main/docker-compose.yml"
click node_runtime_config "https://github.com/mrkrinetic/matchforge/blob/main/src/main/resources/application.yml"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_browser,node_controller,node_player_service,node_player toneBlue
class node_kafka,node_kafka_consumer,node_redis toneAmber
class node_worker,node_matching_service,node_match toneMint
class node_redis_events,node_event_listener,node_websocket_handler,node_session_manager toneRose
class node_application,node_infrastructure,node_runtime_config toneNeutral
```

## Components

### Client & Ingress

- [Browser client](https://github.com/mrkrinetic/matchforge/blob/main/src/main/resources/static/index.html): static client · `src/main/resources/static/index.html`
- [PlayerController](https://github.com/mrkrinetic/matchforge/blob/main/src/main/java/com/matchmaker/player/PlayerController.java): REST API · Handles POST /api/queue/join. · `src/main/java/com/matchmaker/player/PlayerController.java`
- [PlayerService](https://github.com/mrkrinetic/matchforge/blob/main/src/main/java/com/matchmaker/player/PlayerService.java): queue publisher · `src/main/java/com/matchmaker/player/PlayerService.java`
- [Player](https://github.com/mrkrinetic/matchforge/blob/main/src/main/java/com/matchmaker/player/Player.java): request model · Carries identity, MMR, region, mode, and ping. · `src/main/java/com/matchmaker/player/Player.java`

### Async Queue Pipeline

- [Kafka queue topic](https://github.com/mrkrinetic/matchforge/blob/main/src/main/java/com/matchmaker/config/KafkaConfig.java): durable event bus · Topic: player-matchmaking-queue. · `src/main/java/com/matchmaker/config/KafkaConfig.java`
- [MatchmakingKafkaConsumer](https://github.com/mrkrinetic/matchforge/blob/main/src/main/java/com/matchmaker/matchmaking/MatchmakingKafkaConsumer.java): event consumer · `src/main/java/com/matchmaker/matchmaking/MatchmakingKafkaConsumer.java`
- [Redis matchmaking state](https://github.com/mrkrinetic/matchforge/blob/main/src/main/java/com/matchmaker/config/RedisConfig.java): queue and result store · Stores per-mode/region queues, players, and match records. · `src/main/java/com/matchmaker/config/RedisConfig.java`

### Match Formation

- [MatchmakingWorker](https://github.com/mrkrinetic/matchforge/blob/main/src/main/java/com/matchmaker/matchmaking/MatchmakingWorker.java): scheduled worker · Scans queues on the configured cadence. · `src/main/java/com/matchmaker/matchmaking/MatchmakingWorker.java`
- [MatchmakingService](https://github.com/mrkrinetic/matchforge/blob/main/src/main/java/com/matchmaker/matchmaking/MatchmakingService.java): matching rules · Expands MMR tolerance by queue wait time and validates region and ping. · `src/main/java/com/matchmaker/matchmaking/MatchmakingService.java`
- [Match](https://github.com/mrkrinetic/matchforge/blob/main/src/main/java/com/matchmaker/matchmaking/Match.java): domain model · `src/main/java/com/matchmaker/matchmaking/Match.java`

### Notification Delivery

- [Redis Pub/Sub events](https://github.com/mrkrinetic/matchforge/blob/main/src/main/java/com/matchmaker/config/RedisConfig.java): notification channel · Channel: matchmaking:events. · `src/main/java/com/matchmaker/config/RedisConfig.java`
- [MatchEventListener](https://github.com/mrkrinetic/matchforge/blob/main/src/main/java/com/matchmaker/notification/MatchEventListener.java): event router · `src/main/java/com/matchmaker/notification/MatchEventListener.java`
- [WebSocketHandler](https://github.com/mrkrinetic/matchforge/blob/main/src/main/java/com/matchmaker/notification/WebSocketHandler.java): connection endpoint · Accepts /ws?player_id=X connections. · `src/main/java/com/matchmaker/notification/WebSocketHandler.java`
- [WebSocketSessionManager](https://github.com/mrkrinetic/matchforge/blob/main/src/main/java/com/matchmaker/notification/WebSocketSessionManager.java): local session registry · Player-to-session routing is process-local. · `src/main/java/com/matchmaker/notification/WebSocketSessionManager.java`

### Other components

- [Spring Boot application](https://github.com/mrkrinetic/matchforge/blob/main/src/main/java/com/matchmaker/MatchmakingEngineApplication.java): Java runtime · `src/main/java/com/matchmaker/MatchmakingEngineApplication.java`
- [Local infrastructure](https://github.com/mrkrinetic/matchforge/blob/main/docker-compose.yml): Docker Compose · Provisions Kafka and Redis. · `docker-compose.yml`
- [Runtime configuration](https://github.com/mrkrinetic/matchforge/blob/main/src/main/resources/application.yml): Spring config · Defines endpoints, queues, worker cadence, modes, regions, and thresholds. · `src/main/resources/application.yml`

## Connections

- 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
