A high-performance, thread-safe consistent hashing implementation in Go, supporting virtual nodes and weighted distribution.
- 💪 Thread-safe implementation
- 🔄 Virtual nodes support for better distribution
- ⚖️ Configurable node weights
- 🎯 Customizable hash functions
- 📊 Detailed statistics for monitoring
- 🚀 High performance with minimal memory allocations
go get github.com/game1991/consistent_hash
package main
import (
"fmt"
ch "github.com/game1991/consistent_hash"
)
func main() {
// Create a new consistent hash ring with default configuration
ring := ch.New(nil)
// Add some nodes
ring.Add("node1", "node2", "node3")
// Add a node with custom weight
ring.AddWithWeight("node4", 2)
// Get the node for a key
node := ring.Get("my-key")
fmt.Printf("Key 'my-key' maps to node: %s\n", node)
// Get multiple nodes for redundancy
nodes := ring.GetN("my-key", 2)
fmt.Printf("Key 'my-key' maps to nodes: %v\n", nodes)
}
config := &ch.Config{
Replicas: 100, // Number of virtual nodes per physical node
HashFunc: ch.NewCRC32(), // Hash function to use
}
ring := ch.New(config)
New(config *Config) *ConsistentHash
: Creates a new consistent hash ringDefaultConfig() *Config
: Returns default configuration
Add(members ...string)
: Add nodes with default weightAddWithWeight(member string, weight int) error
: Add a node with specific weightRemove(members ...string)
: Remove nodes from the ringMembers() []string
: Get all current nodes
Get(key string) string
: Get the node for a keyGetN(key string, n int) []string
: Get N nodes for a key (for redundancy)
GetStats() *Stats
: Get detailed statistics about the hash ring, including:- Total physical nodes
- Total virtual nodes
- Average weight
- Weight distribution
- Load distribution
This package implements consistent hashing with the following key features:
- Virtual Nodes: Each physical node is represented by multiple virtual nodes on the hash ring to ensure better distribution.
- Weighted Distribution: Nodes can have different weights, affecting their virtual node count and responsibility range.
- Thread Safety: All operations are thread-safe through proper mutex usage.
- Customizable Hash Function: Supports custom hash functions through the
Hasher
interface.
- Uses fixed-size byte arrays for hash calculations to minimize memory allocations
- Efficient binary search for node lookup
- Pre-allocated capacity for the hash ring to reduce reallocations
MIT License
Contributions are welcome! Please feel free to submit a Pull Request.