# msterx-1/raft-algorithm architecture

> Architecture diagram of the GitHub repository msterx-1/raft-algorithm, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-11-26). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/msterx-1/raft-algorithm
- Repository: https://github.com/msterx-1/raft-algorithm

## Overview

This is a teaching implementation of Raft consensus, using a simulated RPC network to elect leaders and replicate and apply log entries. The main workflow is an application submitting a command to the leader, replication to peers, commitment by a majority, and delivery to the application. Election, heartbeat, and conflict-repair behavior are also represented. The excerpts show the protocol and transport calls; the application is represented as an external actor. The tester support in raft/config.go is omitted as peripheral; unsampled behavior is not inferred.

Main entry points: [`raft/raft.go`](https://github.com/msterx-1/raft-algorithm/blob/main/raft/raft.go) (Log replication), [`labrpc/labrpc.go`](https://github.com/msterx-1/raft-algorithm/blob/main/labrpc/labrpc.go) (RPC network)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_raft["Raft protocol"]
  node_election["Leader election<br/>[raft.go]"]
  node_vote["Vote handler<br/>[raft.go]"]
  node_heartbeat["Heartbeat scheduler<br/>[raft.go]"]
  node_replication["Log replication<br/>[raft.go]"]
  node_append["Append handler<br/>[raft.go]"]
  node_commit["Commit tracking<br/>[raft.go]"]
  node_peer["Peer Raft<br/>[raft.go]"]
end

subgraph group_transport["RPC transport"]
  node_rpc["RPC network<br/>[labrpc.go]"]
end

subgraph group_apply["Application interface"]
  node_start["Submit command<br/>[raft.go]"]
  node_applyloop["Apply committed<br/>[raft.go]"]
end

node_app(("Application"))
node_consumer(("Apply consumer"))

node_app -->|"submits"| node_start
node_start -->|"schedules"| node_replication
node_election -->|"requests votes"| node_vote
node_election -->|"sends vote RPC"| node_peer
node_heartbeat -->|"schedules"| node_replication
node_heartbeat -->|"sends heartbeat"| node_peer
node_replication -->|"sends entries"| node_peer
node_peer -->|"calls RPC"| node_rpc
node_rpc -.->|"dispatches vote"| node_vote
node_rpc -.->|"dispatches entries"| node_append
node_append -->|"advances commit"| node_commit
node_replication -->|"advances commit"| node_commit
node_commit -->|"signals"| node_applyloop
node_applyloop -->|"sends apply message"| node_consumer

click node_start "https://github.com/msterx-1/raft-algorithm/blob/main/raft/raft.go"
click node_election "https://github.com/msterx-1/raft-algorithm/blob/main/raft/raft.go"
click node_vote "https://github.com/msterx-1/raft-algorithm/blob/main/raft/raft.go"
click node_heartbeat "https://github.com/msterx-1/raft-algorithm/blob/main/raft/raft.go"
click node_replication "https://github.com/msterx-1/raft-algorithm/blob/main/raft/raft.go"
click node_append "https://github.com/msterx-1/raft-algorithm/blob/main/raft/raft.go"
click node_commit "https://github.com/msterx-1/raft-algorithm/blob/main/raft/raft.go"
click node_applyloop "https://github.com/msterx-1/raft-algorithm/blob/main/raft/raft.go"
click node_peer "https://github.com/msterx-1/raft-algorithm/blob/main/raft/raft.go"
click node_rpc "https://github.com/msterx-1/raft-algorithm/blob/main/labrpc/labrpc.go"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_election,node_vote,node_heartbeat,node_replication,node_append,node_commit,node_peer toneBlue
class node_rpc toneAmber
class node_start,node_applyloop toneMint
class node_app,node_consumer toneIndigo
```

## Components

### Raft protocol

- [Leader election](https://github.com/msterx-1/raft-algorithm/blob/main/raft/raft.go): `raft/raft.go`
- [Vote handler](https://github.com/msterx-1/raft-algorithm/blob/main/raft/raft.go): `raft/raft.go`
- [Heartbeat scheduler](https://github.com/msterx-1/raft-algorithm/blob/main/raft/raft.go): `raft/raft.go`
- [Log replication](https://github.com/msterx-1/raft-algorithm/blob/main/raft/raft.go): `raft/raft.go`
- [Append handler](https://github.com/msterx-1/raft-algorithm/blob/main/raft/raft.go): `raft/raft.go`
- [Commit tracking](https://github.com/msterx-1/raft-algorithm/blob/main/raft/raft.go): `raft/raft.go`
- [Peer Raft](https://github.com/msterx-1/raft-algorithm/blob/main/raft/raft.go): `raft/raft.go`

### RPC transport

- [RPC network](https://github.com/msterx-1/raft-algorithm/blob/main/labrpc/labrpc.go): `labrpc/labrpc.go`

### Application interface

- [Submit command](https://github.com/msterx-1/raft-algorithm/blob/main/raft/raft.go): `raft/raft.go`
- [Apply committed](https://github.com/msterx-1/raft-algorithm/blob/main/raft/raft.go): `raft/raft.go`

### Other components

- **Application**
- **Apply consumer**

## Connections

- Application → Submit command: submits (evidence: `raft/raft.go`)
- Submit command → Log replication: schedules (evidence: `raft/raft.go`)
- Leader election → Vote handler: requests votes (evidence: `raft/raft.go`)
- Leader election → Peer Raft: sends vote RPC (evidence: `raft/raft.go`)
- Heartbeat scheduler → Log replication: schedules (evidence: `raft/raft.go`)
- Heartbeat scheduler → Peer Raft: sends heartbeat (evidence: `raft/raft.go`)
- Log replication → Peer Raft: sends entries (evidence: `raft/raft.go`)
- Peer Raft → RPC network: calls RPC (evidence: `raft/raft.go`)
- RPC network → Vote handler: dispatches vote
- RPC network → Append handler: dispatches entries
- Append handler → Commit tracking: advances commit (evidence: `raft/raft.go`)
- Log replication → Commit tracking: advances commit (evidence: `raft/raft.go`)
- Commit tracking → Apply committed: signals (evidence: `raft/raft.go`)
- Apply committed → Apply consumer: sends apply message (evidence: `raft/raft.go`)
