# pneum4/megaarticle architecture

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

- Interactive diagram: https://gitdiagram.com/pneum4/megaarticle
- Repository: https://github.com/pneum4/megaarticle

## Overview

1. This is a Gradle multi-module Java/Spring Boot repository organized as a microservice system for a high-traffic bulletin board. The codebase is split into shared libraries under `common/` and independently deployable services under `service/`, plus a separate `swagger-gateway` module for API documentation aggregation.

2. The architecture is strongly write/read separated. The write-side services own the authoritative data for articles, comments, likes, and views. The read-side services (`article-read` and `hot-article`) consume domain events and maintain optimized projections for fast querying.

3. Shared foundation modules provide the cross-service contracts and infrastructure:
   - `common/event` defines the event envelope, event types, and payloads for article/comment lifecycle, like/unlike, and view events.
   - `common/data-serializer` centralizes serialization of event/data payloads.
   - `common/snowflake` provides distributed ID generation.
   - `common/outbox-message-relay` implements the outbox relay pattern, with relay, coordinator, repository, and publisher components for reliable event publication.

4. `service/article` is the article write model. It exposes `ArticleController`, persists `Article` and `BoardArticleCount`, and contains request/response DTOs plus pagination logic. This service is the source of truth for article CRUD and article-count updates.

5. `service/comment` manages threaded comments and supports two versions of the comment model (`Comment`/`CommentV2`) with `CommentPath` for deep nesting. It has separate controllers and services for the two versions, plus an `ArticleCommentCount` projection to track comment totals.

6. `service/like` owns article likes. It stores individual like records and aggregated `ArticleLikeCount`, exposing a dedicated controller/service for like and unlike actions. Like state is maintained here rather than embedded in the article service.

7. `service/view` is the view-counting subsystem. It includes a distributed-lock repository and a backup repository, which indicates contention-sensitive counting with a persistence fallback/repair path. `ArticleViewCountBackUpProcessor` suggests asynchronous reconciliation of view counters after transient failures or heavy write load.

8. `service/article-read` is the primary read-optimized API. It has its own controller, query repositories, and a service layer that assembles article read responses. It also calls out to `ArticleClient`, `CommentClient`, `LikeClient`, and `ViewClient`, so the read model is enriched from multiple source services rather than queried from a single monolith.

9. The article-read service also contains a custom cache subsystem (`OptimizedCache`, aspect, TTL, lock provider, manager, origin supplier), which is designed to reduce repeated read amplification and prevent cache stampedes on hot endpoints. This is a key performance boundary in the architecture.

10. `service/hot-article` is a separate ranking/read model for “hot” content. It consumes article/comment/like/view events, calculates scores with `HotArticleScoreCalculator`, updates rankings via `HotArticleScoreUpdater`, and serves them through `HotArticleController`. The supporting repositories track article creation time and per-signal counts needed for scoring.

11. Event propagation is the main integration path between services. Write-side services emit domain events through the shared event model and outbox relay; Kafka is the asynchronous backbone used by consumer modules in `article-read` and `hot-article`. This gives eventual consistency between the authoritative write stores and the precomputed read stores.

12. `swagger-gateway` is not a business gateway in the request path; it is a documentation/Swagger aggregation module. Each service also has its own `SwaggerConfig`, which reinforces the independently deployable-service boundary.

13. The main infrastructure technologies that materially shape the architecture are Spring Boot, Kafka, Redis, and MySQL. Kafka carries cross-service events, Redis is used where low-latency coordination or buffering matters, and the services are designed to run separately, likely via Docker as noted in the README.

14. For an architecture diagram, the most important boundaries are: write services as system-of-record, common libraries as shared contracts/infrastructure, Kafka as the async event bus, and read/ranking services as event-driven projections optimized for query and aggregation.

Main entry points: [`service/article-read`](https://github.com/pneum4/megaarticle/tree/main/service/article-read) (Article read), [`swagger-gateway`](https://github.com/pneum4/megaarticle/tree/main/swagger-gateway) (Swagger gateway), [`service/article`](https://github.com/pneum4/megaarticle/tree/main/service/article) (Article svc), [`common/event`](https://github.com/pneum4/megaarticle/tree/main/common/event) (Event model)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_group_common["Common libs"]
  node_node_event["Event model<br/>shared contract"]
  node_node_serializer["Serializer<br/>shared infra"]
  node_node_snowflake["Snowflake<br/>id generator"]
  node_node_outbox["Outbox relay<br/>event delivery"]
end

subgraph group_group_write["Write services"]
  node_node_article["Article svc<br/>write service"]
  node_node_article_ctrl["Article API<br/>controller"]
  node_node_comment["Comment svc<br/>write service"]
  node_node_like["Like svc<br/>write service"]
  node_node_view["View svc<br/>write service"]
end

subgraph group_group_read["Read models"]
  node_node_read["Article read<br/>read service"]
  node_node_read_cache["Read cache<br/>cache subsystem"]
  node_node_read_consumer["Event consumer<br/>kafka consumer"]
  node_node_read_repo[("Read repos<br/>projection store")]
  node_node_hot["Hot article<br/>ranking service"]
  node_node_hot_score["Score engine<br/>ranking logic"]
  node_node_hot_consumer["Rank consumer<br/>kafka consumer"]
  node_node_hot_repo[("Hot repos<br/>projection store")]
end

subgraph group_group_docs["Docs"]
  node_node_swagger["Swagger gateway<br/>docs module"]
end

node_node_article -->|"publishes"| node_node_event
node_node_comment -->|"publishes"| node_node_event
node_node_like -->|"publishes"| node_node_event
node_node_view -->|"publishes"| node_node_event
node_node_article -->|"relays via"| node_node_outbox
node_node_comment -->|"relays via"| node_node_outbox
node_node_like -->|"relays via"| node_node_outbox
node_node_view -->|"relays via"| node_node_outbox
node_node_article -->|"ids"| node_node_snowflake
node_node_comment -->|"ids"| node_node_snowflake
node_node_like -->|"ids"| node_node_snowflake
node_node_view -->|"ids"| node_node_snowflake
node_node_read -->|"uses"| node_node_read_cache
node_node_read -->|"queries"| node_node_read_repo
node_node_read -->|"updates from"| node_node_read_consumer
node_node_read -->|"fetches from"| node_node_article
node_node_read -->|"fetches from"| node_node_comment
node_node_read -->|"fetches from"| node_node_like
node_node_read -->|"fetches from"| node_node_view
node_node_hot -->|"updates from"| node_node_hot_consumer
node_node_hot -->|"calculates"| node_node_hot_score
node_node_hot -->|"persists"| node_node_hot_repo
node_node_hot -->|"enriches from"| node_node_article
node_node_hot_consumer -->|"consumes"| node_node_event
node_node_read_consumer -->|"consumes"| node_node_event
node_node_swagger -->|"documents"| node_node_article
node_node_swagger -->|"documents"| node_node_comment
node_node_swagger -->|"documents"| node_node_like
node_node_swagger -->|"documents"| node_node_view
node_node_swagger -->|"documents"| node_node_read
node_node_swagger -->|"documents"| node_node_hot

click node_node_event "https://github.com/pneum4/megaarticle/tree/main/common/event"
click node_node_serializer "https://github.com/pneum4/megaarticle/tree/main/common/data-serializer"
click node_node_snowflake "https://github.com/pneum4/megaarticle/tree/main/common/snowflake"
click node_node_outbox "https://github.com/pneum4/megaarticle/tree/main/common/outbox-message-relay"
click node_node_article "https://github.com/pneum4/megaarticle/tree/main/service/article"
click node_node_article_ctrl "https://github.com/pneum4/megaarticle/blob/main/service/article/src/main/java/pneum4/board/article/controller/ArticleController.java"
click node_node_comment "https://github.com/pneum4/megaarticle/tree/main/service/comment"
click node_node_like "https://github.com/pneum4/megaarticle/tree/main/service/like"
click node_node_view "https://github.com/pneum4/megaarticle/tree/main/service/view"
click node_node_read "https://github.com/pneum4/megaarticle/tree/main/service/article-read"
click node_node_read_cache "https://github.com/pneum4/megaarticle/tree/main/service/article-read/src/main/java/pneum4/board/articleread/cache"
click node_node_read_consumer "https://github.com/pneum4/megaarticle/blob/main/service/article-read/src/main/java/pneum4/board/articleread/consumer/ArticleReadEventConsumer.java"
click node_node_read_repo "https://github.com/pneum4/megaarticle/tree/main/service/article-read/src/main/java/pneum4/board/articleread/repository"
click node_node_hot "https://github.com/pneum4/megaarticle/tree/main/service/hot-article"
click node_node_hot_score "https://github.com/pneum4/megaarticle/tree/main/service/hot-article/src/main/java/pneum4/board/hotarticle/service"
click node_node_hot_consumer "https://github.com/pneum4/megaarticle/blob/main/service/hot-article/src/main/java/pneum4/board/hotarticle/consumer/HotArticleEventConsumer.java"
click node_node_hot_repo "https://github.com/pneum4/megaarticle/tree/main/service/hot-article/src/main/java/pneum4/board/hotarticle/repository"
click node_node_swagger "https://github.com/pneum4/megaarticle/tree/main/swagger-gateway"

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_node_event,node_node_serializer,node_node_snowflake,node_node_outbox toneBlue
class node_node_article,node_node_article_ctrl,node_node_comment,node_node_like,node_node_view toneAmber
class node_node_read,node_node_read_cache,node_node_read_consumer,node_node_read_repo,node_node_hot,node_node_hot_score,node_node_hot_consumer,node_node_hot_repo toneMint
class node_node_swagger toneRose
```

## Components

### Common libs

Shared contracts and infrastructure used by services

- [Event model](https://github.com/pneum4/megaarticle/tree/main/common/event): shared contract · Event envelope, types, and payloads for article, comment, like, and view changes · `common/event`
- [Serializer](https://github.com/pneum4/megaarticle/tree/main/common/data-serializer): shared infra · Central payload serialization utilities · `common/data-serializer`
- [Snowflake](https://github.com/pneum4/megaarticle/tree/main/common/snowflake): id generator · Distributed ID generation for services · `common/snowflake`
- [Outbox relay](https://github.com/pneum4/megaarticle/tree/main/common/outbox-message-relay): event delivery · Outbox relay, coordinator, repository, and publisher for reliable async publication · `common/outbox-message-relay`

### Write services

System-of-record services for mutations and authoritative state

- [Article svc](https://github.com/pneum4/megaarticle/tree/main/service/article): write service · Authoritative article CRUD and board article counts · `service/article`
- [Article API](https://github.com/pneum4/megaarticle/blob/main/service/article/src/main/java/pneum4/board/article/controller/ArticleController.java): controller · ArticleController entrypoint for article mutations and queries · `service/article/src/main/java/pneum4/board/article/controller/ArticleController.java`
- [Comment svc](https://github.com/pneum4/megaarticle/tree/main/service/comment): write service · Threaded comments with v1 and v2 models plus comment totals · `service/comment`
- [Like svc](https://github.com/pneum4/megaarticle/tree/main/service/like): write service · Article like/unlike ownership and aggregated like counts · `service/like`
- [View svc](https://github.com/pneum4/megaarticle/tree/main/service/view): write service · View counting with distributed locking and backup reconciliation · `service/view`

### Read models

Event-driven projections and query-optimized APIs

- [Article read](https://github.com/pneum4/megaarticle/tree/main/service/article-read): read service · Read-optimized article API that assembles responses from multiple source services · `service/article-read`
- [Read cache](https://github.com/pneum4/megaarticle/tree/main/service/article-read/src/main/java/pneum4/board/articleread/cache): cache subsystem · Optimized cache, lock provider, TTL, and origin supplier to prevent stampedes · `service/article-read/src/main/java/pneum4/board/articleread/cache`
- [Event consumer](https://github.com/pneum4/megaarticle/blob/main/service/article-read/src/main/java/pneum4/board/articleread/consumer/ArticleReadEventConsumer.java): kafka consumer · Consumes article and comment events to update read projections · `service/article-read/src/main/java/pneum4/board/articleread/consumer/ArticleReadEventConsumer.java`
- [Read repos](https://github.com/pneum4/megaarticle/tree/main/service/article-read/src/main/java/pneum4/board/articleread/repository): projection store · Query models and repositories for article lists and board counts · `service/article-read/src/main/java/pneum4/board/articleread/repository`
- [Hot article](https://github.com/pneum4/megaarticle/tree/main/service/hot-article): ranking service · Event-driven hot content ranking and serving · `service/hot-article`
- [Score engine](https://github.com/pneum4/megaarticle/tree/main/service/hot-article/src/main/java/pneum4/board/hotarticle/service): ranking logic · Hot article score calculation and projection updates · `service/hot-article/src/main/java/pneum4/board/hotarticle/service`
- [Rank consumer](https://github.com/pneum4/megaarticle/blob/main/service/hot-article/src/main/java/pneum4/board/hotarticle/consumer/HotArticleEventConsumer.java): kafka consumer · Consumes article, comment, like, and view events to refresh hot rankings · `service/hot-article/src/main/java/pneum4/board/hotarticle/consumer/HotArticleEventConsumer.java`
- [Hot repos](https://github.com/pneum4/megaarticle/tree/main/service/hot-article/src/main/java/pneum4/board/hotarticle/repository): projection store · Ranking state, signal counts, and article creation time repositories · `service/hot-article/src/main/java/pneum4/board/hotarticle/repository`

### Docs

Swagger aggregation and API documentation

- [Swagger gateway](https://github.com/pneum4/megaarticle/tree/main/swagger-gateway): docs module · Aggregates service API docs without being in the business request path · `swagger-gateway`

## Connections

- Article svc → Event model: publishes · Article lifecycle changes are emitted as shared domain events
- Comment svc → Event model: publishes · Comment lifecycle changes are emitted as shared domain events
- Like svc → Event model: publishes · Like and unlike actions are emitted as shared domain events
- View svc → Event model: publishes · View events are emitted for downstream projections
- Article svc → Outbox relay: relays via · Write-side services use the outbox relay for reliable publication
- Comment svc → Outbox relay: relays via · Comment events are dispatched through the shared outbox pattern
- Like svc → Outbox relay: relays via · Like events are dispatched through the shared outbox pattern
- View svc → Outbox relay: relays via · View events are dispatched through the shared outbox pattern
- Article svc → Snowflake: ids · Distributed IDs support write-side entities
- Comment svc → Snowflake: ids · Distributed IDs support comment entities
- Like svc → Snowflake: ids · Distributed IDs support like entities
- View svc → Snowflake: ids · Distributed IDs support view entities
- Article read → Read cache: uses · Read API depends on the optimized cache boundary
- Article read → Read repos: queries · Read API assembles responses from projection repositories
- Article read → Event consumer: updates from · Event consumer keeps read projections current
- Article read → Article svc: fetches from · Read responses are enriched from article source service
- Article read → Comment svc: fetches from · Read responses are enriched from comment source service
- Article read → Like svc: fetches from · Read responses are enriched from like source service
- Article read → View svc: fetches from · Read responses are enriched from view source service
- Hot article → Rank consumer: updates from · Hot rankings are driven by Kafka event consumption
- Hot article → Score engine: calculates · Score engine computes ranking signals and updates
- Hot article → Hot repos: persists · Ranking projections are stored in dedicated repositories
- Hot article → Article svc: enriches from · Hot article responses can reference article source data
- Rank consumer → Event model: consumes · Consumes shared lifecycle and signal events from the event stream
- Event consumer → Event model: consumes · Consumes shared lifecycle and signal events from the event stream
- Swagger gateway → Article svc: documents · Aggregates service API documentation
- Swagger gateway → Comment svc: documents · Aggregates service API documentation
- Swagger gateway → Like svc: documents · Aggregates service API documentation
- Swagger gateway → View svc: documents · Aggregates service API documentation
- Swagger gateway → Article read: documents · Aggregates service API documentation
- Swagger gateway → Hot article: documents · Aggregates service API documentation
