# Session 20: JetStream Cluster & Raft

## Summary

Raft consensus algorithm implementation and JetStream clustering — how streams and consumers are replicated across server nodes.

## Scope

| Go File | Features | Feature IDs | Go LOC |
|---------|----------|-------------|--------|
| server/raft.go | 198 | 2599–2796 | 4,078 |
| server/jetstream_cluster.go | 231 | 1520–1750 | 10,098 |
| **Total** | **429** | | **14,176** |

## .NET Classes

- `RaftNode` — Raft consensus implementation (169 features)
- `AppendEntry`, `AppendEntryResponse` — Raft log entries
- `Checkpoint` — Raft snapshots
- `CommittedEntry`, `Entry`, `EntryType` — entry types
- `VoteRequest`, `VoteResponse`, `RaftState` — election types
- `RaftGroup` — Raft group configuration
- `JetStreamCluster` — cluster-wide JetStream coordination (51 features)
- `Consumer` (cluster) — consumer assignment tracking (7 features)
- `ConsumerAssignment` — consumer placement
- `StreamAssignment`, `UnsupportedStreamAssignment` — stream placement
- Plus ~69 `JetStreamEngine` methods for cluster operations

## Test Files

| Test File | Tests | Test IDs |
|-----------|-------|----------|
| server/raft_test.go | 104 | 2616–2719 |
| server/jetstream_cluster_1_test.go | 151 | 757–907 |
| server/jetstream_cluster_2_test.go | 123 | 908–1030 |
| server/jetstream_cluster_3_test.go | 97 | 1031–1127 |
| server/jetstream_cluster_4_test.go | 85 | 1128–1212 |
| server/jetstream_cluster_long_test.go | 7 | 1213–1219 |
| server/jetstream_super_cluster_test.go | 47 | 1419–1465 |
| server/jetstream_meta_benchmark_test.go | 2 | 1416–1417 |
| server/jetstream_sourcing_scaling_test.go | 1 | 1418 |
| **Total** | **617** | |

## Dependencies

- Session 01 (Foundation Types)
- Session 08 (Client Connection)
- Session 09 (Server Core Part 1)
- Session 14 (Routes)
- Session 17 (Store Interfaces)
- Session 19 (JetStream Core)

## .NET Target Location

- `dotnet/src/ZB.MOM.NatsNet.Server/JetStream/Cluster/`
- `dotnet/src/ZB.MOM.NatsNet.Server/Raft/`

## Notes

- **This is a multi-sitting session** — 14.2K Go LOC and 617 tests (the largest session)
- Suggested sub-batching:
  - **20a**: Raft types and election (entries, votes, state — ~30 features)
  - **20b**: Raft core (log replication, append, commit — ~85 features)
  - **20c**: Raft remaining (snapshots, checkpoints, recovery — ~83 features)
  - **20d**: JetStream cluster types and assignments (~30 features)
  - **20e**: JetStream cluster operations Part 1 (~130 features)
  - **20f**: JetStream cluster operations Part 2 (~71 features)
  - **20g**: Tests (617 tests, batched by test file)
- Raft is the most algorithmically complex code in the server
- Cluster tests often require multi-server setups — integration test candidates
