You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

164 lines
6.8 KiB

  1. ---
  2. marp: true
  3. ---
  4. # Kademlia
  5. <br><br><br>
  6. [@arnaucube](https://twitter.com/arnaucube)
  7. 2019-04-26
  8. ---
  9. ### Overview
  10. - nodes self sets a random unique ID (UUID)
  11. - nodes are grouped in `neighbourhoods` determined by the `node ID` distance
  12. - Kademlia uses `distance` calculation between two nodes
  13. - distance is computed as XOR (exclusive or) of the two `node ID`s
  14. ---
  15. - XOR acts as the distance function between all `node ID`s. Why:
  16. - distance between a node and itself is zero
  17. - is symmetric: distance between A to B is the same to B to A
  18. - follows `triangle inequality`
  19. - given A, B, C vertices (points) of a triangle
  20. - AB <= (AC + CB)
  21. - the distance from A to B is shorter or equal to the sum of the distance from A to C plus the distance from C to B
  22. - so, we get the shortest path
  23. ---
  24. - with that last 3 properties we ensure that XOR
  25. - captures all of the essential & important features of a "real" distance function
  26. - is simple and cheap to calculate
  27. - each search iteration comes one bit closer to the target
  28. - a basic Kademlia network with `2^n` nodes will only take `n` steps (in worst case) to find that node
  29. ---
  30. ### Routing tables
  31. - each node has a routing table, that consists of a `list` for each bit of the `node ID`
  32. - each entry holds the necessary data to locate another node
  33. - IP address, port, `node ID`, etc
  34. - each entry corresponds to a specific distance from the node
  35. - for example, node in the Nth position in the `list`, must have a differing Nth bit from the `node ID`
  36. - so, the list holds a classification of 128 distances of other nodes in the network
  37. ---
  38. - as nodes are encountered on the network, they are added to the `lists`
  39. - store and retrieval operations
  40. - helping other nodes to find a key
  41. - every node encountered will be considered for inclusion in the lists
  42. - keps network constantly updated
  43. - adding resilience to failures and attacks
  44. ---
  45. - `k-buckets`
  46. - `k` is a system wide number
  47. - every `k-bucket` is a `list` having up to `k` entries inside
  48. - example:
  49. - network with `k=20`
  50. - each node will have `lists` containing up to 20 nodes for a particular bit
  51. - possible nodes for each `k-bucket` decreases quickly
  52. - as there will be very few nodes that are that close
  53. - since quantity of possible IDs is much larger than any node population, some of the `k-buckets` corresponding to very short distances will remain empty
  54. ---
  55. - example:
  56. ![k-buckets](https://upload.wikimedia.org/wikipedia/commons/6/63/Dht_example_SVG.svg "k-buckets")
  57. - network size: 2^3
  58. - max nodes: 8, current nodes: 7
  59. - let's take 6th node (110) (black leaf)
  60. - 3 `k-buckets` for each node in the network (gray circles)
  61. - nodes 0, 1, 2 (000, 001, 010) are in the farthest `k-bucket`
  62. - node 3 (011) is not participating in the network
  63. - middle `k-bucket` contains the nodes 4 and 5 (100, 101)
  64. - last `k-bucket` can only contain node 7 (111)
  65. ---
  66. - Each node knows its neighbourhood well and has contact with a few nodes far away which can help locate other nodes far away.
  67. - Kademlia priorizes long connected nodes to remain stored in the `k-buckets`
  68. - as the nodes that have been connected for a long time in a network will probably remain connected for a long time in the future
  69. ---
  70. - when a `k-bucket` is full and a new node is discovered for that `k-bucket`
  71. - node sends a ping to the last recently seen node in the `k-bucket`
  72. - if the node is still alive, the new node is stored in a secondary list (a replacement cache)
  73. - replacement cache is used if a node in the `k-bucket` stops responding
  74. - basically, new nodes are used only when older nodes disappear
  75. ---
  76. ### Protocol messages
  77. - PING
  78. - STORE
  79. - FIND_NODE
  80. - FIND_VALUE
  81. Each `rpc` msg includes a random value from the initiator, to ensure that the response corresponds to the request
  82. ---
  83. ### Locating nodes
  84. - node lookups can proceed asynchronously
  85. - `α` denotes the quantity of simultaneous lookups
  86. - `α` tipically is 3
  87. - node initiates a FIND_NODE request to the `α` nodes in its own `k-bucket` that are closest ones to the desired key
  88. ---
  89. - when the recipient nodes receive the request, they will look in their `k-buckets` and return the `k` closest nodes to the desired key that they know
  90. - the requester will update a results list with the results (`node ID`s) that receives
  91. - keeping the `k` best ones (the `k` nodes that are closer to the searched key)
  92. - the requester node will select these `k` best results and issue the request to them
  93. - the proces is repeated again and again until get the searched key
  94. ---
  95. - iterations continue until no nodes are returned that are closer than the best previous results
  96. - when iterations stop, the best `k` nodes in the results list are the ones in the whole network that are the closest to the desired key
  97. - node information can be augmented with RTT (round trip times)
  98. - when the RTT is spended, another query can be initiated
  99. - always the query's number are <= `α` (quantity of simultaneous lookups)
  100. ---
  101. ### Locating resources
  102. - data (values) located by mapping it to a key
  103. - typically a hash is used for the map
  104. - locating data follows the same procedure as locating the closest nodes to a key
  105. - except the search terminates when a node has the requested value in his store and returns this value
  106. ---
  107. #### Data replicating & caching
  108. - values are stored at several nodes (k of them)
  109. - the node that stores a value
  110. - periodically explores the network to find the k nodes close to the key value
  111. - to replicate the value onto them
  112. - this compensates the disappeared nodes
  113. ---
  114. - avoiding "hot spots"
  115. - for popular values (might have many requests)
  116. - near nodes outside the k closest ones, store the value
  117. - this new storing is called `cache`
  118. - caching nodes will drop the value after a certain time
  119. - depending on their distance from the key
  120. - in this way the value is stored farther away from the key
  121. - depending on the quantity of requests
  122. - allows popular searches to find a storer more quickly
  123. - alleviates possible "hot spots"
  124. - not all implementations of Kademlia have these functionallities (replicating & caching)
  125. - in order to remove old information quickly from the system
  126. ---
  127. ### Joining the network
  128. - to join the net, a node must first go through a `bootstrap` process
  129. - `bootstrap` process
  130. - needs to know the IP address & port of another node (bootstrap node)
  131. - compute random unique `node ID` number
  132. - inserts the bootstrap node into one of its k-buckets
  133. ---
  134. - `bootstrap` process [...]
  135. - perform a node lookup of its own `node ID` against the bootstrap node
  136. - this populate other nodes `k-buckets` with the new `node ID`
  137. - populate the joining node `k-buckets` with the nodes in the path between that node and the bootstrap node
  138. - refresh all `k-buckets` further away than the `k-bucket` the bootstrap node falls in
  139. - this refresh is a lookup of a random key that is within that `k-bucket` range
  140. - initially nodes have one `k-bucket`
  141. - when is full, it can be split