# minhhungit/mongodb-cluster-docker-compose architecture

> Architecture diagram of the GitHub repository minhhungit/mongodb-cluster-docker-compose, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-02). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/minhhungit/mongodb-cluster-docker-compose
- Repository: https://github.com/minhhungit/mongodb-cluster-docker-compose

## Overview

##### Main Components
- Config Servers: Represent a replica set of three config servers (configsvr01, configsvr02, configsvr03). These are critical to the sharded cluster configuration.
- Routers (mongos): Include two mongos router nodes (router01 and router02) that act as the query routers between client applications and shards.
- Client Interaction: Optionally include a client layer (as seen in the “client” directory) which represents sample .NET applications (DemoMongoClusterReader, DemoMongoClusterWriter) that connect to the cluster.
- Automated Initialization Scripts: Indicate the presence of special initialization scripts (found throughout the “scripts”, “PSA/scripts”, “minimize/scripts”, and “with-keyfile-auth/scripts” directories) that automatically configure and initialize the cluster using Docker Compose.

##### Architectural Patterns and Design Principles
- Containerized architecture: Emphasize how Docker Compose is used to launch and interconnect multiple containers.
- Replica Sets and Sharding: Represent the separation of responsibilities between config servers (cluster metadata), shard replica sets (data storage/redundancy), and routers (query routing).
- Modular Configuration: Note that there are multiple variants (e.g., PSA, minimize, with-keyfile-auth) that might imply slight variations in configuration; however, the core architecture remains consistent.

##### Technologies and Frameworks
- MongoDB: Used as the database engine with replication and sharding.
- Docker and Docker Compose: For container orchestration. Display how different docker-compose files are used to launch various cluster variants.
- .NET (Client folder): Represented by sample projects for MongoDB read/write operations.
- Scripting (JavaScript initialization files): Indicate their role for automating config server, router, and shard replica set initialization.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Client Layer
    subgraph "Client Applications"
        C1["DemoMongoClusterReader"]:::client
        C2["DemoMongoClusterWriter"]:::client
        C3["MongoCluster.Messages"]:::client
    end

    %% Router Layer
    subgraph "Mongos Routers"
        R1["Router01"]:::router
        R2["Router02"]:::router
    end

    %% Data Layer - Config Server Replica Set
    subgraph "Config Server Replica Set"
        CS1["configsvr01"]:::data
        CS2["configsvr02"]:::data
        CS3["configsvr03"]:::data
    end

    %% Data Layer - Shard01 Replica Set
    subgraph "Shard01 (Replica Set)"
        S1A["shard01-a"]:::data
        S1B["shard01-b"]:::data
        S1C["shard01-c"]:::data
    end

    %% Data Layer - Shard02 Replica Set
    subgraph "Shard02 (Replica Set)"
        S2A["shard02-a"]:::data
        S2B["shard02-b"]:::data
        S2C["shard02-c"]:::data
    end

    %% Data Layer - Shard03 Replica Set
    subgraph "Shard03 (Replica Set)"
        S3A["shard03-a"]:::data
        S3B["shard03-b"]:::data
        S3C["shard03-c"]:::data
    end

    %% Initialization Automation
    subgraph "Initialization Automation"
        %% Init Scripts Group
        I_CS["Init Config Server"]:::init
        I_S1["Init Shard01"]:::init
        I_S2["Init Shard02"]:::init
        I_S3["Init Shard03"]:::init
        I_R["Init Router"]:::init

        %% Entrypoint Scripts Group
        E_CS["Entrypoint Config Server"]:::init
        E_S1["Entrypoint Shard01"]:::init
        E_S2["Entrypoint Shard02"]:::init
        E_S3["Entrypoint Shard03"]:::init
        E_R["Entrypoint Router"]:::init
    end

    %% Docker Compose Environment
    subgraph "Docker Compose Environment"
        DC_R["Root Docker Compose"]:::compose
        DC_PSA["PSA Docker Compose"]:::compose
        DC_Min["Minimize Docker Compose"]:::compose
        DC_Key["With-keyfile-auth Docker Compose"]:::compose
    end

    %% Connections from Clients to Routers
    C1 --- R1
    C1 --- R2
    C2 --- R1
    C2 --- R2
    C3 --- R1
    C3 --- R2

    %% Routers fetch configuration from Config Servers
    R1 ---|"fetches_config"| CS1
    R1 ---|"fetches_config"| CS2
    R1 ---|"fetches_config"| CS3
    R2 ---|"fetches_config"| CS1
    R2 ---|"fetches_config"| CS2
    R2 ---|"fetches_config"| CS3

    %% Routers route queries to Shard01 Replica Set
    R1 ---|"routes_query"| S1A
    R1 ---|"routes_query"| S1B
    R1 ---|"routes_query"| S1C
    R2 ---|"routes_query"| S1A
    R2 ---|"routes_query"| S1B
    R2 ---|"routes_query"| S1C

    %% Routers route queries to Shard02 Replica Set
    R1 ---|"routes_query"| S2A
    R1 ---|"routes_query"| S2B
    R1 ---|"routes_query"| S2C
    R2 ---|"routes_query"| S2A
    R2 ---|"routes_query"| S2B
    R2 ---|"routes_query"| S2C

    %% Routers route queries to Shard03 Replica Set
    R1 ---|"routes_query"| S3A
    R1 ---|"routes_query"| S3B
    R1 ---|"routes_query"| S3C
    R2 ---|"routes_query"| S3A
    R2 ---|"routes_query"| S3B
    R2 ---|"routes_query"| S3C

    %% Initialization Automation to Data and Router Components
    I_CS ---|"configures"| CS1
    I_CS ---|"configures"| CS2
    I_CS ---|"configures"| CS3

    I_S1 ---|"configures"| S1A
    I_S1 ---|"configures"| S1B
    I_S1 ---|"configures"| S1C

    I_S2 ---|"configures"| S2A
    I_S2 ---|"configures"| S2B
    I_S2 ---|"configures"| S2C

    I_S3 ---|"configures"| S3A
    I_S3 ---|"configures"| S3B
    I_S3 ---|"configures"| S3C

    I_R ---|"configures"| R1
    I_R ---|"configures"| R2

    %% Entrypoint Scripts trigger corresponding Init Scripts (dashed arrows)
    E_CS ---|"triggers"| I_CS
    E_S1 ---|"triggers"| I_S1
    E_S2 ---|"triggers"| I_S2
    E_S3 ---|"triggers"| I_S3
    E_R ---|"triggers"| I_R

    %% Docker Compose Environment orchestrates the overall setup (dashed connections)
    DC_R ---|"orchestrates"| C1
    DC_R ---|"orchestrates"| R1
    DC_R ---|"orchestrates"| CS1
    DC_R ---|"orchestrates"| S1A

    DC_PSA ---|"orchestrates"| C2
    DC_Min ---|"orchestrates"| R2
    DC_Key ---|"orchestrates"| CS2

    %% Click Events
    click I_CS "https://github.com/minhhungit/mongodb-cluster-docker-compose/blob/master/PSA/scripts/init-configserver.js"
    click I_S1 "https://github.com/minhhungit/mongodb-cluster-docker-compose/blob/master/PSA/scripts/init-shard01.js"
    click I_S2 "https://github.com/minhhungit/mongodb-cluster-docker-compose/blob/master/PSA/scripts/init-shard02.js"
    click I_S3 "https://github.com/minhhungit/mongodb-cluster-docker-compose/blob/master/PSA/scripts/init-shard03.js"
    click I_R "https://github.com/minhhungit/mongodb-cluster-docker-compose/blob/master/PSA/scripts/init-router.js"

    click C1 "https://github.com/minhhungit/mongodb-cluster-docker-compose/tree/master/client/DemoMongoClusterReader"
    click C2 "https://github.com/minhhungit/mongodb-cluster-docker-compose/tree/master/client/DemoMongoClusterWriter"
    click C3 "https://github.com/minhhungit/mongodb-cluster-docker-compose/blob/master/client/MongoCluster.Messages"

    click E_CS "https://github.com/minhhungit/mongodb-cluster-docker-compose/blob/master/scripts/entrypoint-configserver.sh"
    click E_S1 "https://github.com/minhhungit/mongodb-cluster-docker-compose/blob/master/scripts/entrypoint-shard01.sh"
    click E_S2 "https://github.com/minhhungit/mongodb-cluster-docker-compose/blob/master/scripts/entrypoint-shard02.sh"
    click E_S3 "https://github.com/minhhungit/mongodb-cluster-docker-compose/blob/master/scripts/entrypoint-shard03.sh"
    click E_R "https://github.com/minhhungit/mongodb-cluster-docker-compose/blob/master/scripts/entrypoint-route.sh"

    click DC_R "https://github.com/minhhungit/mongodb-cluster-docker-compose/blob/master/docker-compose.yml"
    click DC_PSA "https://github.com/minhhungit/mongodb-cluster-docker-compose/blob/master/PSA/docker-compose.yml"
    click DC_Min "https://github.com/minhhungit/mongodb-cluster-docker-compose/blob/master/minimize/docker-compose.yml"
    click DC_Key "https://github.com/minhhungit/mongodb-cluster-docker-compose/blob/master/with-keyfile-auth/docker-compose.yml"

    %% Styles
    classDef client fill:#a8e6cf,stroke:#379683,stroke-width:2px;
    classDef router fill:#ffd3b6,stroke:#ff8b94,stroke-width:2px;
    classDef data fill:#ffaaa5,stroke:#d9534f,stroke-width:2px;
    classDef init fill:#d0e1f9,stroke:#4d79ff,stroke-width:2px;
    classDef compose fill:#c1f0c1,stroke:#228B22,stroke-width:2px;
```
