Why we need swarm and how does it work?

Hey guys, I am a new comer to IPFS and curious about some concepts and mechanisms. Here are my questions:
#1 what is swarm in IPFS? I have searched this term by Google. The answer is “swarm” is a kind of DHT and is widely used in Ethereum. But as far as I know, IPFS uses Kademlia as its DHT algorithm. I guess that swarm in IPFS is far different from the “swarm” resulted from Google.
#2 From the documents and experience of using Kubo, I gradually know IPFS swarm is a swarm of peers we currently establish a connection to. But why IPFS needs to maintain such a swarm, I have no idea.
#3 How does swarm work? Especially in peering, how does it observe peers and make a decision in connnecting to a peer and disconnecting from a peer?
You help will be appreciated, I know these questions would be very tedious and annoying, thank you for your patience.

It’s just a way of referring to the IPFS public p2p network, unrelated to Ethereum in that they are two different networks. But both are built using libp2p.

Because for a peer-to-peer application to work, a peer needs to be connected to other peers so that they can provide and obtain value to and from the network. In this case the main service the network provide to its peers is routing (knowing how to find content and how to get to the peers hosting it).

You can probably dive-in here: https://docs.libp2p.io/. There are several ways of finding peers. One is DHT lookups, but can also get peer information from indexes. Peers that you need for something (i.e. downloading something) are ranked higher than peers that are not needed for anything or actively doing something with you. That way some connections are kept and others are closed, when there are too many.

Hitherto I have thought k-buckets(the concept in Kademlia) handle these tasks. According to the context you wrote, I guess that the peers in IPFS swarm are exactly the peers in k-buckets. Am I right?

Yes, k-buckets from the dht would usually define who you are connected too, but not always. You can also have LAN peers, or peers that you are connected to for other reasons (because you are uploading or downloading). The DHT is just one of several “services” that an IPFS peer is running, although one of the main ones.

Ok, thank you for your explanation, I gradually know it. Do you mean the peers in k-buckets is a subset of the peers listed by ‘ipfs swarm peers’?