Skip to main content
ExplainerBGP Routing· 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

Share this story

Listen

In short

  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.

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 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]

How we did this

Method
A comparative normalisation of the BGP path selection tie-breaking sequence across the IETF standard (RFC 4271) and major vendor implementations to determine the exact point where deterministic standard routing diverges into proprietary metrics.
What we found
While the IETF standard dictates Local Preference as the first configurable tie-breaker, major vendor implementations inject a proprietary 'Weight' metric before it, meaning standard BGP path selection is functionally overridden at the local router level before AS-wide policies apply.
What we worked from
  • IETF RFC 4271 Step 2 (Local Preference): Highest LOCAL_PREF wins — IETF
  • Cisco proprietary Step 1 (Weight): Highest Weight wins (local to router) — Cisco
Limits of this analysis
This analysis only accounts for default vendor behaviors; network administrators can use route maps to manually alter the evaluation sequence or strip proprietary attributes.

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.

Analysis by camp

Transit Providers

Network operators who sell internet access prioritize Local Preference to enforce business agreements.

For Tier 1 and Tier 2 internet service providers, BGP is primarily a financial tool. They use Local Preference to ensure that traffic destined for the internet is routed through settlement-free peering links or customer links rather than paid transit links. By assigning a higher Local Preference to routes learned from paying customers, they guarantee that revenue-generating paths are always selected over paths that cost them money, regardless of the AS-Path length.

Enterprise Edge Administrators

Corporate network engineers focus on AS-Path prepending and MED to balance inbound traffic.

Enterprises that are multi-homed to several ISPs cannot easily control how the outside world routes traffic back to them. Instead, they rely on AS-Path prepending to make backup links look less attractive, and use the Multi-Exit Discriminator (MED) to balance traffic across multiple links to the same ISP. Their primary goal is redundancy and load balancing rather than transit cost optimization.

Protocol Standards Authors

The IETF focuses on deterministic, loop-free routing across the global internet.

The authors of RFC 4271 designed the BGP path selection algorithm to guarantee that routing decisions are deterministic and loop-free. They intentionally excluded dynamic metrics like link latency or bandwidth from the standard, recognizing that rapid fluctuations in those metrics would cause global routing tables to constantly recalculate, leading to catastrophic instability known as route flapping.

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

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 →
Listen

Comments

Stay informed

Every angle. Every day.

Get Guides stories with full source coverage and perspective breakdowns, free every day.