# hypermodeinc/dgraph architecture

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

- Interactive diagram: https://gitdiagram.com/hypermodeinc/dgraph
- Repository: https://github.com/hypermodeinc/dgraph

## Overview

##### Main project type and purpose
- Recognize Dgraph as a distributed database system specifically built to handle graph queries using a GraphQL-inspired query language.
- Its purpose is to combine NoSQL scalability with SQL-like transactions, distributed joins, and reduced latency on large-scale, real-time data sets.

##### Component structure from the file tree and README
- The file tree shows many top-level directories and modules. Important directories include “dgraph” (which houses commands for alpha, bulk loading, zero nodes, etc.), “dql” (query and parser modules), “graphql” (resolver, schema, API support for GraphQL), “worker” (handling backup, restore, mutation processing), and “ee” (enterprise and extended functionalities such as ACL, audit, encryption, multi-tenancy).
- The README gives clear indications that the system runs in a clustered setup with separate roles such as “alpha” (query processing and data serving) and “zero” (cluster coordination, leader election, shard rebalancing using Raft).
- Tools for testing (systest, dgraphtest) and deployment (Dockerfiles, Kubernetes configurations in “compose”) are also included.

##### Components and elements
a. Core Components:
- Client Interface Layer: External clients connect to Dgraph via GraphQL, HTTP (JSON, RDF), or GRPC. Mark these as consumer endpoints.
- Query Processing Nodes (“Alpha” nodes): These components process queries, mutations, and lookups. They host the GraphQL API and the query resolver layer.
- Cluster Management Nodes (“Zero” nodes): These are responsible for managing Raft-based cluster coordination, leader election, and shard assignments.
- Storage Engine & Data Persistence: Underlying the alpha nodes is the graph storage engine (which comprises posting lists, index management, etc.). You could indicate storage subsystems responsible for write-ahead logs (as seen in “raftwal”) and on-disk data organization.
- Supporting Services: Backup/restore workers, audit and ACL components (found under “ee” and “worker”), and modules for schema processing (“schema”, “query”, “codec” and “posting” packages).
- External Dependencies: Underlying networking infrastructure (e.g., Docker, Kubernetes orchestration) and client libraries.

b. Relationships and Data Flows:
- Client requests entering via GraphQL/HTTP/GRPC into the Alpha nodes.
- Alpha nodes communicating with Zero nodes for cluster coordination, configuration updates, and shard mappings.
- Internal data flows from query processing to storage engines, with Raft groups ensuring consistent replication.
- Flows that integrate with external systems (Docker deployment, external client libraries).

##### Architectural patterns and design principles
- Distributed, sharded architecture with separation of concerns – query processing (Alpha) versus cluster management (Zero).
- Use of Raft for consensus and ensuring ACID-compliant, distributed transaction processing.
- Native support for GraphQL interfaces combined with lower-level storage optimizations.
- Modular design where various packages (e.g., worker, posting, worker/backup, ee for enterprise features) encapsulate specific functionalities.

##### Additional relay points
- For a full-stack application like Dgraph, ensure that you capture both the client-facing API and the internal data replication and cluster coordination.
- Technologies such as GRPC, HTTP, Raft consensus, and GraphQL.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Client Layer
    subgraph "Client Layer"
        CLIENT("Clients"):::client
        API("GraphQL API & Resolver"):::interface
    end

    %% Query & Processing Layer
    subgraph "Query & Processing Layer"
        ALPHA("Alpha Node - Query Processing"):::processing
        SCHEMA("Schema Processing & Query Utilities"):::processing
    end

    %% Cluster Management Layer
    subgraph "Cluster Management Layer"
        ZERO("Zero Node - Cluster Coordination"):::cluster
    end

    %% Storage Layer
    subgraph "Storage Layer"
        POSTING("Storage Engine: Posting Lists"):::storage
        RAFT("Storage Engine: Raft Logs"):::storage
    end

    %% Supporting Services
    subgraph "Supporting Services"
        WORKER("Backup/Restore Worker"):::supporting
        EE("Enterprise Features: ACL,Audit,Encryption"):::supporting
    end

    %% Data Flows
    CLIENT -->|"sends request"| API
    API -->|"processes request"| ALPHA
    ALPHA -->|"queries storage"| POSTING
    ALPHA -->|"queries storage"| RAFT
    ALPHA -->|"executes schema logic"| SCHEMA
    ALPHA -->|"requests config"| ZERO
    ZERO -->|"provides updates"| ALPHA
    ALPHA -->|"enforces security"| EE
    WORKER -->|"performs backup"| POSTING
    WORKER -->|"performs backup"| RAFT

    %% Click Events
    click API "https://github.com/hypermodeinc/dgraph/tree/main/graphql"
    click ALPHA "https://github.com/hypermodeinc/dgraph/tree/main/dgraph/cmd/alpha"
    click ZERO "https://github.com/hypermodeinc/dgraph/tree/main/dgraph/cmd/zero"
    click POSTING "https://github.com/hypermodeinc/dgraph/tree/main/posting"
    click RAFT "https://github.com/hypermodeinc/dgraph/tree/main/raftwal"
    click WORKER "https://github.com/hypermodeinc/dgraph/tree/main/worker"
    click EE "https://github.com/hypermodeinc/dgraph/tree/main/ee"
    click SCHEMA "https://github.com/hypermodeinc/dgraph/tree/main/schema"

    %% Styles
    classDef client fill:#aee1f9,stroke:#333,stroke-width:2px;
    classDef interface fill:#ffdddd,stroke:#333,stroke-width:2px;
    classDef processing fill:#ccffcc,stroke:#333,stroke-width:2px;
    classDef cluster fill:#d9ead3,stroke:#333,stroke-width:2px;
    classDef storage fill:#ffffcc,stroke:#333,stroke-width:2px;
    classDef supporting fill:#f9e79f,stroke:#333,stroke-width:2px;
```
