# harsha19-08/distributeddb architecture

> Architecture diagram of the GitHub repository harsha19-08/distributeddb, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-08-15). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/harsha19-08/distributeddb
- Repository: https://github.com/harsha19-08/distributeddb

## Overview

#### Purpose and entry points
DistributedDB is a Java 17, Dynamo-inspired distributed key-value store: consistent-hash routing, replicated quorum reads/writes, gossip membership, WAL-backed local storage, and Prometheus metrics. Each node starts from `src/main/java/com/ddb/node/NodeServer.java` with a node JSON configuration such as `config/node1-local.json`. Client and load-generation entry points are `src/main/java/com/ddb/client/CliClient.java` and `src/main/java/com/ddb/bench/Benchmark.java`.

#### RPC boundary and node composition
Protocol Buffers define three gRPC surfaces: client-facing KV operations, internal replica operations, and gossip exchange in `src/main/proto/ddb.proto`. `src/main/java/com/ddb/node/NodeServer.java` constructs the node’s shared ring, membership, storage, quorum coordinator, metrics, and gRPC server. `src/main/java/com/ddb/node/KVServiceImpl.java` accepts GET/PUT/DELETE from clients; `src/main/java/com/ddb/node/ReplicaServiceImpl.java` performs local replica reads and durable replica writes.

#### Routing and quorum coordination
`src/main/java/com/ddb/hash/HashRing.java` maps a key to a clockwise preference list of distinct physical nodes, using configured virtual nodes. `src/main/java/com/ddb/quorum/QuorumCoordinator.java` is the distributed request control point: it fans writes to N replicas and completes after W acknowledgements; reads query replicas until R responses, then select the latest timestamped value. Configuration controls replication factor and R/W quorum values through `src/main/java/com/ddb/node/NodeConfig.java`.

#### Local durability and conflict boundary
Replica RPCs terminate at `src/main/java/com/ddb/storage/StorageEngine.java`, which combines an in-memory key map, bounded LRU cache, and persistent log. `src/main/java/com/ddb/storage/WriteAheadLog.java` appends and fsyncs writes before acknowledgement and replays records at startup. `src/main/java/com/ddb/storage/VersionedValue.java` represents timestamped values and tombstones; last-write-wins reconciliation prevents stale replica updates from replacing newer data.

#### Membership and ring healing
`src/main/java/com/ddb/gossip/GossipManager.java` periodically exchanges heartbeats and membership information with peers over the gossip RPC service. `src/main/java/com/ddb/gossip/MembershipState.java` owns ALIVE/SUSPECT/DEAD transitions and recovery. Membership changes feed the hash-ring boundary: dead nodes are removed from routing, while recovered nodes are restored, allowing coordinators to select live replica preferences.

#### Observability and deployment
`src/main/java/com/ddb/metrics/NodeMetrics.java` exports per-node request, latency, cache, WAL, membership, ring, and replication metrics over HTTP for Prometheus. `prometheus/prometheus.yml` scrapes the cluster. `docker-compose.yml` and `Dockerfile` provide the material deployment topology: five database nodes plus Prometheus; Docker-oriented node configurations are under `config/node1.json` through `config/node5.json`.

#### Primary flow
A client sends a KV gRPC request to any node. The KV service delegates to quorum coordination, which consults the current hash ring and calls replica gRPC endpoints in parallel. Each replica service reads from or commits to its local storage engine; writes become durable through the WAL before its acknowledgement. The coordinator returns after configured quorum completion, reconciling read responses by version/tombstone timestamp. Independently, gossip maintains live membership and consequently the routing ring.

Main entry points: [`src/main/proto/ddb.proto`](https://github.com/harsha19-08/distributeddb/blob/main/src/main/proto/ddb.proto) (gRPC Protocol), [`src/main/java/com/ddb/quorum/QuorumCoordinator.java`](https://github.com/harsha19-08/distributeddb/blob/main/src/main/java/com/ddb/quorum/QuorumCoordinator.java) (Quorum Coordinator), [`src/main/java/com/ddb/node/NodeServer.java`](https://github.com/harsha19-08/distributeddb/blob/main/src/main/java/com/ddb/node/NodeServer.java) (Node Server), [`src/main/java/com/ddb/node/ReplicaServiceImpl.java`](https://github.com/harsha19-08/distributeddb/blob/main/src/main/java/com/ddb/node/ReplicaServiceImpl.java) (Replica Service)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_access["Clients &amp; API"]
  node_cli["CLI Client<br/>[CliClient.java]"]
  node_benchmark["Benchmark<br/>load generator<br/>[Benchmark.java]"]
  node_proto["gRPC Protocol<br/>protobuf contract<br/>[ddb.proto]"]
end

subgraph group_node["Database Node"]
  node_node_server["Node Server<br/>node composition<br/>[NodeServer.java]"]
  node_kv_service["KV Service<br/>client RPC service<br/>[KVServiceImpl.java]"]
  node_quorum["Quorum Coordinator<br/>request coordinator"]
  node_replica_service["Replica Service<br/>internal RPC service"]
  node_storage[("Storage Engine<br/>local storage<br/>[StorageEngine.java]")]
  node_wal[("Write-Ahead Log<br/>durability log<br/>[WriteAheadLog.java]")]
  node_version["Versioned Value<br/>conflict model"]
  node_node_config["Node Config<br/>[NodeConfig.java]"]
  node_metrics["Node Metrics<br/>Prometheus exporter<br/>[NodeMetrics.java]"]
end

subgraph group_cluster["Cluster Control"]
  node_gossip["Gossip Manager<br/>membership exchange<br/>[GossipManager.java]"]
  node_membership["Membership State<br/>failure detector"]
  node_ring(("Hash Ring<br/>consistent-hash routing<br/>[HashRing.java]"))
end

subgraph group_ops["Operations"]
  node_prometheus["Prometheus<br/>metrics collector<br/>[prometheus.yml]"]
  node_compose["Five-Node Deployment<br/>Docker Compose topology<br/>[docker-compose.yml]"]
  node_dockerfile["Node Container Image<br/>Docker image"]
end

node_cli -->|"uses KV RPC"| node_proto
node_benchmark -->|"uses KV RPC"| node_proto
node_proto -->|"client operations"| node_kv_service
node_proto -->|"replica operations"| node_replica_service
node_proto -->|"gossip exchange"| node_gossip
node_node_server -->|"hosts"| node_kv_service
node_node_server -->|"hosts"| node_replica_service
node_node_server -->|"exports"| node_metrics
node_node_config -->|"N, R, W"| node_quorum
node_kv_service -->|"GET / PUT / DELETE"| node_quorum
node_quorum -->|"selects live replicas"| node_ring
node_quorum -->|"parallel replica RPCs"| node_replica_service
node_quorum -->|"reconciles reads"| node_version
node_replica_service -->|"local reads and writes"| node_storage
node_storage -->|"append before acknowledgement"| node_wal
node_storage -->|"applies LWW updates"| node_version
node_gossip -->|"updates heartbeats"| node_membership
node_membership -->|"adds or removes live nodes"| node_ring
node_prometheus -->|"scrapes HTTP metrics"| node_metrics
node_compose -->|"runs five nodes"| node_node_server
node_compose -->|"runs collector"| node_prometheus
node_dockerfile -->|"builds node image"| node_compose

click node_cli "https://github.com/harsha19-08/distributeddb/blob/main/src/main/java/com/ddb/client/CliClient.java"
click node_benchmark "https://github.com/harsha19-08/distributeddb/blob/main/src/main/java/com/ddb/bench/Benchmark.java"
click node_proto "https://github.com/harsha19-08/distributeddb/blob/main/src/main/proto/ddb.proto"
click node_node_server "https://github.com/harsha19-08/distributeddb/blob/main/src/main/java/com/ddb/node/NodeServer.java"
click node_kv_service "https://github.com/harsha19-08/distributeddb/blob/main/src/main/java/com/ddb/node/KVServiceImpl.java"
click node_quorum "https://github.com/harsha19-08/distributeddb/blob/main/src/main/java/com/ddb/quorum/QuorumCoordinator.java"
click node_replica_service "https://github.com/harsha19-08/distributeddb/blob/main/src/main/java/com/ddb/node/ReplicaServiceImpl.java"
click node_storage "https://github.com/harsha19-08/distributeddb/blob/main/src/main/java/com/ddb/storage/StorageEngine.java"
click node_wal "https://github.com/harsha19-08/distributeddb/blob/main/src/main/java/com/ddb/storage/WriteAheadLog.java"
click node_version "https://github.com/harsha19-08/distributeddb/blob/main/src/main/java/com/ddb/storage/VersionedValue.java"
click node_node_config "https://github.com/harsha19-08/distributeddb/blob/main/src/main/java/com/ddb/node/NodeConfig.java"
click node_metrics "https://github.com/harsha19-08/distributeddb/blob/main/src/main/java/com/ddb/metrics/NodeMetrics.java"
click node_gossip "https://github.com/harsha19-08/distributeddb/blob/main/src/main/java/com/ddb/gossip/GossipManager.java"
click node_membership "https://github.com/harsha19-08/distributeddb/blob/main/src/main/java/com/ddb/gossip/MembershipState.java"
click node_ring "https://github.com/harsha19-08/distributeddb/blob/main/src/main/java/com/ddb/hash/HashRing.java"
click node_prometheus "https://github.com/harsha19-08/distributeddb/blob/main/prometheus/prometheus.yml"
click node_compose "https://github.com/harsha19-08/distributeddb/blob/main/docker-compose.yml"
click node_dockerfile "https://github.com/harsha19-08/distributeddb/blob/main/Dockerfile"

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_cli,node_benchmark,node_proto toneBlue
class node_node_server,node_kv_service,node_quorum,node_replica_service,node_storage,node_wal,node_version,node_node_config,node_metrics toneAmber
class node_gossip,node_membership,node_ring toneMint
class node_prometheus,node_compose,node_dockerfile toneRose
```

## Components

### Clients & API

- [CLI Client](https://github.com/harsha19-08/distributeddb/blob/main/src/main/java/com/ddb/client/CliClient.java): client · `src/main/java/com/ddb/client/CliClient.java`
- [Benchmark](https://github.com/harsha19-08/distributeddb/blob/main/src/main/java/com/ddb/bench/Benchmark.java): load generator · `src/main/java/com/ddb/bench/Benchmark.java`
- [gRPC Protocol](https://github.com/harsha19-08/distributeddb/blob/main/src/main/proto/ddb.proto): protobuf contract · Defines KV, replica, and gossip RPCs. · `src/main/proto/ddb.proto`

### Database Node

- [Node Server](https://github.com/harsha19-08/distributeddb/blob/main/src/main/java/com/ddb/node/NodeServer.java): node composition · Builds shared node services and hosts gRPC. · `src/main/java/com/ddb/node/NodeServer.java`
- [KV Service](https://github.com/harsha19-08/distributeddb/blob/main/src/main/java/com/ddb/node/KVServiceImpl.java): client RPC service · `src/main/java/com/ddb/node/KVServiceImpl.java`
- [Quorum Coordinator](https://github.com/harsha19-08/distributeddb/blob/main/src/main/java/com/ddb/quorum/QuorumCoordinator.java): request coordinator · Completes writes at W and reads at R responses. · `src/main/java/com/ddb/quorum/QuorumCoordinator.java`
- [Replica Service](https://github.com/harsha19-08/distributeddb/blob/main/src/main/java/com/ddb/node/ReplicaServiceImpl.java): internal RPC service · `src/main/java/com/ddb/node/ReplicaServiceImpl.java`
- [Storage Engine](https://github.com/harsha19-08/distributeddb/blob/main/src/main/java/com/ddb/storage/StorageEngine.java): local storage · Owns memory map, LRU cache, and persistent log. · `src/main/java/com/ddb/storage/StorageEngine.java`
- [Write-Ahead Log](https://github.com/harsha19-08/distributeddb/blob/main/src/main/java/com/ddb/storage/WriteAheadLog.java): durability log · Fsyncs writes and replays on startup. · `src/main/java/com/ddb/storage/WriteAheadLog.java`
- [Versioned Value](https://github.com/harsha19-08/distributeddb/blob/main/src/main/java/com/ddb/storage/VersionedValue.java): conflict model · Timestamped values and tombstones support LWW reconciliation. · `src/main/java/com/ddb/storage/VersionedValue.java`
- [Node Config](https://github.com/harsha19-08/distributeddb/blob/main/src/main/java/com/ddb/node/NodeConfig.java): node configuration · Sets replication and R/W quorum values. · `src/main/java/com/ddb/node/NodeConfig.java`
- [Node Metrics](https://github.com/harsha19-08/distributeddb/blob/main/src/main/java/com/ddb/metrics/NodeMetrics.java): Prometheus exporter · `src/main/java/com/ddb/metrics/NodeMetrics.java`

### Cluster Control

- [Gossip Manager](https://github.com/harsha19-08/distributeddb/blob/main/src/main/java/com/ddb/gossip/GossipManager.java): membership exchange · Periodically exchanges peer heartbeats and membership. · `src/main/java/com/ddb/gossip/GossipManager.java`
- [Membership State](https://github.com/harsha19-08/distributeddb/blob/main/src/main/java/com/ddb/gossip/MembershipState.java): failure detector · Manages ALIVE, SUSPECT, DEAD, and recovery transitions. · `src/main/java/com/ddb/gossip/MembershipState.java`
- [Hash Ring](https://github.com/harsha19-08/distributeddb/blob/main/src/main/java/com/ddb/hash/HashRing.java): consistent-hash routing · Selects clockwise live replica preferences across virtual nodes. · `src/main/java/com/ddb/hash/HashRing.java`

### Operations

- [Prometheus](https://github.com/harsha19-08/distributeddb/blob/main/prometheus/prometheus.yml): metrics collector · `prometheus/prometheus.yml`
- [Five-Node Deployment](https://github.com/harsha19-08/distributeddb/blob/main/docker-compose.yml): Docker Compose topology · Runs five database nodes and Prometheus. · `docker-compose.yml`
- [Node Container Image](https://github.com/harsha19-08/distributeddb/blob/main/Dockerfile): Docker image · `Dockerfile`

## Connections

- CLI Client → gRPC Protocol: uses KV RPC
- Benchmark → gRPC Protocol: uses KV RPC
- gRPC Protocol → KV Service: client operations
- gRPC Protocol → Replica Service: replica operations
- gRPC Protocol → Gossip Manager: gossip exchange
- Node Server → KV Service: hosts
- Node Server → Replica Service: hosts
- Node Server → Node Metrics: exports
- Node Config → Quorum Coordinator: N, R, W
- KV Service → Quorum Coordinator: GET / PUT / DELETE
- Quorum Coordinator → Hash Ring: selects live replicas
- Quorum Coordinator → Replica Service: parallel replica RPCs
- Quorum Coordinator → Versioned Value: reconciles reads
- Replica Service → Storage Engine: local reads and writes
- Storage Engine → Write-Ahead Log: append before acknowledgement
- Storage Engine → Versioned Value: applies LWW updates
- Gossip Manager → Membership State: updates heartbeats
- Membership State → Hash Ring: adds or removes live nodes
- Prometheus → Node Metrics: scrapes HTTP metrics
- Five-Node Deployment → Node Server: runs five nodes
- Five-Node Deployment → Prometheus: runs collector
- Node Container Image → Five-Node Deployment: builds node image
