# mosecorg/mosec architecture

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

- Interactive diagram: https://gitdiagram.com/mosecorg/mosec
- Repository: https://github.com/mosecorg/mosec

## Overview

##### Main Components

a. Core Components:
- Web Server Layer (Rust-based)
- Task Coordinator
- Worker Pipeline System
- Dynamic Batching Manager
- Monitoring System

b. Integration Components:
- Client Interface
- Model Storage
- Metrics Endpoint
- OpenAPI Documentation

**Client Layer**

**API Layer (Rust)**

**Coordination Layer**
- Dynamic Batching
- Task Distribution

**Worker Pipeline Layer**
- Worker Pool 1 (Stage 1)
- Worker Pool 2 (Stage 2)
- Worker Pool N (Stage N)

**Model Layer**

##### Key Elements and Relationships

a. Request Flow:
- Dynamic batching aggregation points
- How batched requests are distributed to workers

b. Process Communication:
- IPC (Inter-Process Communication) between stages
- Shared memory options (Redis/Plasma)
- Serialization/deserialization points

c. Monitoring Flow:
- Metrics collection points
- Prometheus integration
- Monitoring dashboard connection

##### Technical Details

- Protocol support (JSON/msgpack)
- Worker process pools
- Batch size configuration
- Wait time parameters
- GPU memory management
- IPC mechanisms

##### Implementation Details

- Server class (`mosec/server.py`)
- Worker implementation (`mosec/worker.py`)
- Coordination logic (`mosec/coordinator.py`)
- Mixin capabilities (`mosec/mixin/`)

##### External Integration Points

Connections to:
- Kubernetes integration points
- Monitoring systems
- Model storage systems
- Client applications

## Diagram (Mermaid)

```mermaid
graph TB
    subgraph Client["Client Layer"]
        C1[Client Applications]
    end

    subgraph API["Web Server Layer"]
        WS["Web Server"]:::rust
        API_DOC["OpenAPI Documentation"]:::rust
        ROUTES["Request Router"]:::rust
    end

    subgraph Coordination["Coordination Layer"]
        TC["Task Coordinator"]:::core
        DB["Dynamic Batching"]:::core
        PROTO["Protocol Handler"]:::core
    end

    subgraph Workers["Worker Pipeline Layer"]
        WP1["Worker Pool Stage 1"]:::worker
        WP2["Worker Pool Stage 2"]:::worker
        WP3["Worker Pool Stage N"]:::worker
        
        subgraph Mixins["Worker Mixins"]
            MIX1["MsgPack Worker"]:::mixin
            MIX2["NumBin Worker"]:::mixin
            MIX3["Plasma Worker"]:::mixin
            MIX4["Redis Worker"]:::mixin
            MIX5["Typed Worker"]:::mixin
        end
    end

    subgraph Storage["Storage Layer"]
        MS["Model Storage"]
        SHM["Shared Memory"]
    end

    subgraph Monitor["Monitoring Layer"]
        PROM["Prometheus"]:::monitor
        DASH["Dashboard"]:::monitor
        METRICS["Metrics Collector"]:::monitor
    end

    C1 --> WS
    WS --> API_DOC
    WS --> ROUTES
    ROUTES --> TC
    TC --> DB
    TC --> PROTO
    DB --> WP1
    DB --> WP2
    DB --> WP3
    WP1 --> WP2
    WP2 --> WP3
    WP1 -.-> MIX1
    WP1 -.-> MIX2
    WP2 -.-> MIX3
    WP2 -.-> MIX4
    WP3 -.-> MIX5
    WP1 --> MS
    WP2 --> MS
    WP3 --> MS
    WP1 --> SHM
    WP2 --> SHM
    WP3 --> SHM
    TC --> METRICS
    METRICS --> PROM
    PROM --> DASH

    classDef default fill:#f9f9f9,stroke:#333,stroke-width:1px
    classDef rust fill:#deb887,stroke:#333,stroke-width:1px
    classDef core fill:#ff9999,stroke:#333,stroke-width:1px
    classDef worker fill:#90ee90,stroke:#333,stroke-width:1px
    classDef mixin fill:#ffd700,stroke:#333,stroke-width:1px
    classDef monitor fill:#dda0dd,stroke:#333,stroke-width:1px

    click WS "https://github.com/mosecorg/mosec/blob/main/src/main.rs"
    click ROUTES "https://github.com/mosecorg/mosec/blob/main/src/routes.rs"
    click API_DOC "https://github.com/mosecorg/mosec/blob/main/src/apidoc.rs"
    click TC "https://github.com/mosecorg/mosec/blob/main/mosec/coordinator.py"
    click PROTO "https://github.com/mosecorg/mosec/blob/main/src/protocol.rs"
    click WP1 "https://github.com/mosecorg/mosec/blob/main/mosec/worker.py"
    click MIX1 "https://github.com/mosecorg/mosec/blob/main/mosec/mixin/msgpack_worker.py"
    click MIX2 "https://github.com/mosecorg/mosec/blob/main/mosec/mixin/numbin_worker.py"
    click MIX3 "https://github.com/mosecorg/mosec/blob/main/mosec/mixin/plasma_worker.py"
    click MIX4 "https://github.com/mosecorg/mosec/blob/main/mosec/mixin/redis_worker.py"
    click MIX5 "https://github.com/mosecorg/mosec/blob/main/mosec/mixin/typed_worker.py"
    click METRICS "https://github.com/mosecorg/mosec/blob/main/src/metrics.rs"
    click PROM "https://github.com/mosecorg/mosec/blob/main/examples/monitor/prometheus.yml"
    click DASH "https://github.com/mosecorg/mosec/blob/main/examples/monitor/mosec_dashboard.json"
    click SHM "https://github.com/mosecorg/mosec/tree/main/examples/shm_ipc"
```
