Skip to content

Commit 3fce074

Browse files
committed
DHT
1 parent 370485f commit 3fce074

5 files changed

Lines changed: 491 additions & 6 deletions

File tree

secretary/dht.md

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
In-Depth Explanation of Chord and Kademlia DHTs
2+
3+
Chord and Kademlia are two of the most well-known Distributed Hash Table (DHT) implementations, widely used in decentralized networks. They both allow efficient key-value lookups in large, distributed systems, but they use different approaches for routing, lookup efficiency, and fault tolerance.
4+
5+
1️⃣ Chord – The Ring-Based DHT
6+
7+
📌 Developed by: MIT (2001)
8+
📌 Key Features:
9+
• Uses consistent hashing to distribute keys evenly.
10+
• Organizes nodes in a circular ring topology.
11+
• Supports logarithmic lookups: O(log N).
12+
• Maintains a finger table for fast lookups.
13+
14+
How Chord Works
15+
16+
Chord uses a circular ID space (usually a 160-bit or 128-bit space) where each node and key is assigned an ID based on consistent hashing (SHA-1 is often used).
17+
1. Node & Key Placement
18+
• Nodes are assigned unique IDs in a circular space.
19+
• Keys are assigned IDs and stored at the first node whose ID is greater than or equal to the key ID (successor node).
20+
2. Finger Table (Fast Lookups)
21+
• Instead of searching sequentially, Chord nodes maintain a finger table of O(log N) entries, where each entry points to a node exponentially far in the ring.
22+
• The finger table allows Chord to route queries in O(log N) hops.
23+
3. Lookup Algorithm
24+
• If a node receives a query for a key:
25+
• If it stores the key, it returns the value.
26+
• If not, it forwards the query to the closest preceding node in its finger table.
27+
• Each step cuts the search space in half (similar to binary search), reducing lookup complexity to O(log N).
28+
4. Fault Tolerance & Node Joining
29+
• Each node maintains a successor list (not just one successor) to handle failures.
30+
• When a new node joins:
31+
• It informs its successor and updates its finger table.
32+
• Existing nodes redistribute some keys to the new node.
33+
34+
Example of Chord in Action
35+
36+
Let’s assume a 5-node Chord ring with a 6-bit ID space (0-63).
37+
• Nodes: {2, 12, 24, 36, 48}
38+
• Key 25 needs to be stored → It belongs to Node 36 (first node ≥ 25).
39+
40+
Lookup Example:
41+
• A request for Key 25 starts at Node 2:
42+
• 2 forwards to 24 (from its finger table).
43+
• 24 forwards to 36 (the key owner).
44+
• Lookup completes in O(log 5) = 3 hops.
45+
46+
Advantages of Chord
47+
48+
✅ Mathematically simple and elegant.
49+
✅ Efficient lookups (O(log N)).
50+
✅ Fault-tolerant with successor lists.
51+
52+
Disadvantages of Chord
53+
54+
❌ High maintenance cost (finger table updates).
55+
❌ Higher routing overhead than Kademlia in practice.
56+
57+
2️⃣ Kademlia – The XOR-Based DHT
58+
59+
📌 Developed by: MIT (2002)
60+
📌 Key Features:
61+
• Uses XOR distance to measure node closeness.
62+
• Lookup complexity is O(log N).
63+
• Uses k-buckets for storing routing information.
64+
• Parallel lookups improve fault tolerance.
65+
66+
How Kademlia Works
67+
68+
Kademlia uses a binary tree structure instead of a ring like Chord. Nodes and keys are hashed into a 160-bit ID space (usually using SHA-1 or Keccak).
69+
1. XOR Distance Metric
70+
• The distance between two nodes is measured using the XOR metric:
71+
72+
Distance(A, B) = A ⊕ B
73+
74+
75+
• A node is “closer” to another node if the XOR result is smaller.
76+
• This allows Kademlia to structure the network as a binary tree, where closer nodes share longer common prefixes.
77+
78+
2. Routing & Lookup
79+
• Each node maintains k-buckets, which store contact information for other nodes.
80+
• Kademlia selects nodes that are geometrically closer at each hop.
81+
• Lookup queries are sent in parallel (unlike Chord’s sequential forwarding).
82+
3. k-Buckets (Efficient Storage)
83+
• Nodes maintain a list of k-nodes per distance group (organized logarithmically).
84+
• Frequently contacted nodes are kept at the front (LRU policy).
85+
• This makes Kademlia highly resilient to churn (node joins/leaves).
86+
4. Node Join & Failure Handling
87+
• When a node joins, it pings other nodes to learn about the network.
88+
• Nodes update their k-buckets dynamically (no global structure maintenance).
89+
• Lookups still work even if some nodes leave (due to redundancy in k-buckets).
90+
91+
Example of Kademlia in Action
92+
• Suppose we have nodes with IDs: {0010, 1001, 1100, 1111}.
93+
• A lookup for key 1011 follows the XOR metric:
94+
• 0010 ⊕ 1011 = 1001 → Node 1001 is closest.
95+
• 1001 ⊕ 1011 = 0010 → Node 1100 is closer.
96+
• Final step reaches key owner in O(log N).
97+
98+
Advantages of Kademlia
99+
100+
✅ More efficient routing than Chord (O(log N)).
101+
✅ Highly resistant to churn with k-buckets.
102+
✅ Parallel lookups reduce latency.
103+
104+
Disadvantages of Kademlia
105+
106+
❌ Higher storage overhead (due to k-buckets).
107+
❌ More complex than Chord.
108+
109+
🔍 Chord vs. Kademlia: A Side-by-Side Comparison
110+
111+
Feature Chord 🏛 Kademlia 🚀
112+
Lookup Complexity O(log N) O(log N)
113+
Network Structure Circular Ring XOR-based binary tree
114+
Routing Efficiency Sequential forwarding Parallel lookups
115+
Fault Tolerance Successor lists k-buckets
116+
Churn Handling Moderate Excellent
117+
Used In Research, academia BitTorrent, IPFS, Ethereum
118+
119+
Which One is Better?
120+
121+
✅ For academic research? Chord is simpler and easier to understand.
122+
✅ For real-world P2P apps? Kademlia is the winner (BitTorrent, IPFS, Ethereum).
123+
✅ For networks with high churn? Kademlia’s k-buckets provide better resilience.
124+
125+
Both DHTs are powerful, but Kademlia dominates in real-world applications due to its efficient XOR-based lookups and robustness against churn.
126+
127+
Would you like a deep dive into the implementation details (e.g., Golang examples for Chord/Kademlia)? 🚀

secretary/dynamic_serialization.md

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
Yes, you can achieve dynamic serialization and deserialization in Go using reflect combined with JSON or other formats like YAML. The idea is to define your struct metadata in JSON and use reflection to process it dynamically instead of hardcoding struct definitions.
2+
3+
Steps to Achieve This:
4+
1. Define struct metadata in JSON
5+
6+
{
7+
"a": { "type": "int", "enc": 3 },
8+
"s": { "type": "string", "size": 3 }
9+
}
10+
11+
12+
2. Parse the JSON metadata and create a dynamic struct using reflection.
13+
3. Serialize and deserialize based on the metadata.
14+
15+
Example Implementation
16+
17+
Here’s how you can dynamically process such metadata in Go:
18+
19+
package main
20+
21+
import (
22+
"encoding/json"
23+
"fmt"
24+
"reflect"
25+
)
26+
27+
type FieldInfo struct {
28+
Type string `json:"type"`
29+
Enc int `json:"enc,omitempty"`
30+
Size int `json:"size,omitempty"`
31+
}
32+
33+
func main() {
34+
jsonData := `{
35+
"a": { "type": "int", "enc": 3 },
36+
"s": { "type": "string", "size": 3 }
37+
}`
38+
39+
// Parse the JSON metadata
40+
var metadata map[string]FieldInfo
41+
err := json.Unmarshal([]byte(jsonData), &metadata)
42+
if err != nil {
43+
panic(err)
44+
}
45+
46+
// Create a dynamic struct based on metadata
47+
fields := []reflect.StructField{}
48+
for name, info := range metadata {
49+
var fieldType reflect.Type
50+
switch info.Type {
51+
case "int":
52+
fieldType = reflect.TypeOf(0)
53+
case "string":
54+
fieldType = reflect.TypeOf("")
55+
default:
56+
continue
57+
}
58+
59+
fields = append(fields, reflect.StructField{
60+
Name: name,
61+
Type: fieldType,
62+
Tag: reflect.StructTag(fmt.Sprintf(`custom:"enc=%d,size=%d"`, info.Enc, info.Size)),
63+
})
64+
}
65+
66+
// Create a struct type dynamically
67+
dynamicType := reflect.StructOf(fields)
68+
instance := reflect.New(dynamicType).Elem()
69+
70+
// Assign values dynamically
71+
instance.Field(0).SetInt(42) // Set int value
72+
instance.Field(1).SetString("abc") // Set string value
73+
74+
// Print dynamic struct values
75+
fmt.Println("Dynamically Created Struct:")
76+
for i := 0; i < dynamicType.NumField(); i++ {
77+
field := dynamicType.Field(i)
78+
fmt.Printf("%s: %v\n", field.Name, instance.Field(i).Interface())
79+
}
80+
81+
// Access struct tags
82+
fmt.Println("\nStruct Field Tags:")
83+
for i := 0; i < dynamicType.NumField(); i++ {
84+
field := dynamicType.Field(i)
85+
fmt.Printf("%s tag: %s\n", field.Name, field.Tag)
86+
}
87+
}
88+
89+
Explanation:
90+
1. The JSON metadata defines field types and encoding rules.
91+
2. Reflection is used to dynamically create a struct.
92+
3. Values are assigned dynamically, just like in a statically defined struct.
93+
4. The program prints the dynamically created struct’s values and tags.
94+
95+
Benefits:
96+
• No need to hardcode structs.
97+
• New fields can be added via JSON without modifying Go code.
98+
• Can be extended to dynamic serialization and deserialization.
99+
100+
Would you like to integrate this with your existing serialization logic?

0 commit comments

Comments
 (0)