# sanjayur0/distributed-cache architecture

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

- Interactive diagram: https://gitdiagram.com/sanjayur0/distributed-cache
- Repository: https://github.com/sanjayur0/distributed-cache

## Overview

This is a Java distributed in-memory cache demonstrating consistent hashing across independent HTTP node processes. A client uses CacheRouter to route writes to a primary and replicas, and reads across assigned nodes for failover; nodes serve requests from local concurrent maps. The README also documents cluster launching, load-distribution and rebalancing demonstrations, and real-process failure testing. Core routing, ring, node, and JSON relationships are supported by excerpts or the README; demo implementation details were not sampled.

Main entry points: [`src/main/java/cache/CacheRouter.java`](https://github.com/sanjayur0/distributed-cache/blob/main/src/main/java/cache/CacheRouter.java) (Cache router), [`src/main/java/cache/NodeServer.java`](https://github.com/sanjayur0/distributed-cache/blob/main/src/main/java/cache/NodeServer.java) (HTTP node server), [`src/main/java/cache/HashRing.java`](https://github.com/sanjayur0/distributed-cache/blob/main/src/main/java/cache/HashRing.java) (Consistent hash ring), [`src/main/java/demo/KillNodeDemo.java`](https://github.com/sanjayur0/distributed-cache/blob/main/src/main/java/demo/KillNodeDemo.java) (Node failure demo)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_routing["Client routing"]
  node_router["Cache router<br/>[CacheRouter.java]"]
  node_json["JSON codec<br/>[JsonUtil.java]"]
end

subgraph group_nodes["Cache nodes"]
  node_node_server["HTTP node server<br/>[NodeServer.java]"]
  node_local_store[("In-memory store<br/>[NodeServer.java]")]
end

subgraph group_algorithm["Hashing algorithm"]
  node_ring["Consistent hash ring<br/>[HashRing.java]"]
end

subgraph group_demos["Demonstrations"]
  node_cluster_launcher["Cluster launcher"]
  node_rebalance_demo["Rebalancing demo"]
  node_distribution_demo["Load distribution demo"]
  node_kill_demo["Node failure demo<br/>[KillNodeDemo.java]"]
end

node_client(("Cache client"))

node_client -->|"requests"| node_router
node_router -->|"selects nodes"| node_ring
node_router -->|"sends HTTP requests"| node_node_server
node_node_server -->|"reads and writes"| node_local_store
node_router -->|"encodes and parses"| node_json
node_node_server -->|"parses and encodes"| node_json
node_cluster_launcher -->|"spawns processes"| node_node_server
node_rebalance_demo -->|"demonstrates remapping"| node_ring
node_distribution_demo -->|"demonstrates balance"| node_ring
node_kill_demo -->|"demonstrates failover"| node_router
node_kill_demo -->|"starts node processes"| node_cluster_launcher

click node_router "https://github.com/sanjayur0/distributed-cache/blob/main/src/main/java/cache/CacheRouter.java"
click node_ring "https://github.com/sanjayur0/distributed-cache/blob/main/src/main/java/cache/HashRing.java"
click node_node_server "https://github.com/sanjayur0/distributed-cache/blob/main/src/main/java/cache/NodeServer.java"
click node_local_store "https://github.com/sanjayur0/distributed-cache/blob/main/src/main/java/cache/NodeServer.java"
click node_json "https://github.com/sanjayur0/distributed-cache/blob/main/src/main/java/util/JsonUtil.java"
click node_cluster_launcher "https://github.com/sanjayur0/distributed-cache/blob/main/src/main/java/demo/ClusterLauncher.java"
click node_rebalance_demo "https://github.com/sanjayur0/distributed-cache/blob/main/src/main/java/demo/DemoRebalancing.java"
click node_distribution_demo "https://github.com/sanjayur0/distributed-cache/blob/main/src/main/java/demo/DemoLoadDistribution.java"
click node_kill_demo "https://github.com/sanjayur0/distributed-cache/blob/main/src/main/java/demo/KillNodeDemo.java"

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_router,node_json toneBlue
class node_node_server,node_local_store,node_client toneAmber
class node_ring toneMint
class node_cluster_launcher,node_rebalance_demo,node_distribution_demo,node_kill_demo toneRose
```

## Components

### Client routing

- [Cache router](https://github.com/sanjayur0/distributed-cache/blob/main/src/main/java/cache/CacheRouter.java): `src/main/java/cache/CacheRouter.java`
- [JSON codec](https://github.com/sanjayur0/distributed-cache/blob/main/src/main/java/util/JsonUtil.java): `src/main/java/util/JsonUtil.java`

### Cache nodes

- [HTTP node server](https://github.com/sanjayur0/distributed-cache/blob/main/src/main/java/cache/NodeServer.java): `src/main/java/cache/NodeServer.java`
- [In-memory store](https://github.com/sanjayur0/distributed-cache/blob/main/src/main/java/cache/NodeServer.java): `src/main/java/cache/NodeServer.java`

### Hashing algorithm

- [Consistent hash ring](https://github.com/sanjayur0/distributed-cache/blob/main/src/main/java/cache/HashRing.java): `src/main/java/cache/HashRing.java`

### Demonstrations

- [Cluster launcher](https://github.com/sanjayur0/distributed-cache/blob/main/src/main/java/demo/ClusterLauncher.java): `src/main/java/demo/ClusterLauncher.java`
- [Rebalancing demo](https://github.com/sanjayur0/distributed-cache/blob/main/src/main/java/demo/DemoRebalancing.java): `src/main/java/demo/DemoRebalancing.java`
- [Load distribution demo](https://github.com/sanjayur0/distributed-cache/blob/main/src/main/java/demo/DemoLoadDistribution.java): `src/main/java/demo/DemoLoadDistribution.java`
- [Node failure demo](https://github.com/sanjayur0/distributed-cache/blob/main/src/main/java/demo/KillNodeDemo.java): `src/main/java/demo/KillNodeDemo.java`

### Other components

- **Cache client**

## Connections

- Cache client → Cache router: requests (evidence: `README.md`)
- Cache router → Consistent hash ring: selects nodes (evidence: `src/main/java/cache/CacheRouter.java`)
- Cache router → HTTP node server: sends HTTP requests (evidence: `src/main/java/cache/CacheRouter.java`)
- HTTP node server → In-memory store: reads and writes (evidence: `src/main/java/cache/NodeServer.java`)
- Cache router → JSON codec: encodes and parses (evidence: `src/main/java/cache/CacheRouter.java`)
- HTTP node server → JSON codec: parses and encodes (evidence: `src/main/java/cache/NodeServer.java`)
- Cluster launcher → HTTP node server: spawns processes (evidence: `README.md`)
- Rebalancing demo → Consistent hash ring: demonstrates remapping (evidence: `README.md`)
- Load distribution demo → Consistent hash ring: demonstrates balance (evidence: `README.md`)
- Node failure demo → Cache router: demonstrates failover (evidence: `README.md`)
- Node failure demo → Cluster launcher: starts node processes (evidence: `README.md`)
