I have a question regarding the way IPFS finds content in the network.
I have an IPFS node running with let’s say the peer ID “ABC” and added a file to the network with the CID “XYZ”.
Now I’m trying to access the file “XYZ” through one of the public gateways. If I understood it correctly first the gateways IPFS node sends Bitswap messages to its direct peers and asks for the “XYZ” content. This will most likely fail because my node is presumably not directly connected to the gateway. As a result the gateway will fall back to query the DHT.
In a Kademlia backed DHT it is unlikely that the CID “XYZ” will have its closest distance to my node “ABC” so querying the DHT for “XYZ” will result in a peer with ID e.g. “XYW”.
I’m clearly misunderstanding something here because the public gateway is able to query the content. Did my node put the file onto the peer “XYW” or how is the network able to find my node to fetch the content?
I hope you folks can help me out.
When you publish to the DHT, you publish a record saying that ABC has the content XYZ. This record is relayed by nodes until it reaches nodes in DHT server mode close to the content. So the node XYW ends up storing the record. When the gateway contacts XYW, XYW tells it that ABC has the content. The gateway then contact ABC and fetch the content.
TL;DR, the DHT nodes doesn’t store the content next to them. They store who can provide the content next to them.
Note that the record is stored on several nodes, so if XYW is unavailable, XZZ, XYB and XYT also have the record.
On the other hand, if ABC is unavailable, the content XYZ will be unavailable unless:
- someone has requested a copy and still has it in their cache
- someone pinned the content, hence fetched a copy
- a gateway or other service prehentively fetched the file when they relayed the record linking ABC to XYZ
Remember, in IPFS, you’re not actually adding a file to the network. You’re adding it to the node to which it was uploaded (and hopefully pinned). You’re only adding the EXISTENCE of the CID to the (DHT) network. The file content itself won’t move until/unless someone asks for it.