How DHT decides from which node to retrieve resource?

I have a general(maybe dumb) question about how IPFS decides from which node to get a resource.
User 1(India) adds a resource
User 2(USA, NY) gets and pins that resource
User 3(USA, Washington, D.C) wants that resource.

User 3 will get the resource from which node?
Does IPFS take into account the proximity?


IPFS white paper says:

IPFS nodes require a routing system that can find (a) other peers’ network addresses and (b) peers who can serve particular objects. IPFS achieves this using a DSHT based on S/Kademlia and Coral

But currently the implementation in IPFS is Kademlia.

Kademlia is a little complex, not easy to describe in short.
You can refer to
Or video, it describes “Pastry DHT algorithm”, similiar to “Kademlia DHT algorithm”

In Draft 3 Juan Benet says the IPFS uses a mix of Coral and S/Kademlia but I was wondering if there are multiple eligible nodes that have the resource, it takes into account the proximity, the bandwith, etc?

The DHT is used to advertise and find peers that can provide requested pieces of content. But unless I’m mistaken, bitswap is a significant part of determining which peers are involved in fulfilling a given request.

The bitswap spec documentation seems to be in flux, but for reference here it is.

The implementation used in go-ipfs is here:


As I understand the way bitswap works right now is as follows (maybe someone can confirm this):
Alice publishes to network a want list of blocks. Then other users participating in the exchange start transmitting data (bytes) to Alice. When the transmission finishes Alices calculates the Hash of the data received and check if it matches any of the blocks in her want list. The problem is that as the number of users increases then multiple users send Alice the same block. As a result, Alice spends her bandwidth to download the same block multiple times causing the average time required to download the file to increase.
IPFS Performance · Issue #5226 · ipfs/go-ipfs · GitHub

Since it’s related to DHT…

Let’s say we have node A, B, C.
‘A’ looks for a hash: A–>B–>C (has this hash).

‘B’ will update his own DHT with the pair (hash, NodeID_C) ?