Intermediate 6 min read Updated: Aug 25, 2026

Spanning Tree Protocol (STP) Explained

Learn how STP prevents deadly Layer 2 loops in Ethernet networks.

The Spanning Tree Protocol (STP) is a Layer 2 network protocol that prevents fatal loops in a local area network (LAN). Redundancy is critical in enterprise networks—you want multiple cables connecting your switches so that if one cable fails, the network stays online. However, without STP, those redundant cables will destroy the network.

Why Does STP Exist? (The Danger of Loops)

At Layer 3, Routers use a Time To Live (TTL) mechanism to ensure packets eventually die and don't loop forever. However, at Layer 2, standard Ethernet frames have no TTL.

If you connect Switch A to Switch B with two cables, you have created a physical loop. If a PC sends a broadcast frame (like an ARP request), Switch A floods it out all ports, sending it to Switch B. Switch B receives it, floods it out all ports, and sends it back to Switch A. Switch A sends it back to Switch B.

Within seconds, this creates a Broadcast Storm. Millions of duplicate frames orbit the loop at the speed of light. The switches' CPUs spike to 100%, legitimate traffic cannot pass, the MAC address tables become corrupted, and the entire network crashes completely. This is why STP was invented.

How STP Works

STP solves this problem by logically blocking redundant links. It maps the network, finds all the loops, and shuts down specific ports (putting them in a Blocking state). If the active, unblocked cable gets severed, STP detects the failure, unblocks the backup port, and restores connectivity.

The Election Process

STP devices communicate by sending special frames called BPDUs (Bridge Protocol Data Units). The protocol follows a strict election process to build a loop-free tree:

  1. Elect a Root Bridge: The network elects one switch to be the "king" of the network, known as the Root Bridge. This is determined by the Bridge ID (Priority + MAC Address). The switch with the lowest Bridge ID wins.
  2. Find the Best Path (Root Ports): Every other switch in the network calculates the fastest path (lowest Path Cost based on bandwidth) to reach the Root Bridge. The single best port on each switch pointing toward the Root Bridge becomes the Root Port.
  3. Designate Ports: For every network segment (cable) between two switches, one port must forward traffic. The port closest to the Root Bridge becomes the Designated Port.
  4. Block the Rest: Any port that is not a Root Port or a Designated Port is put into a Blocking / Discarding state. Traffic cannot pass through it, effectively breaking the physical loop.

Simple Topology Example

Imagine three switches connected in a triangle: Switch A, Switch B, and Switch C. This is a physical loop.

  • Through BPDU exchange, Switch A is elected the Root Bridge.
  • Switch B calculates its shortest path to Switch A is via its direct cable.
  • Switch C calculates its shortest path to Switch A is via its direct cable.
  • The cable connecting Switch B to Switch C is redundant. STP will select one end of that cable and place it into a Blocking state.
  • Data flows normally. If the cable between Switch A and Switch B is cut by a contractor, STP detects the failure, transitions the blocked link between B and C to a forwarding state, and connectivity is restored.

STP vs RSTP

Original STP (802.1D) was invented in the 1980s. When a link failed, it took up to 50 seconds for STP to unblock the backup port. In modern networks, 50 seconds of downtime drops VoIP phone calls and crashes databases.

Rapid Spanning Tree Protocol (RSTP - 802.1w) is the modern standard. By improving how BPDUs are handled and changing the port states, RSTP can detect a failure and unblock a backup link in just a few milliseconds. Almost all modern networks run RSTP or a variant like MSTP.

STP Best Practices and Troubleshooting

1. Always Intentionally Choose the Root Bridge

If you leave all switch priorities at their default, the oldest, slowest switch in your network (because it has the oldest/lowest MAC address) will win the election and become the Root Bridge. This forces all traffic to flow toward your weakest switch. Always manually configure your powerful Core switches to have a priority of 0 or 4096 to guarantee they become the Root.

2. Use PortFast / Edge Port on Access Ports

By default, when you plug a PC into a switch, STP makes that port wait 30 seconds before forwarding traffic to ensure no loops exist. This prevents PCs from getting DHCP addresses quickly. You should configure all Access Ports as "Edge Ports" (or PortFast), which tells STP to bypass the 30-second wait since PCs cannot cause loops. Never enable this on a Trunk Port connecting to another switch.

3. Enable BPDU Protection (BPDU Guard)

If you enable PortFast on an Access Port, and an employee brings a cheap, unmanaged 5-port switch from home and plugs it into two wall jacks, they will create a fatal loop instantly. BPDU Guard is a security feature that immediately shuts down an Access Port if it receives a BPDU, neutralizing the rogue switch before the network crashes.

4. Never Disable STP

Unless you are running an advanced Layer 3 routed-access design or a specialized fabric, never globally disable STP. The risk of an accidental loop taking down the entire business is too high.

Quick Summary

  • STP prevents Layer 2 broadcast storms caused by redundant physical cables.
  • It elects a Root Bridge and blocks redundant paths.
  • If an active link fails, STP unblocks the backup path to restore the network.
  • RSTP is the modern, much faster version of STP.
  • Always manually configure your core switches to be the Root Bridge.
Code copied to clipboard!