# sachin-bansal94/distribute-kv-store architecture

> Architecture diagram of the GitHub repository sachin-bansal94/distribute-kv-store, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-10-05). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/sachin-bansal94/distribute-kv-store
- Repository: https://github.com/sachin-bansal94/distribute-kv-store

## Overview

This repository contains a distributed key-value store alongside an earlier standalone TCP server. The main workflow is client command → primary processing → synchronous replication to healthy backups → local apply and WAL persistence → client response; reads are served locally. A background monitor probes and catches up replicas. Backup nodes enforce sequence ordering and persist replicated writes. The README documents the broader protocol and recovery behavior; source excerpts show the storage, WAL, command, and socket-handling implementations, but not every primary-server path.

Main entry points: [`primary_node.cpp`](https://github.com/sachin-bansal94/distribute-kv-store/blob/main/primary_node.cpp) (Command processing), [`backup_node.cpp`](https://github.com/sachin-bansal94/distribute-kv-store/blob/main/backup_node.cpp) (Backup protocol), [`kv_server_week4.cpp`](https://github.com/sachin-bansal94/distribute-kv-store/blob/main/kv_server_week4.cpp) (Standalone TCP server)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_primary["Primary node"]
  node_primary_server["Primary server<br/>[primary_node.cpp]"]
  node_primary_commands["Command processing<br/>[primary_node.cpp]"]
  node_primary_lru["Primary LRU store<br/>[primary_node.cpp]"]
  node_primary_wal[("Primary WAL<br/>[primary_node.cpp]")]
end

subgraph group_replication["Replication and recovery"]
  node_replication_manager["Replication manager<br/>[primary_node.cpp]"]
  node_health_monitor["Health monitor<br/>[primary_node.cpp]"]
end

subgraph group_backup["Backup nodes"]
  node_backup_a["Backup node<br/>[backup_node.cpp]"]
  node_backup_commands["Backup protocol<br/>[backup_node.cpp]"]
  node_backup_lru["Backup LRU store<br/>[backup_node.cpp]"]
  node_backup_wal[("Backup WAL<br/>[backup_node.cpp]")]
end

subgraph group_standalone["Standalone server"]
  node_legacy_server["Standalone TCP server"]
  node_legacy_commands["Standalone commands"]
  node_legacy_lru["Standalone LRU store"]
end

node_client(("Client"))

node_client -->|"sends commands"| node_primary_server
node_primary_server -->|"dispatches"| node_primary_commands
node_primary_commands -->|"replicates writes"| node_replication_manager
node_replication_manager -->|"sends SEQWRITE"| node_backup_a
node_primary_commands -->|"applies writes"| node_primary_lru
node_primary_commands -->|"appends writes"| node_primary_wal
node_primary_commands -->|"serves reads"| node_primary_lru
node_health_monitor -->|"probes and syncs"| node_backup_a
node_health_monitor -->|"replays entries"| node_primary_wal
node_backup_a -->|"handles connections"| node_backup_commands
node_backup_commands -->|"applies writes"| node_backup_lru
node_backup_commands -->|"persists writes"| node_backup_wal
node_client -->|"sends commands"| node_legacy_server
node_legacy_server -->|"dispatches"| node_legacy_commands
node_legacy_commands -->|"reads and writes"| node_legacy_lru

click node_primary_server "https://github.com/sachin-bansal94/distribute-kv-store/blob/main/primary_node.cpp"
click node_primary_commands "https://github.com/sachin-bansal94/distribute-kv-store/blob/main/primary_node.cpp"
click node_primary_lru "https://github.com/sachin-bansal94/distribute-kv-store/blob/main/primary_node.cpp"
click node_primary_wal "https://github.com/sachin-bansal94/distribute-kv-store/blob/main/primary_node.cpp"
click node_replication_manager "https://github.com/sachin-bansal94/distribute-kv-store/blob/main/primary_node.cpp"
click node_health_monitor "https://github.com/sachin-bansal94/distribute-kv-store/blob/main/primary_node.cpp"
click node_backup_a "https://github.com/sachin-bansal94/distribute-kv-store/blob/main/backup_node.cpp"
click node_backup_commands "https://github.com/sachin-bansal94/distribute-kv-store/blob/main/backup_node.cpp"
click node_backup_lru "https://github.com/sachin-bansal94/distribute-kv-store/blob/main/backup_node.cpp"
click node_backup_wal "https://github.com/sachin-bansal94/distribute-kv-store/blob/main/backup_node.cpp"
click node_legacy_server "https://github.com/sachin-bansal94/distribute-kv-store/blob/main/kv_server_week4.cpp"
click node_legacy_commands "https://github.com/sachin-bansal94/distribute-kv-store/blob/main/kv_server_week4.cpp"
click node_legacy_lru "https://github.com/sachin-bansal94/distribute-kv-store/blob/main/kv_server_week4.cpp"

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_primary_server,node_primary_commands,node_primary_lru,node_primary_wal,node_client toneBlue
class node_replication_manager,node_health_monitor toneAmber
class node_backup_a,node_backup_commands,node_backup_lru,node_backup_wal toneMint
class node_legacy_server,node_legacy_commands,node_legacy_lru toneRose
```

## Components

### Primary node

- [Primary server](https://github.com/sachin-bansal94/distribute-kv-store/blob/main/primary_node.cpp): `primary_node.cpp`
- [Command processing](https://github.com/sachin-bansal94/distribute-kv-store/blob/main/primary_node.cpp): `primary_node.cpp`
- [Primary LRU store](https://github.com/sachin-bansal94/distribute-kv-store/blob/main/primary_node.cpp): `primary_node.cpp`
- [Primary WAL](https://github.com/sachin-bansal94/distribute-kv-store/blob/main/primary_node.cpp): `primary_node.cpp`

### Replication and recovery

- [Replication manager](https://github.com/sachin-bansal94/distribute-kv-store/blob/main/primary_node.cpp): `primary_node.cpp`
- [Health monitor](https://github.com/sachin-bansal94/distribute-kv-store/blob/main/primary_node.cpp): `primary_node.cpp`

### Backup nodes

- [Backup node](https://github.com/sachin-bansal94/distribute-kv-store/blob/main/backup_node.cpp): `backup_node.cpp`
- [Backup protocol](https://github.com/sachin-bansal94/distribute-kv-store/blob/main/backup_node.cpp): `backup_node.cpp`
- [Backup LRU store](https://github.com/sachin-bansal94/distribute-kv-store/blob/main/backup_node.cpp): `backup_node.cpp`
- [Backup WAL](https://github.com/sachin-bansal94/distribute-kv-store/blob/main/backup_node.cpp): `backup_node.cpp`

### Standalone server

- [Standalone TCP server](https://github.com/sachin-bansal94/distribute-kv-store/blob/main/kv_server_week4.cpp): `kv_server_week4.cpp`
- [Standalone commands](https://github.com/sachin-bansal94/distribute-kv-store/blob/main/kv_server_week4.cpp): `kv_server_week4.cpp`
- [Standalone LRU store](https://github.com/sachin-bansal94/distribute-kv-store/blob/main/kv_server_week4.cpp): `kv_server_week4.cpp`

### Other components

- **Client**

## Connections

- Client → Primary server: sends commands (evidence: `README.md`)
- Primary server → Command processing: dispatches (evidence: `README.md`)
- Command processing → Replication manager: replicates writes (evidence: `README.md`)
- Replication manager → Backup node: sends SEQWRITE (evidence: `README.md`)
- Command processing → Primary LRU store: applies writes (evidence: `README.md`)
- Command processing → Primary WAL: appends writes (evidence: `README.md`)
- Command processing → Primary LRU store: serves reads (evidence: `primary_node.cpp`)
- Health monitor → Backup node: probes and syncs (evidence: `README.md`)
- Health monitor → Primary WAL: replays entries (evidence: `README.md`)
- Backup node → Backup protocol: handles connections (evidence: `backup_node.cpp`)
- Backup protocol → Backup LRU store: applies writes (evidence: `backup_node.cpp`)
- Backup protocol → Backup WAL: persists writes (evidence: `backup_node.cpp`)
- Client → Standalone TCP server: sends commands (evidence: `kv_server_week4.cpp`)
- Standalone TCP server → Standalone commands: dispatches (evidence: `kv_server_week4.cpp`)
- Standalone commands → Standalone LRU store: reads and writes (evidence: `kv_server_week4.cpp`)
