# moband/minyurl architecture

> Architecture diagram of the GitHub repository moband/minyurl, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-16). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/moband/minyurl
- Repository: https://github.com/moband/minyurl

## Overview

##### Project Type and Purpose
- MinyUrl is a containerized, microservice‐style URL shortener service.
- It exposes a REST API, internally structured in layers (Controller → Service → Repository), uses Redis for caching, MongoDB for durable storage, and NGINX for load‐balancing.

##### Top‐Level System Components
a. HTTP Clients – consumer of the API (e.g., curl, browser, other microservices)
b. NGINX Load Balancer – routes incoming HTTP(S) to the MinyUrlService containers
c. MinyUrlService – Spring Boot application packaged in Docker
- Controller Layer (ShortUrlController)
- Service Layer (ShortUrlServiceImpl, WorkerStatusServiceImpl, etc.)
- Repository Layer (Spring Data MongoDB repositories)
- Cross‐cutting Aspects (LoggingAspect, Retry, DuplicateKeyExceptionAspect)
- Utility modules (Base58 encoder, ResponseUtil, GlobalConstants)
- Configuration (CacheConfig, AspectsConfig)
d. Redis Cache – stores recently accessed short→long mappings and/or counters
e. MongoDB – stores persistent documents: ShortUrl, WorkerStatus, PartitionStatus, embedded Stats

##### Interactions and Data Flows
- “Shortify” Request:
1. Client → NGINX → one of MinyUrlService instances
2. Controller receives JSON NewLinkDto → invokes Service
3. Service checks for key conflicts (via DuplicateKeyAspect), generates Base58 code, persists ShortUrl document in MongoDB, optionally primes Redis cache
4. Controller returns  JSON BaseResponse with the short code
- “Resolve” Request:
1. Client → NGINX → MinyUrlService
2. Controller maps path variable (KEY) → Service
3. Service queries Redis cache (CacheConfig).
- Hit: return URL, increment in‐memory counter or Stats.
- Miss: fetch from Mongo, update cache, update stats in Mongo, return URL
4. NGINX or application performs HTTP redirect to the long URL
- “Stats” Request:
1. Client → NGINX → MinyUrlService
2. Controller returns aggregated MonthlyVisitsReport and embedded BrowserStats, OsStats, DateStat from Mongo

##### Architectural Patterns & Principles
- Layered Architecture (Controller → Service → Repository)
- Aspect‐Oriented Programming for cross‐cutting concerns (logging, retry, exception handling)
- Cache‐Aside pattern (Redis sits alongside MongoDB)
- Dockerized microservice with externalized configuration (application.yml)
- Horizontal scalability (multiple MinyUrlService containers behind NGINX LB)

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% External Clients
    Clients["HTTP Clients"]:::external

    %% Load Balancer
    NGINX["NGINX Load Balancer\n(80/443)"]:::lb
    click NGINX "https://github.com/moband/minyurl/blob/master/nginx/nginx.conf"
    click NGINX "https://github.com/moband/minyurl/tree/master/nginx/Dockerfile"

    %% Service Cluster
    subgraph "MinyUrlService Cluster" 
        direction TB
        SVC1["MinyUrlService Instance\n(Spring Boot)"]:::service
        SVC2["MinyUrlService Instance\n(Spring Boot)"]:::service
        click SVC1 "https://github.com/moband/minyurl/tree/master/MinyUrlService/Dockerfile"
        click SVC1 "https://github.com/moband/minyurl/blob/master/MinyUrlService/src/main/java/com/neueda/kgs/Application.java"
    end

    %% Data Stores
    RedisCache["Redis Cache\n(6379)"]:::cache
    click RedisCache "https://github.com/moband/minyurl/blob/master/MinyUrlService/src/main/java/com/neueda/kgs/config/CacheConfig.java"
    MongoDB["MongoDB\n(27017)"]:::db
    click MongoDB "https://github.com/moband/minyurl/blob/master/MinyUrlService/pom.xml"

    %% Docker Compose
    DockerCompose["Docker Compose Orchestration"]:::orchestrator
    click DockerCompose "https://github.com/moband/minyurl/blob/master/docker-compose.yml"

    %% Connections External
    Clients -->|HTTPS| NGINX
    NGINX --> SVC1
    NGINX --> SVC2

    %% Connections Data
    SVC1 <--> RedisCache
    SVC2 <--> RedisCache
    SVC1 <--> MongoDB
    SVC2 <--> MongoDB

    %% Orchestration Bound
    DockerCompose --> NGINX
    DockerCompose --> RedisCache
    DockerCompose --> MongoDB
    DockerCompose --> SVC1
    DockerCompose --> SVC2

    %% Internal View for one instance
    subgraph "MinyUrlService Instance Internal" 
        direction TB

        subgraph "Controller Layer" 
            direction TB
            ShortUrlController["ShortUrlController"]:::module
            click ShortUrlController "https://github.com/moband/minyurl/blob/master/MinyUrlService/src/main/java/com/neueda/kgs/controller/ShortUrlController.java"
            DTOs["DTOs\n(BaseResponse, NewLinkDto,\nResolveLinkDto, MonthlyVisitsReport,\nVisitStateDto)"]:::module
            click DTOs "https://github.com/moband/minyurl/blob/master/MinyUrlService/src/main/java/com/neueda/kgs/controller/dto/BaseResponse.java"
            click DTOs "https://github.com/moband/minyurl/blob/master/MinyUrlService/src/main/java/com/neueda/kgs/controller/dto/NewLinkDto.java"
            click DTOs "https://github.com/moband/minyurl/blob/master/MinyUrlService/src/main/java/com/neueda/kgs/controller/dto/ResolveLinkDto.java"
            click DTOs "https://github.com/moband/minyurl/blob/master/MinyUrlService/src/main/java/com/neueda/kgs/controller/dto/MonthlyVisitsReport.java"
            click DTOs "https://github.com/moband/minyurl/blob/master/MinyUrlService/src/main/java/com/neueda/kgs/controller/dto/VisitStateDto.java"
        end

        subgraph "Service Layer" 
            direction TB
            ShortUrlServiceImpl["ShortUrlServiceImpl"]:::module
            click ShortUrlServiceImpl "https://github.com/moband/minyurl/blob/master/MinyUrlService/src/main/java/com/neueda/kgs/service/impl/ShortUrlServiceImpl.java"
            WorkerStatusServiceImpl["WorkerStatusServiceImpl"]:::module
            click WorkerStatusServiceImpl "https://github.com/moband/minyurl/blob/master/MinyUrlService/src/main/java/com/neueda/kgs/service/impl/WorkerStatusServiceImpl.java"
            ARPStatusServiceImpl["AllocatedRangePartitionStatusServiceImpl"]:::module
            click ARPStatusServiceImpl "https://github.com/moband/minyurl/blob/master/MinyUrlService/src/main/java/com/neueda/kgs/service/impl/AllocatedRangePartitionStatusServiceImpl.java"
        end

        subgraph "Repository Layer" 
            direction TB
            ShortUrlRepo["ShortUrlRepository"]:::module
            click ShortUrlRepo "https://github.com/moband/minyurl/blob/master/MinyUrlService/src/main/java/com/neueda/kgs/repository/ShortUrlRepository.java"
            WorkerStatusRepo["WorkerStatusRepository"]:::module
            click WorkerStatusRepo "https://github.com/moband/minyurl/blob/master/MinyUrlService/src/main/java/com/neueda/kgs/repository/WorkerStatusRepository.java"
            ARPStatusRepo["AllocatedRangePartitionStatusRepository"]:::module
            click ARPStatusRepo "https://github.com/moband/minyurl/blob/master/MinyUrlService/src/main/java/com/neueda/kgs/repository/AllocatedRangePartitionStatusRepository.java"
        end

        subgraph "Cross-cutting Aspects" 
            direction TB
            LoggingAspect["LoggingAspect"]:::aspect
            click LoggingAspect "https://github.com/moband/minyurl/blob/master/MinyUrlService/src/main/java/com/neueda/kgs/aspect/LoggingAspect.java"
            DuplicateKeyAspect["DuplicateKeyExceptionAspect"]:::aspect
            click DuplicateKeyAspect "https://github.com/moband/minyurl/blob/master/MinyUrlService/src/main/java/com/neueda/kgs/aspect/DuplicateKeyExceptionAspect.java"
            RetryAspect["RetryAspect"]:::aspect
            click RetryAspect "https://github.com/moband/minyurl/blob/master/MinyUrlService/src/main/java/com/neueda/kgs/aspect/Retry.java"
        end

        subgraph "Configuration Modules" 
            direction TB
            CacheConfig["CacheConfig"]:::module
            click CacheConfig "https://github.com/moband/minyurl/blob/master/MinyUrlService/src/main/java/com/neueda/kgs/config/CacheConfig.java"
            AspectsConfig["AspectsConfig"]:::module
            click AspectsConfig "https://github.com/moband/minyurl/blob/master/MinyUrlService/src/main/java/com/neueda/kgs/config/AspectsConfig.java"
        end

        subgraph "Utility Components" 
            direction TB
            Base58["Base58"]:::module
            click Base58 "https://github.com/moband/minyurl/blob/master/MinyUrlService/src/main/java/com/neueda/kgs/util/Base58.java"
            GlobalConstants["GlobalConstants"]:::module
            click GlobalConstants "https://github.com/moband/minyurl/blob/master/MinyUrlService/src/main/java/com/neueda/kgs/util/GlobalConstants.java"
            ResponseUtil["ResponseUtil"]:::module
            click ResponseUtil "https://github.com/moband/minyurl/blob/master/MinyUrlService/src/main/java/com/neueda/kgs/util/ResponseUtil.java"
            Utility["Utility"]:::module
            click Utility "https://github.com/moband/minyurl/blob/master/MinyUrlService/src/main/java/com/neueda/kgs/util/Utility.java"
        end

        %% Internal Connections
        ShortUrlController -->|invokes| ShortUrlServiceImpl
        ShortUrlServiceImpl -->|calls| ShortUrlRepo
        ShortUrlServiceImpl <-->|cache-aside| RedisCache
        ShortUrlRepo -->|persists| MongoDB

    end

    %% Styles
    classDef external fill:#FFA500,stroke:#333,stroke-width:1px
    classDef lb fill:#ADD8E6,stroke:#333,stroke-width:1px
    classDef service fill:#90EE90,stroke:#333,stroke-width:1px
    classDef module fill:#FFFF99,stroke:#333,stroke-width:1px
    classDef aspect fill:#D3D3D3,stroke:#333,stroke-width:1px
    classDef db fill:#FF6347,stroke:#333,stroke-width:1px
    classDef cache fill:#87CEFA,stroke:#333,stroke-width:1px
    classDef orchestrator fill:#DDA0DD,stroke:#333,stroke-width:1px
```
