# njrslv/raft architecture

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

## Overview

##### Main Components

A. Cluster Nodes (Multiple Instances)
Each node should contain:
- Key-Value Server
- Raft Server
- Storage Layer
- State Machine
- RPC Communication Layer

B. Client Interface
- HTTP API Endpoints
- Key-Value Operations

##### Component Relationships

a) External Interface Layer:
- To the Key-Value Server component (kvapi.go)

- KV Server component (from kv/kvapi.go)
- Raft Server component (from server.go)
- State Machine (from state_machine.go)
- Storage System (from storage.go)
- RPC Layer (from rpc.go)

c) Inter-Node Communication:
- Use proto/raft.proto definitions for communication contracts

##### Specific Implementation Details to Highlight

a) Persistence Layer:
- State persistence files (kv/bin/state#{id}.json)
- Persistent storage for Raft logs

b) Configuration:
- Config.go component showing cluster configuration
- Time-related configurations from time.go

- Client requests → KV API → Raft Leader
- Leader → Follower replication
- Log persistence flows
- State machine updates

##### Configuration Components
- Cluster Configuration
- Timing Parameters

##### Core Raft Features
- Leader Election
- Log Replication
- State Persistence

##### Supporting Systems
- Logging System
- Error Handling

- The separation between the KV store interface and Raft consensus layer
- The replication of logs between nodes
- The persistence mechanism
- The RPC communication paths

##### Additional Notes

A note section mentioning:
- Unimplemented features (Membership Changes, Log Compaction)
- The slow coefficient for timing
- The fact that KV and Raft servers run in the same process

## Diagram (Mermaid)

```mermaid
graph TB
    subgraph Client
        HTTP["/set, /get HTTP Endpoints"]:::api
    end

    subgraph "Node Components"
        KVServer["Key-Value Server"]:::kv
        RaftServer["Raft Server"]:::raft
        StateMachine["State Machine"]:::core
        Storage["Storage System"]:::storage
        RPCLayer["RPC Communication"]:::comm
        
        Config["Configuration"]:::config
        TimeControl["Timing Control"]:::config
        Logger["Logging System"]:::support
        
        KVServer --> RaftServer
        RaftServer --> StateMachine
        StateMachine --> Storage
        RaftServer --> RPCLayer
        
        Config --> RaftServer
        TimeControl --> RaftServer
        Logger --> RaftServer
    end

    subgraph "Persistence"
        StateFiles["State Files"]:::storage
        LogStore["Log Storage"]:::storage
        
        Storage --> StateFiles
        RaftServer --> LogStore
    end

    HTTP --> KVServer

    subgraph "Inter-Node Communication"
        RPCProto["RPC Protocol"]:::comm
        RPCLayer --> RPCProto
    end

    %% Component Mappings
    click KVServer "https://github.com/NJrslv/raft/blob/main/kv/kvapi.go"
    click RaftServer "https://github.com/NJrslv/raft/blob/main/server.go"
    click StateMachine "https://github.com/NJrslv/raft/blob/main/state_machine.go"
    click Storage "https://github.com/NJrslv/raft/blob/main/storage.go"
    click RPCLayer "https://github.com/NJrslv/raft/blob/main/rpc.go"
    click RPCProto "https://github.com/NJrslv/raft/blob/main/proto/raft.proto"
    click Config "https://github.com/NJrslv/raft/blob/main/config.go"
    click TimeControl "https://github.com/NJrslv/raft/blob/main/time.go"
    click StateFiles "https://github.com/NJrslv/raft/tree/main/kv/bin"
    click Logger "https://github.com/NJrslv/raft/blob/main/logger.go"

    %% Styling
    classDef api fill:#2196F3,stroke:#1565C0,color:white
    classDef kv fill:#4CAF50,stroke:#2E7D32,color:white
    classDef raft fill:#81C784,stroke:#388E3C,color:white
    classDef core fill:#66BB6A,stroke:#2E7D32,color:white
    classDef storage fill:#FFB74D,stroke:#F57C00,color:white
    classDef comm fill:#9575CD,stroke:#5E35B1,color:white
    classDef config fill:#90A4AE,stroke:#546E7A,color:white
    classDef support fill:#B0BEC5,stroke:#78909C,color:white

    %% Legend
    subgraph Legend
        L1["API Interface"]:::api
        L2["Core Components"]:::raft
        L3["Storage"]:::storage
        L4["Communication"]:::comm
        L5["Configuration"]:::config
    end
```
