Skip to main content
ExplainerBGP RoutingExplainer· 5 min read· in Guides

Path Vector Routing: How BGP Selects the Optimal Route Based on AS-Path Length, Local Preference, and MED

The Border Gateway Protocol relies on a strict, sequential algorithm to determine the best path for internet traffic. By evaluating attributes like Local Preference, AS-Path length, and the Multi-Exit Discriminator, routers enforce business policies over raw network speed.

By Hui Lin

Transit Providers 40%Enterprise Edge Administrators 35%Protocol Standards Authors 25%
Transit Providers
Prioritize Local Preference to enforce business agreements and minimize transit costs.
Enterprise Edge Administrators
Focus on AS-Path prepending and MED to ensure redundancy and load balancing.
Protocol Standards Authors
Advocate for deterministic, loop-free routing algorithms that prioritize global stability.

Perspectives this story doesn't cover

  • Cloud Content Delivery Networks
  • Internet Exchange Point Operators

Summary

  1. BGP uses a strict, sequential algorithm to select the best path for internet traffic.
  2. Local Preference is the primary attribute used to control outbound traffic within an Autonomous System.
  3. AS-Path length counts the number of networks a route traverses, with the shortest path preferred.
  4. The Multi-Exit Discriminator (MED) influences inbound traffic from a directly connected neighboring network.
  5. Proprietary metrics like Cisco's Weight can override standard BGP attributes at the local router level.

Inside a data center in Ashburn, Virginia, a core router receives three competing advertisements for the exact same block of IP addresses. In a fraction of a millisecond, the machine must choose exactly one path to forward traffic. It does not measure the latency of the fiber-optic cables, nor does it calculate the available bandwidth on the links. Instead, it runs a strict, sequential elimination algorithm defined by the Border Gateway Protocol (BGP).[1]

BGP is the protocol that connects the global internet, operating reliably over TCP port 179. "BGP uses TCP as its transport protocol. This eliminates the need to implement explicit update fragmentation, retransmission, acknowledgement, and sequencing," the Internet Engineering Task Force (IETF) notes in the standard. Unlike interior gateway protocols such as OSPF, which map the exact state of every link in a network, BGP is a path-vector protocol. It views the internet as a series of interconnected Autonomous Systems (ASes)—independent networks operated by internet service providers, enterprises, and universities.[1]

When a router receives multiple paths to the same destination, it places them in its Routing Information Base (RIB) and begins the Phase 2 Decision Process. The IETF codified this process in January 2006 with the publication of RFC 4271. The standard dictates that the router must evaluate a specific set of path attributes in a rigid order, dropping less preferred routes at each step until only one remains.[1]

"The function that calculates the degree of preference for a given route SHALL NOT use any of the following as its inputs: the existence of other routes, the non-existence of other routes, or the path attributes of other routes," the IETF authors wrote in RFC 4271. This ensures that the selection process is entirely deterministic and based solely on the attributes attached to the specific route being evaluated.[1]

The standard BGP path selection sequence evaluates attributes in a rigid, deterministic order.

In practice, the first attribute evaluated is often proprietary. Cisco injects a metric called "Weight" at the very top of the sequence. Weight is a locally significant integer that never leaves the router. The path with the highest weight wins immediately, allowing an administrator to force a specific router to prefer a specific link regardless of what the rest of the network thinks.[2]

If the weights are equal, or if the router strictly follows the IETF standard, the first official tie-breaker is Local Preference (LOCAL_PREF). This is a well-known, discretionary attribute that is shared among all internal BGP (iBGP) peers within a single Autonomous System. The path with the highest Local Preference wins. Network operators heavily rely on this metric to enforce business policies, such as preferring a free peering link over a paid transit link.[1][3]

If the weights are equal, or if the router strictly follows the IETF standard, the first official tie-breaker is Local Preference (LOCAL_PREF).

If multiple paths share the same Local Preference, the router evaluates the AS-Path length. The AS-Path attribute is a sequence of Autonomous System numbers that the route has traversed. The router simply counts the number of AS hops and prefers the shortest path. A segment can hold a maximum of 255 ASes before it requires a new sequence block.[1][4]

Network administrators frequently manipulate this step using a technique called AS-Path prepending. By artificially adding their own AS number multiple times to an outbound route advertisement, they make the path look artificially long to the rest of the internet. This discourages inbound traffic from taking that specific link, effectively using a technical metric to achieve a traffic-engineering goal.[1]

If the AS-Path lengths are identical, the router looks at the Origin code. This attribute indicates how the route was originally injected into BGP. The algorithm prefers routes generated by an Interior Gateway Protocol (IGP) over those learned via the obsolete Exterior Gateway Protocol (EGP), and prefers both over routes marked as "Incomplete" (typically redistributed from another protocol).[1]

The next tie-breaker is the Multi-Exit Discriminator (MED). While Local Preference controls outbound traffic, MED is designed to influence inbound traffic from a directly connected neighboring AS. If an enterprise connects to the same internet service provider in both New York and Los Angeles, it can send a lower MED value from the New York router to signal that the ISP should prefer that entry point. The lowest MED value wins.[1][5]

Different BGP attributes are used to influence outbound versus inbound traffic flows.

However, the MED comparison is only valid if the competing routes were received from the exact same neighboring Autonomous System. If the routes come from different ISPs, the router skips the MED comparison entirely unless explicitly configured otherwise. This prevents a distant network from dictating routing policy across multiple independent transit providers.[5]

If the routes are still tied, the router prefers paths learned via external BGP (eBGP) over those learned via internal BGP (iBGP). This rule ensures that traffic exits the local Autonomous System as quickly as possible, a concept known as "hot-potato routing." By handing the packet off to a neighboring network at the nearest exit, the local network conserves its own internal bandwidth.[1][2]

For routes that remain tied—often because they were learned from two different internal routers—the algorithm evaluates the IGP cost to the BGP next-hop address. The router performs a recursive lookup in its internal routing table and selects the path with the lowest interior metric. This ensures that the physical distance across the local network is minimized.[1]

Preferring eBGP over iBGP ensures traffic exits the local network as quickly as possible.

If all policy and distance metrics are identical, the router relies on arbitrary tie-breakers to guarantee a single winner. It will prefer the route from the peer with the lowest BGP Router ID, followed by the shortest cluster-list length in route-reflector environments, and finally the lowest neighbor IP address. These final steps ensure that the maximum 4,096-octet BGP update messages resolve to a single, stable entry in the forwarding table.[1][2]

Definitions

Autonomous System (AS)
A network or group of networks under a single administrative control, typically an ISP or large enterprise.
Local Preference
A BGP attribute used to indicate the preferred path for outbound traffic leaving an Autonomous System.
AS-Path
A list of the Autonomous System numbers that a route has traversed to reach its destination.
Multi-Exit Discriminator (MED)
An attribute used to suggest the preferred entry point into a network when multiple connections exist to the same neighbor.
eBGP
External BGP, used to exchange routing information between different Autonomous Systems.

Questions & answers

Why doesn't BGP choose the fastest path?

BGP is a policy-based protocol, not a performance-based one. It selects paths based on business agreements and administrative preferences rather than measuring latency or bandwidth.

What happens if all BGP attributes are identical?

If all policy and distance metrics tie, the router uses arbitrary tie-breakers, such as preferring the route from the peer with the lowest BGP Router ID, to guarantee a single winner.

Can an administrator override the BGP standard?

Yes. Many vendors, including Cisco, include a proprietary 'Weight' metric that is evaluated before standard attributes, allowing local administrators to force a specific path selection.

Significance

Every packet traversing the global internet relies on BGP's path selection algorithm to reach its destination. Understanding this sequence allows network engineers to dictate traffic flows, enforce transit agreements, and prevent catastrophic routing loops.

Sources

Source coverage

6 outlets

3 viewpoints surfaced

Transit Providers 40%Enterprise Edge Administrators 35%Protocol Standards Authors 25%
  1. [1]IETFProtocol Standards Authors

    A Border Gateway Protocol 4 (BGP-4)

    Read on IETF
  2. [2]Cisco

    Select BGP Best-path Algorithm

    Read on Cisco
  3. [3]HPE Aruba Networking

    Introduction

    Read on HPE Aruba Networking
  4. [4]BGP.us

    Overview of the BGP (Border Gateway Protocol)

    Read on BGP.us
  5. [5]Cisco

    Use MultiExit Discriminator to Influence BGP Best Path Selection

    Read on Cisco
  6. [6]Factlen Editorial Team

    Synthesis by Factlen editorial team

    Read on Factlen Editorial Team

Comments

Stay informed

Every angle. Every day.

Get Guides stories with full source coverage and perspective breakdowns delivered to your inbox.