# instill-ai/model-backend architecture

> Architecture diagram of the GitHub repository instill-ai/model-backend, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-01-07). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/instill-ai/model-backend
- Repository: https://github.com/instill-ai/model-backend

## Overview

##### Main Components

a) Core Services Layer:
- Model Management Service (pkg/service)
- Worker Service (pkg/worker)
- Repository Layer (pkg/repository)
- Handler Layer (pkg/handler)

b) External Integration Layer:
- Ray Integration (pkg/ray)
- Database Systems
- SQL Database (pkg/db)
- InfluxDB (indicated by repository/influx.go)

c) API Layer:
- gRPC Services (shown by proto files)
- REST Endpoints (indicated by integration tests)
- Private and Public APIs (pkg/handler/private.go and public.go)

d) Model Resources:
- Model Definitions
- Model Instances
- Various Task Types (shown in proto/model/model/v1alpha/)

**[Client Layer]**

**[API Gateway]**

**[Service Layer] ←→ [Authentication/ACL]**

**[Core Services] ←→ [Worker Pool]**

**[Storage Layer]**

##### Key Relationships to Highlight

- Flow from API endpoints through handlers to services
- Worker system's interaction with Ray for model execution
- Demonstrate database interactions for model management
- ACL (Access Control Layer) integration

##### Important Architectural Patterns to Emphasize

- Microservice Architecture: Show clear separation between different service components
- Repository Pattern: Highlight the data access layer
- Worker Pool Pattern: Demonstrate the distributed task execution
- GRPC/REST dual interface: Show both communication methods

##### Additional Elements

- A section for model assets (shown in /assets directory)
- Integration with external services (Ray)
- Demonstrate the migration system (pkg/db/migration)
- Monitoring and logging components (pkg/logger)

##### Specific Implementation Details

- Different model task types supported (classification, detection, OCR, etc.)
- Separation between public and private APIs
- Operation handling system for long-running tasks
- Metadata retention and usage tracking systems

- Component types
- Any abbreviations used

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Styles and classes
    classDef api fill:#2196F3,stroke:#1565C0,color:white
    classDef service fill:#4CAF50,stroke:#388E3C,color:white
    classDef storage fill:#FFC107,stroke:#FFA000,color:black
    classDef security fill:#F44336,stroke:#D32F2F,color:white
    classDef worker fill:#9C27B0,stroke:#7B1FA2,color:white
    classDef monitoring fill:#795548,stroke:#5D4037,color:white
    
    %% Client and API Layer
    Client[Client Applications]
    APIGateway["API Gateway"]:::api
    
    subgraph API["API Layer"]
        PublicAPI["Public API Handler"]:::api
        PrivateAPI["Private API Handler"]:::api
        ProtoAPI["API Protocol Definitions"]:::api
    end
    
    subgraph Security["Security Layer"]
        ACL["Access Control Layer"]:::security
    end
    
    subgraph Core["Core Services"]
        ModelService["Model Management Service"]:::service
        WorkerService["Worker Service"]:::service
        RepoLayer["Repository Layer"]:::service
        HandlerLayer["Handler Layer"]:::service
        DataModels["Data Models"]:::service
        UsageTracker["Usage Tracking"]:::service
    end
    
    subgraph Workers["Worker System"]
        WorkflowEngine["Model Workflow Engine"]:::worker
        RayIntegration["Ray Integration"]:::worker
    end
    
    subgraph Storage["Storage Layer"]
        DB["Database Layer"]:::storage
        InfluxDB["InfluxDB Integration"]:::storage
        Migrations["Database Migrations"]:::storage
        ModelAssets["Model Resources"]:::storage
    end
    
    subgraph Monitor["Monitoring"]
        Logger["Logger Component"]:::monitoring
        TaskTypes["Task Type Definitions"]:::monitoring
    end
    
    %% Relationships
    Client --> APIGateway
    APIGateway --> PublicAPI & PrivateAPI
    PublicAPI & PrivateAPI --> ACL
    ACL --> HandlerLayer
    HandlerLayer --> ModelService
    ModelService --> RepoLayer
    RepoLayer --> DB & InfluxDB
    WorkerService --> WorkflowEngine
    WorkflowEngine --> RayIntegration
    ModelService --> WorkerService
    DB --> Migrations
    ModelService --> ModelAssets
    
    %% Component click events
    click ModelService "https://github.com/instill-ai/model-backend/blob/main/pkg/service/service.go"
    click WorkerService "https://github.com/instill-ai/model-backend/blob/main/pkg/worker/worker.go"
    click RepoLayer "https://github.com/instill-ai/model-backend/blob/main/pkg/repository/repository.go"
    click HandlerLayer "https://github.com/instill-ai/model-backend/blob/main/pkg/handler/handler.go"
    click RayIntegration "https://github.com/instill-ai/model-backend/blob/main/pkg/ray/ray.go"
    click DB "https://github.com/instill-ai/model-backend/blob/main/pkg/db/db.go"
    click InfluxDB "https://github.com/instill-ai/model-backend/blob/main/pkg/repository/influx.go"
    click PrivateAPI "https://github.com/instill-ai/model-backend/blob/main/pkg/handler/private.go"
    click PublicAPI "https://github.com/instill-ai/model-backend/blob/main/pkg/handler/public.go"
    click ModelAssets "https://github.com/instill-ai/model-backend/tree/main/assets/"
    click Migrations "https://github.com/instill-ai/model-backend/tree/main/pkg/db/migration/"
    click TaskTypes "https://github.com/instill-ai/model-backend/tree/main/integration-test/proto/model/model/v1alpha/"
    click ProtoAPI "https://github.com/instill-ai/model-backend/tree/main/integration-test/proto/"
    click Logger "https://github.com/instill-ai/model-backend/blob/main/pkg/logger/logger.go"
    click ACL "https://github.com/instill-ai/model-backend/blob/main/pkg/acl/acl.go"
    click UsageTracker "https://github.com/instill-ai/model-backend/blob/main/pkg/usage/usage.go"
    click WorkflowEngine "https://github.com/instill-ai/model-backend/blob/main/pkg/worker/workflow.go"
    click DataModels "https://github.com/instill-ai/model-backend/blob/main/pkg/datamodel/datamodel.go"
```
