A terminal-based Python simulator for exploring consistent hashing with virtual nodes and replica placement. It demonstrates how keys are distributed across a cluster of servers, how adding or removing a server affects ownership, and how replica servers are selected.
- Consistent hashing over a configurable hash space
- Virtual nodes per server to improve distribution
- Replica placement based on successor virtual nodes
- In-memory storage for primary and replica key/value data
- Interactive command-line interface for simulating cluster operations
This project models a distributed key-value cluster using consistent hashing. Instead of mapping each server directly onto the hash ring, it uses virtual nodes so data is spread more evenly across the ring.
When a key is stored:
- its hash token is computed,
- the responsible server is determined from the ring,
- the key is stored on the primary server and on replica servers according to the configured replication factor.
- Python 3.9+
From the project root, run:
python main.pyYou will enter an interactive shell prompt:
Cluster>
Use help to view the available commands.
help- Show command usageadd-server <server-name>- Add a new server to the clusterremove-server <server-name>- Remove an existing serverput <key> <value>- Store a key/value pairget <key>- Retrieve a value by keydelete <key>- Delete a keyshow-cluster- Show cluster membership and storage summaryshow-ring- Display the hash ring contentsshow-server <server-name>- Show details for a specific servershow-replica <key>- Show the replica chain for a keyexit- Exit the simulator
Cluster> add-server server1
Cluster> add-server server2
Cluster> put user1 Alice
Cluster> get user1
Cluster> show-ring
Cluster> show-replica user1
Cluster> exit
The simulator starts with the following defaults in main.py:
- Replication factor: 3
- Virtual nodes per server: 10
- Hash space: 1,000,000
.
├── cli/
│ ├── commands.py
│ ├── input_handler.py
│ └── shell.py
├── cluster/
│ ├── cluster_manager.py
│ ├── hash_ring.py
│ ├── hashing.py
│ ├── migration_manager.py
│ ├── replica_manager.py
│ └── virtual_node_generator.py
├── models/
│ ├── cluster_config.py
│ ├── key_value.py
│ ├── physical_server.py
│ └── virtual_node.py
├── storage/
│ └── memory_store.py
├── main.py
- A server is added to the cluster and assigned virtual nodes on the hash ring.
- Keys are hashed to a token.
- The token is mapped to the next server on the ring.
- The key is stored on the primary server and on additional replica servers.
- If a server is removed, data is rebalanced and ownership changes accordingly.
This project is intended as a learning and visualization tool for understanding consistent hashing, virtual nodes, and basic replication behavior in distributed systems.