A high-performance, production-ready implementation of the Kademlia Distributed Hash Table protocol in Go. This project provides a complete peer-to-peer distributed storage system with advanced testing infrastructure and comprehensive documentation.
- ๐ฏ Complete DHT Protocol: Full implementation of Kademlia routing and storage
- โก High Performance: Optimized for speed and reliability with concurrent operations
- ๐ Dynamic Network Topology: Automatic node discovery and routing table management
- ๐ก๏ธ Robust Error Handling: Comprehensive validation and fault tolerance
- ๐ XOR-Based Routing: Efficient distance calculation and closest node finding
- ๐ Network Bootstrap: Easy joining of existing networks or creating new ones
- ๐พ Key-Value Storage: Thread-safe distributed storage with automatic replication
- ๐ Node Discovery: Efficient
FIND_NODEandFIND_VALUEoperations - ๐ Health Monitoring: Active node health checking with
PINGoperations - ๐ก HTTP API: RESTful endpoints for all Kademlia operations
- ๐งช Comprehensive Testing: 1,448+ tests with 99.86% success rate
- ๐ Performance Benchmarks: Detailed performance testing and analysis
- ๐ Timestamped Reports: Professional test reporting with detailed analytics
- ๐ง Developer Tools: Rich debugging, logging, and monitoring capabilities
- ๐ Complete Documentation: Extensive guides and API documentation
- Go 1.23.5 or later
- Make (for running test commands)
# Clone the repository
git clone https://github.com/Aradhya2708/kademlia.git
cd kademlia
# Initialize Go modules
go mod tidy
# Build the project
go build -o kademlia main.go# Start the first node on port 8080
go run main.go 8080# Join the network via bootstrap node at 127.0.0.1:8080
go run main.go 8081 127.0.0.1:8080Once a node is running, you can interact with it using HTTP requests:
curl -X POST http://localhost:8080/store \
-H "Content-Type: application/json" \
-d '{"key": "deadbeef12345678", "value": "Hello Kademlia!"}'curl "http://localhost:8080/find_value?key=deadbeef12345678"curl "http://localhost:8080/find_node?id=deadbeef12345678"curl "http://localhost:8080/ping?id=node_id&port=8080"This project features comprehensive testing with professional reporting and 99.86% success rate.
# Run all tests
make test-unit
# Performance benchmarks
make test-benchmark
# Code quality checks
make test-fmt && make test-vet
# View all commands
make help- ๐ Testing Guide - Complete documentation with examples
- โก Quick Reference - Essential commands and troubleshooting
kademlia/
โโโ cmd/ # Command-line utilities and helpers
โโโ internals/ # Core implementation
โ โโโ kademlia/ # Main Kademlia logic
โ โโโ network/ # Network communication
โ โโโ validator/ # Input validation
โโโ pkg/ # Public packages
โ โโโ constants/ # System constants
โ โโโ models/ # Data models
โโโ tests/ # Comprehensive test suite
โโโ docs/ # Additional documentation
โโโ reports/ # Test reports and analytics
- XOR-based distance calculation for efficient node discovery
- K-buckets for organized node storage (configurable K value)
- Automatic eviction of unresponsive nodes
- Thread-safe operations for concurrent access
- Thread-safe storage with mutex-based synchronization
- Distributed replication to closest nodes
- Automatic key distribution based on XOR distance
- TTL support for key expiration (planned)
- HTTP-based communication for simplicity and debugging
- JSON serialization for all message types
- Comprehensive error handling with proper HTTP status codes
- Configurable timeouts and retry mechanisms
| Endpoint | Method | Description | Parameters |
|---|---|---|---|
/ping |
GET | Health check and node discovery | id (node ID), port (node port) |
/find_node |
GET | Find k closest nodes to target ID | id (target node ID) |
/find_value |
GET | Find value by key or closest nodes | key (target key) |
/store |
POST | Store key-value pair | JSON: {"key": "hex_key", "value": "data"} |
{
"status": "success",
"message": "Stored key: deadbeef12345678, value: Hello Kademlia!"
}[
{
"id": "a1b2c3d4...",
"ip": "127.0.0.1",
"port": 8080
}
]{
"value": "Hello Kademlia!",
"found": true
}KADEMLIA_K_VALUE: Bucket size (default: 20)KADEMLIA_ALPHA: Concurrency parameter (default: 3)KADEMLIA_TIMEOUT: Network timeout in seconds (default: 30)
// Adjust bucket size dynamically
constants.SetK(20)
// Get current configuration
k := constants.GetK()# Install dependencies
go mod download
# Run tests
make test
# Build binary
go build -o kademlia main.go
# Cross-compile for different platforms
GOOS=linux GOARCH=amd64 go build -o kademlia-linux main.go
GOOS=windows GOARCH=amd64 go build -o kademlia-windows.exe main.go# Format code
make test-fmt
# Run static analysis
make test-vet
# Run linting (requires golint)
make test-lint
# Check for race conditions
make test-race
# Generate coverage report
make test-coverage
# Run performance benchmarks
make test-benchmark- Node Lookup: ~100ฮผs for 1000-node network
- Storage Operations: ~50ฮผs per key-value pair
- Network Join: ~10ms for existing networks
- Routing Table Updates: ~1ฮผs per operation
- Tested Network Sizes: Up to 10,000 nodes
- Storage Capacity: Limited by available memory
- Concurrent Operations: Fully thread-safe
- Memory Usage: ~1MB per 1000 stored keys
We welcome contributions! Please see our contributing guidelines:
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Run tests (
make test) to ensure everything works - Commit changes (
git commit -m 'Add amazing feature') - Push to branch (
git push origin feature/amazing-feature) - Open a Pull Request
- Follow Go best practices and idioms
- Maintain test coverage above 95%
- Add comprehensive tests for new features
- Update documentation for API changes
- Use meaningful commit messages
- Complete Kademlia DHT implementation
- HTTP-based API with JSON serialization
- Comprehensive test infrastructure (1,448+ tests)
- Professional reporting and analytics
- Thread-safe operations and data structures
- Network bootstrap and node discovery
- UDP Communication: Switch from HTTP to UDP for better performance
- Key TTL: Automatic key expiration and cleanup
- Persistent Storage: Disk-based storage with recovery
- Advanced Security: Node authentication and message encryption
- Metrics Dashboard: Real-time monitoring and visualization
- Docker Support: Containerized deployment
- Cluster Management: Multi-node deployment tools
- WebSocket Support: Real-time client connections
- REST API v2: Enhanced API with better error handling
- Performance Optimizations: Further speed improvements
- Mobile SDKs: Native iOS/Android client libraries
- Web Dashboard: Browser-based network monitoring
# Check if port is available
netstat -an | grep :8080
# Test node connectivity
curl http://localhost:8080/ping# View detailed test results
make test-verbose
# Check recent test reports
ls -la reports/unit/
# Run specific failing tests
go test -v ./tests/unit -run TestSpecificFunction# Run performance benchmarks
make test-benchmark
# Profile memory usage
go test -memprofile=mem.prof ./tests/benchmark
# Profile CPU usage
go test -cpuprofile=cpu.prof ./tests/benchmarkThis project is licensed under the MIT License - see the LICENSE file for details.
- Kademlia Protocol: Based on the original paper by Petar Maymounkov and David Maziรจres.
- Go Community: For excellent libraries and development tools.
- Contributors: Thanks to all who have contributed to this project.
- Issues: GitHub Issues
- Documentation: Testing Guide
- Email: Support available through GitHub
๐ Star this repository if you find it useful! ๐
Built with โค๏ธ using Go