Distributed hash tables: Chord and Pastry
Clear, exam-focused notes comparing the distributed hash table designs Chord and Pastry: identifiers, routing state, routing algorithm, guarantees, and worked routing examples.
Drafted with Aria, reviewed by the AiCanCode.org team · Updated . How we write and check our notes. Spotted an error? Use Give Feedback at the bottom of the page.
Why it matters
Distributed Hash Tables (DHTs) are used by engineers to build large-scale peer-to-peer systems that map keys (files, objects, service names) to nodes without a central server. Chord and Pastry are two influential, scalable DHT designs that give fast lookups, fault-tolerance and decentralised membership — useful when designing distributed storage, content distribution, and decentralized services.
Key ideas
Start with definitions and first principles.
Identifier space: an ordered circular space of integers [0, 2^m - 1] (clockwise ring). Every node and every key gets a unique identifier by applying a hash function. The integer width m is chosen so collisions are very unlikely.
Consistent hashing: A hash function H maps arbitrary keys to the identifier space. For a key k, its identifier is id(k) = H(k) mod 2^m. This spreads keys uniformly and minimises remapping when nodes join/leave.
Successor of an identifier x: the node whose identifier is the first node at or clockwise after x. Draw a circle labeled 0 to 2^m - 1 clockwise; place node IDs; to find successor(x) start at x and move clockwise until you meet a node; label that node "successor(x)".
Predecessor of a node: the node immediately counter-clockwise before it on the ring.
Routing state: information stored at each node to forward messages toward the node responsible for a key. Good routing state balances small memory per node and small number of hops.
Chord (definition, invariants, and algorithm):
Chord: a DHT that arranges nodes on the identifier circle and uses a finger table for routing.
Finger table definition (Chord): For a node with identifier n (0 <= n < 2^m), the ith finger entry (1 <= i <= m) has start = (n + 2^{i-1}) mod 2^m and points to successor(start). So entry i stores the node that is the first node clockwise at or after start. Draw a circle; mark n; draw arrows from n to the successor of n+1, n+2, n+4, n+8 etc. Label each arrow with i and start value.
Routing rule (Chord): To lookup key k at node n, if k is between n (exclusive) and successor(n) (inclusive) then successor(n) (the successor pointer of n, i.e., the next node clockwise) is responsible. Otherwise forward the query to the closest preceding node in the finger table whose ID is between n and k (i.e., the finger with largest ID in (n, k)). Repeat until you reach the successor. In Chord, the node responsible for key k is successor(k), so keys are stored at successor(k).
Correctness invariant (Chord): Each node maintains a correct successor pointer; stabilization protocols (periodically inform successor and update predecessor pointers) repair the ring under joins/failures. In practice, each node also keeps a successor list of r consecutive successors (e.g., r = O(log N)) to tolerate up to r−1 consecutive node failures.
Complexity (Chord): With high probability, a lookup uses O(log N) hops where N is the number of nodes. Each node’s finger table has m entries (m is the identifier width in bits); with high probability only O(log N) of these point to distinct nodes, so per-node routing state is O(log N).
Pastry (definition, data structures, and algorithm):
Pastry: a DHT that uses prefix-based routing in a base 2^b digit space, and maintains three structures: a routing table, a leaf set (numerically closest nodes, used for correctness/final delivery), and a separate neighborhood set (nodes close in the underlying network, used for locality).
NodeId and digit representation (Pastry): NodeIds are treated as fixed-length base 2^b digits (for example b=4 gives hex digits). Let D be the number of digits. Each nodeId is a sequence of D digits; two nodeIds share a prefix of length l if their first l digits match.
Routing table (Pastry): Organized in D rows (one per prefix length). Row l contains entries for nodes whose nodeId shares the first l digits with this node, but whose (l+1)th digit is each possible value except the local digit. So row l has up to (2^b - 1) entries. Each entry helps change the (l+1)th digit to route closer to the destination by one digit.
Leaf set (Pastry): A set of L/2 numerically closest smaller nodeIds and L/2 numerically closest larger nodeIds (total L nodes). The leaf set is used when the destination is numerically close; it ensures delivery even if the routing table does not contain an exact match.
Routing rule (Pastry): To route a message with key k, at node n: if k is within the range covered by n's leaf set, forward directly to the node in the leaf set closest to k. Otherwise, forward to a routing-table entry that shares a prefix with k longer than n's prefix with k (i.e., increases the prefix match by at least one digit). If no such entry exists, forward to a node in the routing table or leaf set whose nodeId is numerically closer to k than n is. This converges in O(D) hops; with base 2^b, expected hops are O(log_{2^b} N) = O(log N / b).
Fault tolerance and joining (both systems): Both Chord and Pastry use background protocols where joining nodes initialize routing state by contacting existing nodes, and periodic stabilization/gossip fixes incorrect pointers after failures.
Exact statements students should remember (define before using):
- For Chord: finger i of node n points to successor((n + 2^{i-1}) mod 2^m).
- For Pastry: routing table has D rows (D = identifier length in digits) and row l contains entries for all digit choices for position l+1 except the node's own digit.
Assumptions: uniform random distribution of nodeIds (via good hash) and independent node joins/failures. These give the O(log N) average-case bounds.
Formulas
identifier = H(key) mod 2^m — H is a cryptographic hash (e.g., SHA-1), m is identifier width in bits; gives an identifier in [0, 2^m - 1].
finger_start(i, n) = (n + 2^(i-1)) mod 2^m — n is node ID (integer), i ∈ {1..m}; start value whose successor fills finger i.
Chord_lookup_hops = O(log N) — expected number of hops to find a key in a network of N nodes, assuming finger tables correct with high probability.
Pastry_lookup_hops = O(log_{2^b} N) = O((log N) / b) — b is bits per digit (base 2^b), N is number of nodes; D = identifier length / b digits.
Routing_table_size_Pastry ≈ D * (2^b - 1) — number of routing table entries per node (some entries may be empty); D = number of digits.
Units: identifiers are dimensionless integers; H produces bits. These are algorithmic formulas, not SI units.
Worked examples
Example 1 — Chord routing trace (exam standard): Parameters: m = 4 (IDs 0..15). Existing node IDs: 1, 3, 7, 12. Build finger table for node 3 and lookup key 14.
- Compute finger starts for node 3: i=1: start=(3+1)=4; i=2: start=(3+2)=5; i=3: start=(3+4)=7; i=4: start=(3+8)=11.
- For each start, find successor(start) among node IDs {1,3,7,12}:
- successor(4) is 7 (first node clockwise after 4).
- successor(5) is 7.
- successor(7) is 7.
- successor(11) is 12.
- Finger table of node 3 (i: start -> node): 1:4->7, 2:5->7, 3:7->7, 4:11->12.
- Lookup key k = 14 starting at node 3. Is 14 in (3, successor(3)=7]? No (7 < 14). Choose closest preceding finger < 14: candidates 7 and 12; 12 is closest preceding. Forward to node 12.
- At node 12, successor(12) = 1 (wrap-around). Is 14 in (12, 1]? Yes because interval (12..1] covers 13,14,15,0,1; so successor(12) = 1 is responsible for 14. So route: 3 -> 12 -> 1. Hops = 2. Answer: the key 14 is stored at node 1; lookup takes 2 hops. (This is an exam-style step-by-step trace.)
Example 2 — Pastry routing trace (small base): Parameters: base 4 (b = 2 bits per digit), D = 3 digits so IDs are 000..333 (base-4 digits 0..3). Node IDs in base-4: A=012, B=031, C=110, D=233, E=300. Leaf set size L = 4 (2 smaller + 2 larger) — (note: verify L default in your course). Routing: route key k = 321 from node C = 110.
- Compare prefix with k: C (110) vs k (321): prefix match length = 0 (first digit differs).
- Since k is not within C's leaf-set numeric range, use routing table row 0 to select a node whose first digit = 3 (k's first digit). Suppose routing row 0 has an entry for first-digit 3 pointing to E=300. Forward to E.
- At E=300, prefix match with 321 is 1 digit (first digit 3 matches). Use row 1 to find a node whose second digit = 2; in our node set no entry with prefix 32 exists. Fall back to the numerically closer choice from E’s routing/leaf sets; 321 is numerically closest to E=300 among the given nodes, so routing terminates at E. Route: C -> E. (In larger networks, each hop typically increases the shared prefix by one digit; expected hops ≈ D = 3 or log_{4} N.) This trace shows how each hop increases the shared prefix by at least one digit.
(If an instructor expects exact routing-table entries, draw the routing table rows for each node and show the chosen entry.)
Common mistakes
- For Chord: claiming worst-case lookup is O(N) always — the average / high-probability bound is O(log N) given good distribution and maintained fingers.
- For Pastry: mixing up leaf set and routing table roles (leaf set is for local numerical closeness and final delivery; routing table increases common prefix).
- Using base 10 intuition for Pastry digits; use base 2^b digits.
- Forgetting stabilization and failure-repair protocols, which are essential for correctness under churn.
For your exam
Typical question types:
- Definitions and short notes: define DHT, consistent hashing, successor/predecessor, finger table, leaf set.
- Derivations: show how Chord's finger table leads to O(log N) hops (explain halving property), or derive Pastry hop bound O(log_{2^b} N).
- Traces: given a small ring and node IDs, draw finger tables or routing tables and show step-by-step lookup.
- Compare-and-contrast: differences in routing (prefix-based vs. power-of-two fingers), advantages (Pastry: locality support; Chord: simpler), and typical uses.
- Diagrams: draw the identifier circle (label 0..2^m-1), place nodes, show finger arrows for a chosen node; for Pastry draw routing table rows and leaf set.
What a full-marks answer includes:
- Clear definitions of terms used (identifier space, successor, predecessor, finger table, routing table, leaf set).
- Correct formulas (finger start formula for Chord; digit/row description for Pastry).
- Correct step-by-step routing or derivation with intervals and modular arithmetic shown explicitly.
- Mention assumptions (uniform hashing, periodic stabilization) and complexity statements with reason (why O(log N)).
Quick check
- In Chord, what is the start value for the 3rd finger of node n?
- What does Pastry use to make each routing hop bring the message closer: prefix match increase, leaf set, or random walk?
- Give the general expected hop count for Chord in terms of N.
Answers: 1.
start = (n + 2^(3-1)) mod 2^m = (n + 4) mod 2^m. 2. Prefix match increase (routing table) — leaf set is used when numerically close. 3. O(log N).
Finished this topic? Mark it so your progress, study plan and readiness keep up.
Stuck on something here?