-
Notifications
You must be signed in to change notification settings - Fork 254
Description
Problem
In Kademlia, the K (20 for now) closest nodes to the hash H of some content are responsible for serving the providers for that content. As described quite well in libp2p/specs#163, as the content gets more popular, these nodes get overburdened since all provider lookups converge onto them. This creates both performance & availability problems.
Solution
The kademlia paper suggests caching records like so:
For caching purposes, once a lookup succeeds, the requesting node stores the (key,value)
pair at the closest node it observed to the key that did not return the value.
Because of the unidirectionality of the topology, future searches for the same key are
likely to hit cached entries before querying the closest node.
Basically, once a node has finished fetching the providers for hash H from the network, it should store the providers on the node N closest to the hash H that did not return the providers when it was traversing the network. This expands the radius of providers & future queries for H from nodes further from H than N will discover the cached providers before they bother the K closest nodes.
This distributes provider lookups across the network & prevents them from converging on & congesting the same path.
Cache Eviction
From the kad paper:
To avoid “over-caching,” we make the expiration time of a (key,value) pair in any
node’s database exponentially inversely proportional to the number of nodes
between the current node and the node whose ID is closest to the key ID.
Basically, the further a node N is away from a node V which is the node that's closest to hash H in the whole network, the faster the expiry of the cached providers on N. This makes intuitive sense because queries for hash H from any node move closer & closer to V with each hop. So, the closer a node is to V, the higher the probability that it will be on a provider lookup path than a node that's further from V.
Question: Can't we simply use the XOR distance between N & H to determine expiry time rather than number of nodes between N & V since that is not trivial to do in our current implementation
Testing
libp2p testlab FTW !