Spanning Tree is the protocol that keeps a looped Layer-2 network from destroying itself. Ethernet frames have no TTL, so a single bridging loop does not slow a network down — it melts it, in seconds. STP’s job is to find every redundant path and deliberately block it, leaving exactly one loop-free tree, then unblock a backup the moment the active path fails. This walk-through goes from why the loop is fatal to RSTP and the guards that keep the tree where you put it.
1. Why a Layer-2 loop is fatal
An IP packet has a TTL that decrements at each hop and eventually dies. An Ethernet frame has none. Put two switches in a loop and a single broadcast circulates forever, multiplied at every switch, until the links saturate — a broadcast storm. Worse, the same source MAC arrives on two ports, so the MAC table flaps and unicast forwarding breaks too. There is no self-healing; the loop must be prevented, not survived.
2. Electing the root bridge
STP builds a tree rooted at one switch. Every switch starts claiming to be root and advertises its Bridge ID — a 2-byte priority (default 32768) plus its MAC address — in BPDUs. The switch with the lowest Bridge ID wins; since priorities are usually equal, the lowest MAC wins by default — which is why you set priority explicitly rather than letting the oldest switch become root by accident.
3. Each switch picks a root port
Once the root is known, every non-root switch chooses one root port — its best path back to the root, the one with the lowest cumulative root path cost. Cost accumulates per link by speed, so a switch compares the total cost over each of its uplinks and keeps the cheapest. Ties break on the neighbor’s Bridge ID, then its port ID.
4. Each segment picks a designated port
Every link segment also needs exactly one designated port — the port responsible for forwarding toward the root on that segment, chosen as the one advertising the lowest cost. All of the root bridge’s ports are designated. Any port that is neither a root port nor a designated port is put into blocking — that is the port STP shuts down to break the loop.
5. Port states and the slow timers
Classic STP does not forward immediately. A port climbs through Blocking → Listening → Learning → Forwarding, spending a forward delay (15 s) in each of listening and learning so it cannot create a transient loop while the topology settles. With a hello of 2 s and a max-age of 20 s, a failure can take 30–50 seconds to recover — glacial by modern standards, and the reason RSTP exists.
6. RSTP — convergence in milliseconds
Rapid STP (802.1w) keeps the same tree math but replaces the timers with a handshake. It adds alternate and backup port roles — a pre-computed backup to the root port and to a designated port — so a failover is a role switch, not a 30-second wait. A proposal/agreement exchange lets two switches agree to forward instantly, and edge ports (host-facing) forward immediately. Convergence drops from tens of seconds to sub-second.
7. Path cost — the table that decides the tree
Because the whole tree hangs on cumulative cost, knowing the per-link values is what lets you predict and shape it. The legacy (short) scale tops out low, so modern links collapse together; the long path-cost method spreads them out. Set costs deliberately on key links rather than accepting defaults that may send traffic the long way round.
8. Guarding the tree
A correct tree can still be broken by a misbehaving or malicious switch, so STP ships with protections. PortFast forwards edge ports immediately; BPDU Guard err-disables a PortFast port the instant it hears a BPDU (someone plugged a switch into an access port); Root Guard rejects a superior BPDU on a port so a rogue switch cannot steal the root role; Loop Guard keeps a port blocking if BPDUs mysteriously stop, rather than letting it drift to forwarding and form a loop.
Takeaways
Spanning Tree is one idea applied relentlessly: pick a root, measure the cost to it, keep one path, block the rest, and switch to a backup on failure. RSTP makes the switch fast; the guards make sure the tree stays where you designed it. Understand the Bridge ID, the cost table, and the port roles, and nothing STP does will surprise you.
Generic, vendor-neutral fundamentals from my own study and lab work. No client- or employer-specific configuration.