# Data Model: Raft Core Replication ## Entities - **Peer** - Fields: `id (u64)`, `region_id (u64)`, `state (Leader/Follower/Candidate)`, `term (u64)`, `commit_index (u64)`, `last_applied (u64)` - Relationships: owns `RaftStorage`; exchanges `RaftLogEntry` with other peers. - Constraints: single region scope for this phase; fixed voter set of 3. - **RaftLogEntry** - Fields: `index (u64)`, `term (u64)`, `command (bytes)`, `context (bytes, optional)` - Relationships: persisted in `raft_log` CF; applied to state machine when committed. - Constraints: indices strictly increasing; term monotonic per election; applied in order. - **HardState** - Fields: `current_term (u64)`, `voted_for (u64)`, `commit_index (u64)` - Relationships: persisted in `raft_state` CF; loaded at startup before participating. - Constraints: must be flushed atomically with log appends when advancing commit index. - **ConfState** - Fields: `voters (Vec)` - Relationships: persisted in `raft_state` CF; defines quorum (majority of 3). - Constraints: static for this phase; changes require future joint consensus. - **ReplicationState** - Fields: `match_index (u64)`, `next_index (u64)`, `pending (bool)` - Relationships: maintained per follower in memory; not persisted. - Constraints: drives AppendEntries backoff and progress. ## State Transitions - Peer transitions: Follower → Candidate → Leader on election; Leader → Follower on higher term or failed election. - Log application: when `commit_index` advances, apply entries in order to state machine; `last_applied` increases monotonically. - Recovery: on restart, load `HardState`, `ConfState`, and log; reconcile with leader via AppendEntries (truncate/append) before applying new entries.