STP (Spanning Tree Protocol) is an extremely important protocol in Layer-2 networks. We will not cover how to configure it on a switch, but we will just look at why this is an important protocol.
When we build our networks we like to provide redundancy. We like to use redundancy because if one link or device fails it provides another path for data to traverse to reach the destination. This can create problems in a Layer-2 network as we have now created loops! Recall how switches operate and learn a MAC Address. With a loop in the network the initial pack will be duplicated over and over and over again flooding the layer-2 network if unneeded traffic (this is just one example and other example is a broadcast frame). This will flood the network and make the network unusable. You might be thinking doesn't data packets have a TTL (time-to-live) field? And shouldn't that be decremented at each switch? That field is in layer-3, so with being in layer-2 it does not come in to play!
STP solves this problem! STP works by figuring out the best path and breaking the loop. STP uses the link speed (if not configured) to help determine the cost and calculate the total cost of each path.
A 10-Mbps link has a default cost of 100.
A 100-Mbps link has a default cost of 19.
A 1000-Mbps link has a default cost of 4.
A 10-Gbps link has a default cost of 2.
Spanning Tree adds up the total cost for each path to determine the best path. It can then disable an interface to break the loop. If a device or link goes down STP recalculates the best path and can reenable an interface if needed. STP helps our layer-2 networks provide redundancy and be self healing.
This video provides a great explanation of why we use STP in our networks. The video is about 9 minutes, so please make sure to watch this.