KotobaDB Cluster
Distributed clustering and consensus for KotobaDB. Provides high availability, fault tolerance, and horizontal scalability through Raft consensus and data partitioning.
Features
- Raft Consensus: Leader election and log replication for strong consistency
- Automatic Failover: Transparent leader failover with minimal downtime
- Horizontal Scaling: Data partitioning across multiple nodes
- Fault Tolerance: Survives node failures through replication
- Eventual Consistency: Tunable consistency levels for different workloads
- gRPC Communication: Efficient protobuf-based network communication
Architecture
┌─────────────────────────────────────────┐
│ Application Layer │
├─────────────────────────────────────────┤
│ KotobaCluster High-Level API │
│ ┌─────────────────────────────────┐ │
│ │ Consensus (Raft) │ │
│ │ Membership Management │ │
│ │ Data Partitioning │ │
│ │ Replication Manager │ │
│ └─────────────────────────────────┘ │
├─────────────────────────────────────────┤
│ Network Communication Layer │
│ ┌─────────────────────────────────┐ │
│ │ gRPC Services │ │
│ │ Message Routing │ │
│ │ Connection Management │ │
│ └─────────────────────────────────┘ │
└─────────────────────────────────────────┘
Quick Start
Add to your Cargo.toml
:
[]
= "0.1.0"
Basic Cluster Setup
use *;
use ;
async
Cluster Operations
// Execute distributed operations
let operation = CreateNode ;
let result_cid = cluster.execute_operation.await?;
println!;
// Execute distributed queries
let query = MultiPartition ;
let results = cluster.execute_query.await?;
// Monitor cluster health
let status = cluster.get_status.await;
println!;
println!;
// Subscribe to cluster events
let mut events = cluster.subscribe_events.await;
while let Ok = events.recv.await
Configuration
Cluster Configuration
let config = ClusterConfig ;
Membership Configuration
let membership_config = MembershipConfig ;
Replication Configuration
let replication_config = ReplicationConfig ;
Consensus Algorithm (Raft)
How It Works
- Leader Election: Nodes elect a leader through voting
- Log Replication: Leader replicates operations to followers
- Commitment: Operations are committed when majority acknowledge
- Failover: New leader elected if current leader fails
Safety Guarantees
- Election Safety: At most one leader per term
- Leader Append-Only: Leaders never overwrite log entries
- Log Matching: Logs have consistent prefixes
- Leader Completeness: Committed entries persist through leader changes
- State Machine Safety: Operations applied in same order
Performance Characteristics
- Write Latency: 2 round trips (propose + commit)
- Read Latency: 1 round trip (from leader)
- Throughput: Limited by network and storage I/O
- Scalability: Linear with cluster size (for reads)
Data Partitioning
Consistent Hashing
Data is partitioned using consistent hashing with virtual nodes:
// Each physical node gets multiple virtual nodes on the hash ring
// This ensures even data distribution
partitioning.add_node.await?; // 100 virtual nodes
Replication Strategy
Data is replicated to N nodes based on proximity on the hash ring:
// For replication_factor = 3
let nodes = partitioning.get_nodes_for_key;
// Returns 3 closest nodes on the ring
Partition Management
// Check partition ownership
let is_owner = partitioning.is_node_responsible.await;
// Get partition statistics
let stats = partitioning.get_distribution_stats.await;
println!;
Replication & Fault Tolerance
Replication Queue
Operations are queued and replicated asynchronously:
// Queue operation for replication
replication.replicate_operation.await?;
// Check replication health
let health = replication.check_health.await;
if !health.is_healthy
Failure Handling
Automatic failure detection and recovery:
// Node failure detected
replication.handle_node_failure.await?;
// Partitions redistributed
partitioning.rebalance.await?;
Consistency Levels
Choose appropriate consistency for your use case:
- Strong Consistency: Wait for majority acknowledgment
- Eventual Consistency: Asynchronous replication
- Read-Your-Writes: Read from primary replica
Network Communication
gRPC Protocol
All communication uses efficient protobuf messages:
service ClusterService {
rpc RequestVote(VoteRequest) returns (VoteResponse);
rpc AppendEntries(AppendEntriesRequest) returns (AppendEntriesResponse);
rpc Heartbeat(HeartbeatRequest) returns (HeartbeatResponse);
rpc ExecuteOperation(ClientRequest) returns (ClientResponse);
}
Connection Management
Automatic connection handling with reconnection:
// Connect to cluster node
network.connect_to_node.await?;
// Send Raft message
network.send_raft_message.await?;
Monitoring & Observability
Cluster Metrics
let status = cluster.get_status.await;
println!;
println!;
println!;
println!;
println!;
Health Checks
// Check cluster health
let health = cluster.check_health.await;
if !health.is_healthy
Event Subscription
// Subscribe to cluster events
let mut events = cluster.subscribe_events.await;
while let Ok = events.recv.await
Deployment Patterns
Single Region Cluster
┌─────────┐ ┌─────────┐ ┌─────────┐
│ Node 1 │◄──►│ Node 2 │◄──►│ Node 3 │
│ Leader │ │ Follower│ │ Follower│
└─────────┘ └─────────┘ └─────────┘
Multi-Region Cluster
Region A Region B
┌─────────┐ ┌─────────┐ ┌─────────┐
│ Node 1 │◄──►│ Node 2 │◄──►│ Node 4 │
│ Leader │ │ Follower│ │ Follower│
└─────────┘ └─────────┘ └─────────┘
│
▼
┌─────────┐
│ Node 5 │
│ Follower│
└─────────┘
Development Setup
# Start 3-node cluster for development
&
&
&
Performance Tuning
Network Optimization
// Increase connection pool size
// Configure keep-alive settings
// Use connection multiplexing
Storage Optimization
// Tune LSM compaction settings
// Configure bloom filter sizes
// Optimize WAL sync intervals
Consensus Tuning
// Adjust election timeouts
// Configure heartbeat intervals
// Tune batch sizes
Error Handling
Common Errors
match cluster.execute_operation.await
Future Enhancements
- Multi-Raft: Multiple independent Raft groups
- Witness Nodes: Non-voting nodes for read scaling
- Dynamic Membership: Add/remove nodes without restart
- Cross-DC Replication: Geographic replication
- Query Optimization: Distributed query planning
- Backup/Restore: Cluster-wide backup utilities
Contributing
- Fork the repository
- Create a feature branch
- Add comprehensive tests
- Update documentation
- Submit a pull request
License
Licensed under the MIT License.
KotobaDB Cluster - Distributed graph database with strong consistency and high availability 🚀