Skip to content
JL/
All journal entries
JournalNetworkingAugust 19, 20267 min read

How Spanning Tree Prevents Layer 2 Loops

Redundancy helps keep networks available when links or devices fail, but redundant Layer 2 paths can also create switching loops. In this journal, I explain how Spanning Tree Protocol uses BPDUs, a root bridge, path costs, and port roles to keep redundant links available without allowing frames to loop endlessly through the network.

Why Redundancy Is Important

I've realized how critical availability is for an organization to function. In an enterprise network, even a short period of downtime means lost operational time, lower productivity, unavailable services, and a hit to customer trust.

To reduce this risk, organizations implement redundancy. Instead of relying on only one network path, additional links are available as backups. If the main path fails, another path can take over, so users can continue accessing the network.

However, redundancy introduces another problem: Layer 2 loops.

When Redundancy Becomes a Problem

Imagine three switches connected in a triangle.

When a device needs to find the MAC address for an IPv4 address, it sends an ARP request. Since that request is a broadcast, the switch floods the frame out to all its other interfaces.

Normally, this is not a problem.

But with redundant links, another switch can receive that same broadcast and flood it again. The third switch does the same, potentially sending the frame back toward the switch that originally forwarded it.

There is no TTL field in an Ethernet frame that eventually causes the frame to expire. Because of this, a Layer 2 frame can continue circulating through the redundant paths.

If you aren't careful, you end up with a broadcast storm, where traffic just keeps multiplying and eating up all your network resources. Switching loops are a headache too, since they cause duplicate frames and mess with the stability of your MAC address tables.

So redundancy gives us availability, but without some way of controlling the redundant paths, it can also make the network unusable.

Spanning Tree Protocol

To solve this problem, IEEE standardized Spanning Tree Protocol (STP) as part of IEEE 802.1D.

The main idea behind STP is simple:

Keep the redundant links, but do not allow all of them to forward traffic at the same time.

STP creates a loop-free logical topology by allowing the paths that are needed to forward traffic while placing unnecessary redundant paths into a blocking state. If an active path later fails, STP can recalculate the topology and allow one of the redundant paths to begin forwarding.

This means we do not have to physically remove redundancy just to prevent loops.

Electing the Root Bridge

Before STP can decide which links should forward or block traffic, the switches first need a common point of reference.

This is the root bridge.

I think of the root bridge as the logical center of the spanning-tree topology. The other switches determine their best path toward this switch, and those paths influence which interfaces will forward traffic and which ones will be blocked.

Switches exchange Bridge Protocol Data Units, or BPDUs, containing information such as bridge IDs, path costs, and port information. STP uses these BPDUs to build the topology.

Initially, switches participate in the election and determine which switch has the best Bridge ID.

The switch with the lowest Bridge ID becomes the root bridge.

The Bridge ID is primarily based on:

Bridge priority + MAC address

A lower priority value is preferred. If switches have the same priority, the lower MAC address acts as the tie-breaker.

After the root bridge is elected, I had to figure out which interfaces should actually forward traffic.

How I Learned to Track STP Port Selection

STP works by having each switch figure out the best path to the root bridge. From there, every non-root switch picks its own Root Port.

The Root Port is the interface that gives a switch its best path to the root bridge, which is usually decided by the lowest total STP path cost.

For example:

             ROOT
              SW1
             /   \
           /       \
         SW2-------SW3

I needed to make sure both SW2 and SW3 had a path to SW1. I assumed their direct connections to SW1 would have the lowest cost.

I realized those interfaces become their Root Ports.

             SW1
          DP     DP
          /       \
        RP         RP
       SW2--------SW3 

I learned that the root bridge doesn't have a Root Port; instead, all its active ports are Designated Ports.

Root Port

A Root Port (RP) is the best path from a non-root switch toward the root bridge. There can only be one Root Port per switch for that spanning-tree instance.

Designated Port

STP also chooses a Designated Port (DP) for each network segment. The Designated Port is the interface that provides the best path from that segment toward the root bridge. Root Ports and Designated Ports are part of the active topology and are allowed to forward traffic.

This leaves the redundant connection between SW2 and SW3. Both interfaces cannot forward normally, because that would recreate our triangle and therefore the Layer 2 loop.

STP compares the information being advertised on that link. One side becomes the Designated Port, while the other redundant interface is placed into a blocking role.

             SW1
          DP     DP
          /       \
        RP         RP
       SW2--------SW3
        DP        BLOCK

We still physically have the triangle.

But logically, STP has turned it into something closer to this:

             SW1
             / \
            /   \
          SW2   SW3

The redundant link still exists. It simply isn't forwarding normal traffic.

That is the part that finally made STP click for me: STP does not remove redundancy. It temporarily removes redundant paths from the active Layer 2 topology.

How Does STP Decide the Best Path?

STP uses path cost to determine how good a path toward the root bridge is.

Every link has a cost associated with it, and the switch calculates the total cost required to reach the root.

Lower cost wins.

So if a switch can reach the root through:

Path A → Cost 4
Path B → Cost 8

Path A is preferred.

If the costs are equal, STP can use information such as Bridge IDs and Port IDs as tie-breakers.

The important part for me is not necessarily memorizing every tie-breaker immediately. What matters is understanding the decision process:

Find the root → find the lowest-cost paths toward it → forward on the necessary paths → block the redundant ones.

This is where redundancy becomes useful again. Suppose the connection between SW3 and the root bridge fails:

             SW1
          DP      X
          /
        RP
       SW2--------SW3

Even if SW3 has lost its original path toward the root, the physical connection between SW2 and SW3 is still there.

STP detects the topology change and recalculates the network. If that previously blocked path is now needed, it can become part of the forwarding topology.

The network might then logically become:

             SW1
             |
            SW2
             |
            SW3

This is exactly why the redundant link was never removed.

During normal operation:

Redundant link → Blocked

After a failure:

Redundant link → Available as another path

So STP is balancing two things that initially seem to conflict with each other:

Prevent loops while still keeping redundancy.

BPDU and STP Convergence

All of these decisions are made using BPDUs.

BPDUs allow switches to communicate information about the root bridge, their path toward the root, and the ports participating in STP. When better information is received, switches can update their understanding of the topology.

Eventually, the switches agree on a stable, loop-free topology. This process is called convergence.

With classic 802.1D STP, ports can transition through states such as:

Blocking
   ↓
Listening
   ↓
Learning
   ↓
Forwarding

Instead of immediately allowing a redundant interface to forward traffic, STP gives the network time to determine whether doing so would introduce another loop.

Modern networks commonly use faster versions such as Rapid Spanning Tree Protocol (RSTP / 802.1w), but understanding traditional STP first makes the newer versions much easier to understand because the fundamental idea remains the same: elect a root, determine the best paths, and prevent redundant paths from creating loops.

What I Took From STP

At first, redundancy sounded simple to me: just add another link, so there is a backup if the main one fails.

STP made me understand why it is not that simple.

Adding redundant Layer 2 connections without controlling them can create loops that are potentially worse than the failure the redundant links were supposed to protect against.

STP solves this by creating a logical tree on top of the physical network. It elects a root bridge, calculates the best paths toward it, allows the necessary Root and Designated Ports to forward traffic, and blocks the extra paths.

If the topology changes, those redundant links are still available.

So the way I understand STP now is:

Keep multiple physical paths for availability, but create only one loop-free logical path for traffic to follow.

That makes redundancy useful instead of dangerous.