Why STP exists and how loops form
Ethernet switches forward broadcasts to every port except the one the frame arrived on. Add a redundant cable between two switches and a single broadcast bounces back and forth forever, each pass multiplying as switches forward it again and again. This is called a broadcast storm, and it will saturate a 1 Gbps link in under a second.
Physical redundancy is necessary for reliability, so disabling extra cables is not a real solution. STP solves this by logically blocking one of the redundant links while keeping it physically connected. If the active link fails, STP unblocks the backup path automatically. You get both redundancy and a loop-free topology.
How STP elects the root bridge and assigns port roles
STP starts by electing a root bridge, which becomes the reference point for the entire topology. Every switch participates by sending Bridge Protocol Data Units (BPDUs) advertising its Bridge ID, which combines a configurable priority (default 32768) and the switch MAC address. The switch with the lowest Bridge ID wins. Because ties on priority go to the lowest MAC, you should always set priority manually on your intended root bridge rather than relying on MAC address luck.
Once the root bridge is elected, every other switch finds its shortest path back to the root. The port that provides that shortest path is the root port, and it stays forwarding. On each network segment, the switch closest to the root becomes the designated switch, and its port on that segment is the designated port, also forwarding. Any port that is neither a root port nor a designated port gets blocked. Blocked ports receive BPDUs but never forward data frames.
Port states matter for troubleshooting: a port starts in Blocking, moves through Listening and Learning, and finally reaches Forwarding. Classic STP takes up to 50 seconds to converge because of the 15-second forward delay timers at Listening and Learning. RSTP (802.1w) compresses this to under a second by using negotiation instead of timers.
RSTP improvements and PortFast
Rapid Spanning Tree Protocol keeps the same role concepts but replaces the slow timer-based convergence with a direct negotiation between switches. When a link comes up, adjacent switches exchange proposals and agreements. If the downstream switch confirms it has no other paths through that port, the upstream switch moves directly to Forwarding without waiting. Real-world RSTP convergence is typically under a second.
PortFast is a feature that bypasses STP states on ports connected to end devices like PCs or printers. Instead of waiting up to 30 seconds for Listening and Learning, the port goes directly to Forwarding. Only enable PortFast on access ports connected to end hosts, never between switches. Combining PortFast with BPDU Guard shuts down the port immediately if a BPDU arrives, protecting against someone accidentally connecting a switch to what should be an end-host port.
How to choose the correct answer
Root bridge election: lowest Bridge ID wins. Tie on priority goes to lowest MAC address. To control which switch is root, lower its priority below the default 32768.
Port role identification: the port facing toward the root bridge is the root port. The designated port on each segment is the one from the switch closest to the root. Everything else is either blocked or not connected.
Slow convergence or ports taking 30 seconds to forward: classic STP with forward delay timers. Sub-second convergence: RSTP (802.1w). PortFast skips STP states on access ports. BPDU Guard shuts down a PortFast port that receives a BPDU.
Loop prevention on access layer: STP or RSTP. Loop prevention on access ports connected to users: PortFast plus BPDU Guard.