# dgraph-io/dgraph architecture

> Architecture diagram of the GitHub repository dgraph-io/dgraph, 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/dgraph-io/dgraph
- Repository: https://github.com/dgraph-io/dgraph

## Overview

##### Main Components Overview

a) Core Services:
- Zero nodes (cluster management, coordination)
- Alpha nodes (database nodes handling data storage and queries)
- Ratel UI (web interface)

b) Supporting Systems:
- GraphQL API layer
- GRPC/HTTP endpoints
- Storage layer
- Query processing engine
- Transaction management

##### Key Architectural Elements

**Client Layer**
- GraphQL Clients
- HTTP/GRPC Clients
- Ratel UI

**API Layer**
- GraphQL API (/graphql)
- HTTP API (/query, /mutate)
- GRPC Services

**Processing Layer**
- Query Processing
- Parser
- Executor
- Optimizer
- Transaction Manager
- Schema Manager

**Storage Layer**
- Graph Backend
- Posting Lists
- LSM Tree Storage

**Cluster Management**
- Zero Node(s)
- Leader Election
- Group Management
- Schema Sync
- Alpha Node(s)
- Data Sharding
- Replication
- RAFT Consensus

##### Connection Types

Different types of connections:
- Double lines: Replication paths

##### Important Details to Highlight

a) Distributed Features:
- RAFT consensus protocol
- Sharding mechanism
- Replication paths
- Transaction boundaries

b) Integration Points:
- External client connections
- Backup/Restore paths
- Monitoring/Metrics endpoints

##### Additional Elements

Annotations for:
- ACID transaction boundaries
- Consistency guarantees
- Scalability points
- High availability mechanisms

##### Performance Aspects

Highlight:
- Query optimization paths
- Index utilization
- Cache layers
- Load balancing points

##### Scaling Information

Show:
- Horizontal scaling paths
- Shard distribution
- Replication factors
- Cluster expansion points

##### Security Layer (Optional)

Include:
- ACL boundaries
- Authentication points
- Encryption paths
- Audit logging

- Native GraphQL support
- Distributed architecture
- ACID transactions
- Graph backend optimization
- Horizontal scalability

## Diagram (Mermaid)

```mermaid
graph TB
    %% Client Layer
    subgraph Clients
        GQLClient[GraphQL Clients]:::client
        HTTPClient[HTTP/GRPC Clients]:::client
        RatelUI[Ratel UI]:::client
    end

    %% API Layer
    subgraph API
        GQLAPI["GraphQL API"]:::api
        HTTPAPI["HTTP/GRPC API"]:::api
    end

    %% Processing Layer
    subgraph Processing
        QueryProc["Query Processing"]:::processing
        TxnMgr["Transaction Manager"]:::processing
        SchemaMgr["Schema Manager"]:::processing
    end

    %% Storage Layer
    subgraph Storage
        GraphBackend["Graph Backend"]:::storage
        PostingLists["Posting Lists"]:::storage
        LSMStorage["LSM Tree Storage"]:::storage
    end

    %% Cluster Management
    subgraph "Cluster Management"
        subgraph "Zero Node"
            ZeroService["Zero Service"]:::cluster
            LeaderElect["Leader Election"]:::cluster
            GroupMgmt["Group Management"]:::cluster
            SchemaSync["Schema Sync"]:::cluster
        end

        subgraph "Alpha Node"
            AlphaService["Alpha Service"]:::cluster
            DataShard["Data Sharding"]:::cluster
            Replication["Replication"]:::cluster
            RAFTConsensus["RAFT Consensus"]:::cluster
        end
    end

    %% Connections
    GQLClient --> GQLAPI
    HTTPClient --> HTTPAPI
    RatelUI --> HTTPAPI
    
    GQLAPI --> QueryProc
    HTTPAPI --> QueryProc
    
    QueryProc --> TxnMgr
    QueryProc --> SchemaMgr
    
    TxnMgr --> PostingLists
    SchemaMgr --> GraphBackend
    
    PostingLists --> LSMStorage
    GraphBackend --> LSMStorage
    
    ZeroService --> SchemaSync
    SchemaSync --> GroupMgmt
    GroupMgmt --> LeaderElect
    
    AlphaService --> DataShard
    DataShard --> Replication
    Replication --> RAFTConsensus
    
    ZeroService --> AlphaService
    
    %% Component Mappings
    click ZeroService "https://github.com/dgraph-io/dgraph/tree/main/dgraph/cmd/zero/"
    click AlphaService "https://github.com/dgraph-io/dgraph/tree/main/dgraph/cmd/alpha/"
    click GQLAPI "https://github.com/dgraph-io/dgraph/tree/main/graphql/"
    click QueryProc "https://github.com/dgraph-io/dgraph/tree/main/query/"
    click PostingLists "https://github.com/dgraph-io/dgraph/tree/main/posting/"
    click SchemaMgr "https://github.com/dgraph-io/dgraph/tree/main/schema/"
    click TxnMgr "https://github.com/dgraph-io/dgraph/blob/main/posting/mvcc.go"
    click GroupMgmt "https://github.com/dgraph-io/dgraph/tree/main/worker/"
    click HTTPAPI "https://github.com/dgraph-io/dgraph/tree/main/conn/"
    click GraphBackend "https://github.com/dgraph-io/dgraph/tree/main/algo/"

    %% Styles
    classDef client fill:#2374ab
    classDef api fill:#48a9a6
    classDef processing fill:#4ad66d
    classDef storage fill:#e4cc37
    classDef cluster fill:#d81159

    %% Legend
    subgraph Legend
        Client[Client Components]:::client
        APIComp[API Components]:::api
        ProcessingComp[Processing Components]:::processing
        StorageComp[Storage Components]:::storage
        ClusterComp[Cluster Components]:::cluster
    end
```
