# moleculerjs/moleculer architecture

> Architecture diagram of the GitHub repository moleculerjs/moleculer, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2024-12-27). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/moleculerjs/moleculer
- Repository: https://github.com/moleculerjs/moleculer

## Overview

##### Main Components

a. Core Framework Components:
- Service Broker (central component)
- Service Registry
- Transit System
- Context System
- Middleware Pipeline

b. Plugin Systems:
- Transporters (TCP, NATS, MQTT, Redis, Kafka, AMQP)
- Serializers (JSON, Avro, MsgPack, ProtoBuf, etc.)
- Cachers (Memory, Redis)
- Validators
- Loggers
- Metrics & Tracing systems

c. Service Management:
- Service Discovery
- Load Balancing
- Fault Tolerance features

**┌─────────────────────────────────────────────────────────┐**
- Client Applications                    │
- ┘
- ↕
**┌─────────────────────────────────────────────────────────┐**
- API Gateway                         │
- ┘
- ↕
**┌─────────────────────────────────────────────────────────┐**
- Service Broker Layer                   │
- ┬─────────────┬──────────────┬─────────────┤
- Registry   │   Transit   │  Middleware  │   Context   │
- ┴─────────────┴──────────────┴─────────────┘
- ↕
**┌─────────────────────────────────────────────────────────┐**
- Plugin Systems                        │
- ┬──────────┬─────────┬──────────┬─────────────┤
- Transport │Serializer│ Cacher  │ Validator│   Logger    │
- ┴──────────┴─────────┴──────────┴─────────────┘
- ↕
**┌─────────────────────────────────────────────────────────┐**
- Services Layer                         │
- ┬─────────────┬──────────────┬─────────────┤
- Service A  │  Service B  │  Service C   │  Service D  │
- ┴─────────────┴──────────────┴─────────────┘

##### Important Relationships

- Service-to-Service communication paths
- Broker-to-Broker communication
- Plugin integration points
- Event flow paths
- Request-reply patterns

##### Additional Elements

- Fault tolerance patterns (Circuit Breaker, Bulkhead, Retry)
- Metrics and tracing flows
- Service discovery mechanisms

##### Technical Details

- Transport protocol options
- Serialization formats
- Caching mechanisms
- Load balancing strategies
- Service lifecycle hooks

##### Specific Implementation Notes

- Version numbers for services
- Scalability points
- Hot-reload capabilities

## Diagram (Mermaid)

```mermaid
graph TB
    %% Client Layer
    subgraph Clients
        Client1[Client Applications]
        Client2[API Gateway]
    end

    %% Core Layer
    subgraph CoreLayer[Core Framework Layer]
        ServiceBroker["Service Broker"]:::core
        Registry["Service Registry"]:::core
        Transit["Transit System"]:::core
        Context["Context System"]:::core
        Middleware["Middleware Pipeline"]:::core
    end

    %% Plugin Systems Layer
    subgraph PluginLayer[Plugin Systems]
        direction TB
        Transporters["Transporters"]:::plugin
        Serializers["Serializers"]:::plugin
        Cachers["Cachers"]:::plugin
        Validators["Validators"]:::plugin
        Loggers["Loggers"]:::plugin
        Metrics["Metrics System"]:::plugin
        Tracing["Tracing System"]:::plugin
    end

    %% Service Management Layer
    subgraph ManagementLayer[Service Management]
        Discovery["Service Discovery"]:::management
        LoadBalancer["Load Balancing"]:::management
        CircuitBreaker["Circuit Breaker"]:::management
        Bulkhead["Bulkhead"]:::management
        ServiceMgmt["Service Management"]:::management
    end

    %% Services Layer
    subgraph ServicesLayer[Services Layer]
        ServiceA["Service A"]:::service
        ServiceB["Service B"]:::service
        ServiceC["Service C"]:::service
        ServiceD["Service D"]:::service
    end

    %% Relationships
    Client1 --> Client2
    Client2 --> ServiceBroker
    ServiceBroker --> Registry
    ServiceBroker --> Transit
    ServiceBroker --> Context
    ServiceBroker --> Middleware
    
    Transit --> Transporters
    Transit --> Serializers
    ServiceBroker --> Cachers
    ServiceBroker --> Validators
    ServiceBroker --> Loggers
    ServiceBroker --> Metrics
    ServiceBroker --> Tracing
    
    Registry --> Discovery
    ServiceBroker --> LoadBalancer
    Middleware --> CircuitBreaker
    Middleware --> Bulkhead
    ServiceBroker --> ServiceMgmt
    
    ServiceMgmt --> ServiceA
    ServiceMgmt --> ServiceB
    ServiceMgmt --> ServiceC
    ServiceMgmt --> ServiceD

    %% Click Events
    click ServiceBroker "https://github.com/moleculerjs/moleculer/blob/master/src/service-broker.js"
    click Registry "https://github.com/moleculerjs/moleculer/blob/master/src/registry/registry.js"
    click Transit "https://github.com/moleculerjs/moleculer/blob/master/src/transit.js"
    click Context "https://github.com/moleculerjs/moleculer/blob/master/src/context.js"
    click Middleware "https://github.com/moleculerjs/moleculer/blob/master/src/middleware.js"
    click Transporters "https://github.com/moleculerjs/moleculer/blob/master/src/transporters/index.js"
    click Serializers "https://github.com/moleculerjs/moleculer/blob/master/src/serializers/index.js"
    click Cachers "https://github.com/moleculerjs/moleculer/blob/master/src/cachers/index.js"
    click Validators "https://github.com/moleculerjs/moleculer/blob/master/src/validators/index.js"
    click Loggers "https://github.com/moleculerjs/moleculer/blob/master/src/loggers/index.js"
    click Metrics "https://github.com/moleculerjs/moleculer/blob/master/src/metrics/index.js"
    click Tracing "https://github.com/moleculerjs/moleculer/blob/master/src/tracing/index.js"
    click Discovery "https://github.com/moleculerjs/moleculer/blob/master/src/registry/discoverers/index.js"
    click LoadBalancer "https://github.com/moleculerjs/moleculer/blob/master/src/strategies/index.js"
    click CircuitBreaker "https://github.com/moleculerjs/moleculer/blob/master/src/middlewares/circuit-breaker.js"
    click Bulkhead "https://github.com/moleculerjs/moleculer/blob/master/src/middlewares/bulkhead.js"
    click ServiceMgmt "https://github.com/moleculerjs/moleculer/blob/master/src/service.js"

    %% Styles
    classDef core fill:#2374ab,stroke:#000,stroke-width:2px,color:#fff
    classDef plugin fill:#ff7f50,stroke:#000,stroke-width:2px,color:#fff
    classDef management fill:#4caf50,stroke:#000,stroke-width:2px,color:#fff
    classDef service fill:#9c27b0,stroke:#000,stroke-width:2px,color:#fff

    %% Legend
    subgraph Legend
        CoreComp["Core Components"]:::core
        PluginComp["Plugin Systems"]:::plugin
        ManagementComp["Management Components"]:::management
        ServiceComp["Services"]:::service
    end
```
