# rivet-gg/rivet architecture

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

- Interactive diagram: https://gitdiagram.com/rivet-gg/rivet
- Repository: https://github.com/rivet-gg/rivet

## Overview

##### Main Components

a. Core Infrastructure Layer:
- FoundationDB (Actor state storage)
- CockroachDB (OLTP database)
- ClickHouse (Monitoring/analytics)
- NATS (Pub/sub messaging)
- Valkey (Caching layer)

b. Runtime Layer:
- V8 & Deno (Actor isolate runtime)
- Container Runtime (For non-isolate workloads)

c. Networking Layer:
- Traefik (Load balancing & tunneling)
- Edge Network Components
- Game Guard (TCP/UDP handling)

d. Service Layer:
- Actor Service
- Authentication Service
- Matchmaker Service
- CDN Service
- Analytics Service
- Job Service

##### Component Relationships

Key interactions:

a. Data Flow:
- Client → Edge Network → Actor Runtime
- Actor Runtime ↔ FoundationDB (state)
- Services ↔ CockroachDB (operational data)
- Monitoring Data → ClickHouse

b. Control Flow:
- Actor Creation/Scheduling
- RPC Routing
- Event Distribution
- State Management

##### Regional Distribution

Illustrate:
- Global Edge Locations
- Regional Actor Distribution
- Data Locality Zones

##### Design Elements

Use these visual elements:

- Blue: Core Infrastructure
- Green: Runtime Components
- Orange: Networking Layer
- Purple: Service Layer
- Gray: External Systems

- Rectangles: Services
- Hexagons: Actor Instances
- Clouds: Network Components

c. Lines:
- Solid: Synchronous Communication

##### Create multiple layers showing
- Infrastructure Layer (bottom)
- Service Layer (middle)
- Client/Edge Layer (top)

##### Subsystems
- Actor Management
- State Management
- Event Distribution
- Authentication & Authorization
- Monitoring & Logging

##### Scaling components
- Load Balancers
- Actor Distribution
- State Replication

## Diagram (Mermaid)

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

    subgraph "Edge Network"
        EN["Edge Network"]:::network
        TR["Traefik Load Balancer"]:::network
        GG["Game Guard"]:::network
    end

    subgraph "Service Layer"
        AS["Actor Service"]:::service
        AUS["Authentication Service"]:::service
        MS["Matchmaker Service"]:::service
        CDNS["CDN Service"]:::service
        ANS["Analytics Service"]:::service
        JS["Job Service"]:::service
        IS["Identity Service"]:::service
        GS["Group Service"]:::service
        TS["Team Service"]:::service
        US["Upload Service"]:::service
    end

    subgraph "Runtime Layer"
        AR["Actor Runtime (V8/Deno)"]:::runtime
        CR["Container Runtime"]:::runtime
    end

    subgraph "Infrastructure Layer"
        FDB[(FoundationDB)]:::database
        CDB[(CockroachDB)]:::database
        CH[(ClickHouse)]:::database
        NATS[("NATS")]:::messaging
        VK["Valkey Cache"]:::cache
    end

    %% Client Layer Connections
    CL --> EN

    %% Edge Network Connections
    EN --> TR
    TR --> GG
    TR --> AS
    TR --> AUS
    TR --> MS
    TR --> CDNS

    %% Service Layer Connections
    AS --> AR
    AS --> CR
    AS --> FDB
    AUS --> CDB
    MS --> CDB
    CDNS --> CDB
    ANS --> CH
    JS --> CDB

    %% Runtime Layer Connections
    AR --> FDB
    AR --> NATS
    CR --> FDB
    CR --> NATS

    %% Infrastructure Layer Connections
    FDB --> VK
    CDB --> VK
    
    %% Click Events
    click EN "https://github.com/rivet-gg/rivet/blob/main/docs-internal/infrastructure/networking/EDGE_CLUSTER_NETWORKING.md"
    click TR "https://github.com/rivet-gg/rivet/blob/main/docker/dev-full/rivet-guard/traefik.yaml"
    click AS "https://github.com/rivet-gg/rivet/tree/main/packages/api/actor/"
    click AUS "https://github.com/rivet-gg/rivet/tree/main/packages/api/auth/"
    click MS "https://github.com/rivet-gg/rivet/tree/main/packages/api/matchmaker/"
    click CDNS "https://github.com/rivet-gg/rivet/tree/main/packages/api/cloud/src/route/games/cdn/"
    click ANS "https://github.com/rivet-gg/rivet/tree/main/packages/api/cloud/src/route/games/namespaces/analytics/"
    click JS "https://github.com/rivet-gg/rivet/tree/main/packages/api/job/"
    click IS "https://github.com/rivet-gg/rivet/tree/main/packages/api/identity/"
    click GS "https://github.com/rivet-gg/rivet/tree/main/packages/api/group/"
    click TS "https://github.com/rivet-gg/rivet/tree/main/packages/api/team/"
    click US "https://github.com/rivet-gg/rivet/tree/main/packages/api/upload/"
    click AR "https://github.com/rivet-gg/rivet/tree/main/packages/infra/client/isolate-v8-runner/"
    click CR "https://github.com/rivet-gg/rivet/tree/main/packages/infra/client/container-runner/"
    click FDB "https://github.com/rivet-gg/rivet/tree/main/docs-internal/infrastructure/fdb/"
    click CDB "https://github.com/rivet-gg/rivet/tree/main/docs-internal/infrastructure/cockroach/"
    click CH "https://github.com/rivet-gg/rivet/tree/main/docs-internal/infrastructure/clickhouse/"
    click NATS "https://github.com/rivet-gg/rivet/tree/main/docs-internal/infrastructure/nats/"

    %% Styling
    classDef default fill:#f9f9f9,stroke:#333,stroke-width:2px
    classDef network fill:#FFA07A,stroke:#333,stroke-width:2px
    classDef service fill:#DDA0DD,stroke:#333,stroke-width:2px
    classDef runtime fill:#98FB98,stroke:#333,stroke-width:2px
    classDef database fill:#87CEEB,stroke:#333,stroke-width:2px
    classDef messaging fill:#FFD700,stroke:#333,stroke-width:2px
    classDef cache fill:#E6E6FA,stroke:#333,stroke-width:2px

    %% Legend
    subgraph Legend
        L1[Network Component]:::network
        L2[Service Component]:::service
        L3[Runtime Component]:::runtime
        L4[(Database Component)]:::database
        L5[("Messaging Component")]:::messaging
        L6[Cache Component]:::cache
    end
```
