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?
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.
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.