Course contents

Module 3: Path SelectionLesson 3.10 (10 of 13 in this module)19 of 43 in the BGP course

The BGP best-path algorithm

A router can hold five paths to one prefix and install exactly one. It does not add up scores. It walks down a fixed list and stops at the first difference. Learn the list and you can predict the winner.

Advanced · 14 min read

You'll learn to list the Cisco IOS XE best-path steps in order, work out which of several paths wins and at which step, and read the result in show ip bgp.

One prefix, several candidates, one winner

In the course network R1 learns 203.0.113.0/24 from ISP A and, over iBGP, from R2. Both are valid paths. The routing table can hold only one BGP route for that prefix, so the BGP process runs the best-path algorithm and marks the winner with > in the table. Everything else stays in the BGP table as a backup, ready to take over the moment the winner disappears.

Before any comparison, a path must be valid: its next hop must be reachable and no inbound policy may have rejected it. An invalid path is never a candidate, whatever its attributes say. This is the single most common reason a path you configured to win does not.

How a Cisco router chooses one best path
How a Cisco router chooses one best pathTen rows. A path must first be valid with a reachable next hop. Then Cisco IOS XE compares weight, local preference, local origination, AS_PATH length, origin, MED, eBGP over iBGP, IGP metric to the next hop and finally the tie-breakers. The first difference decides.Gate: the path must be valid, with a reachable next hop and not filtered1Weighthighest wins (Cisco only, local to this router)2LOCAL_PREFhighest wins (shared inside the AS)3Locally originatedpreferred over paths learned from peers4AS_PATH lengthshortest wins5ORIGINlowest wins: IGP, then EGP, then incomplete6MEDlowest wins (same neighbouring AS by default)7eBGP over iBGPan external path beats an internal one8IGP metric to next hoplowest wins9Tie-breakersoldest eBGP path, lowest router ID, lowest neighbour IPFirst difference wins. Steps 1 and 2 are the ones you configure most often.

On small screens, swipe sideways or focus the diagram and use the arrow keys.

The comparison stops at the first step where the paths differ. Later steps are never reached for that prefix.

The algorithm stops at the first step where the paths differ, so a step that matters never gets undone by a later one. That is why a high local preference beats a short AS path, and why tuning a later attribute can have no effect at all.

Watching candidates drop out

The easiest way to understand the order is to follow a field of paths down the list. Five paths exist for one prefix. Each is compared with the best one so far, and the router stops comparing a pair as soon as they differ.

Five paths, one winner: where each one drops out
Five paths, one winner: where each one drops outPaths A and B are eliminated at local preference because they have 100 against 200. Path C is eliminated at AS path length because it has three ASes against two. Path E is eliminated at origin because it is incomplete. Path D has weight, local preference and AS path length equal to E, a better origin, and is the winner at the origin step; later steps are not needed.WeightLocal prefOriginatedAS pathOriginMEDeBGPIGP costTie-breakPath ALP 100, 1 AStieoutPath BLP 100, 2 AStieoutPath CLP 200, 3 AStietietieoutPath DLP 200, 2 AS, itietietietiewinsPath ELP 200, 2 AS, ?tietietietieoutRead each row left to right: a path stays green while it ties with the best so far.

On small screens, swipe sideways or focus the diagram and use the arrow keys.

Green means the path is still in the running. Red marks the step where it lost. Grey steps were never reached. Path A has the shortest AS path but loses first, because local preference is compared earlier.
PathWeightLocal prefAS_PATH lengthOriginResult
A01001iOut at step 2: local preference
B01002iOut at step 2: local preference
C02003iOut at step 4: AS_PATH length
D02002iWins at step 5: origin
E02002?Out at step 5: origin

Path A has the shortest AS path of all five and still loses first. That is the lesson: the order is a ranking of whose opinion matters most, with your own policy (weight, local preference) ahead of anything the rest of the internet says about the route.

The steps, with what each one means and how you use it

How the field narrows
How the field narrowsThe router removes candidates step by step until one path remains.All pathsfor one prefixPolicyWeight, local pref,locally originatedPath qualityAS path, origin,MED, eBGPIGP costclosest exitTie-breakersoldest, routerID, address

On small screens, swipe sideways or focus the diagram and use the arrow keys.

Early steps are local policy, middle steps compare path quality, and the last steps break ties.
#StepWinnerWhy it existsHow you change it
1WeightHighestPer-router override, Cisco onlyneighbor weight or set weight
2Local preferenceHighestThe AS-wide choice of exitset local-preference in an inbound route map
3Locally originatedLocal routeA router trusts what it injectednetwork, aggregate-address, redistribute
4AS_PATH lengthShortestFewer ASes is a rough measure of closeras-path prepend (adds length)
5ORIGINi, then e, then ?Trust how the route entered BGPset origin
6MEDLowestA neighbour's preferred entry pointset metric
7eBGP over iBGPExternalLeave the AS sooner rather than laterWhich sessions exist
8IGP metric to next hopLowestUse the closest exitIGP cost, next-hop-self
9Tie-breakersOldest, lowest router ID, lowest addressMake the choice stableRarely tuned

Steps 1 to 3: your own policy

Weight is for one router and never leaves it. Local preference is carried to every iBGP peer, so every router in the AS agrees on the exit. Locally originated means a path this router put into BGP itself beats anything it learned, which stops a router from preferring a longer trip through someone else to a prefix it owns. These three are the steps you control directly, which is why they come first.

Steps 4 to 6: how good the path looks

AS_PATH length is the default proxy for distance. Origin and MED rarely decide anything on their own. MED is compared only between paths from the same neighbouring AS unless you configure bgp always-compare-med.

Steps 7 and 8: leave the AS the nearest way

If the paths are still equal, an external path wins over an internal one, and then the exit with the lowest IGP cost to its next hop wins. Together these give hot-potato routing: hand the packet to the neighbour as soon as you can.

Two routers, one prefix, two exits
Two routers, one prefix, two exitsHot-potato routing.R1IGP cost 10 to ISP A exitIGP cost 50 to ISP B exitLeaves via ISP AR2IGP cost 50 to ISP A exitIGP cost 10 to ISP B exitLeaves via ISP Bvs

On small screens, swipe sideways or focus the diagram and use the arrow keys.

With every earlier step tied, each router chooses the exit with the lowest IGP metric to the next hop, so they can choose different exits.

Step 9: making the answer stable

If everything is still equal, the router prefers the oldest eBGP path, so a path that has been working is not displaced by a new one that appears later. After that come the lowest router ID of the advertising neighbour, the shortest cluster list, and the lowest neighbour address. None of these reflect quality, and a design that depends on them has an unplanned outcome.

A second worked example: MED and the neighbouring AS

MED is the step people get wrong, because whether it is compared depends on where the paths came from.

PathNeighbouring ASLocal prefAS_PATH lengthMED
P645011002200
Q64501100250
R64502100210

P and Q come from the same neighbouring AS, so their MEDs are compared and Q beats P. R has the lowest MED of all, but it comes from a different AS, so by default its MED is not compared with Q's. Q and R then go on to the later steps. With bgp always-compare-med, R would win here. Mixed comparisons can also make the result depend on the order paths arrived, which is what bgp deterministic-med fixes.

router bgp 64500 bgp always-compare-med bgp deterministic-med

Global BGP configuration. Compares MED across different neighbouring ASes, and orders paths from the same AS together before comparing, so the choice no longer depends on arrival order. Use only when you control or trust the MED values from every neighbour.

Reading the result on the router

Illustrative Cisco IOS XE output written for this lesson, not a router capture
R1#show ip bgp 203.0.113.0
BGP routing table entry for 203.0.113.0/24, version 9
Paths: (3 available, best #3, table default)
  64501 64510
    192.0.2.1 from 192.0.2.1 (11.11.11.11)
      Origin IGP, metric 0, localpref 100, valid, external
  64502
    10.255.0.2 from 10.255.0.2 (2.2.2.2)
      Origin IGP, metric 0, localpref 100, valid, internal
  64502 64511 64512
    10.255.0.3 from 10.255.0.3 (3.3.3.3)
      Origin IGP, metric 0, localpref 200, valid, internal, best
The header says three paths are available and the third is best. The best path is the one with the highest local preference, which is the second comparison. Order in the output is not ranking order; only the best flag tells you the winner. To see why, compare the attributes of the winner against each other path, step by step.
Reading show ip bgp
Reading show ip bgpThe leading symbols describe each path.*Valid path (next hop reachable)>Best path chosen for the routing tableiLearned from an iBGP neighbourPath ends in i, e or ?Origin code: IGP, EGP or incomplete

On small screens, swipe sideways or focus the diagram and use the arrow keys.

Look for the asterisk and the greater-than sign first.

Why did my path lose?

When the wrong path wins, do not change attributes at random. Find the step where your path lost, then change that attribute.

A method for an unexpected winner
A method for an unexpected winnerFour questions, in order.Valid?Next hopreachable?Policy?Weight, localpreference differ?Path?AS path length,origin, MEDTie?eBGP, IGP cost,oldest path

On small screens, swipe sideways or focus the diagram and use the arrow keys.

Most unexpected winners are explained by the first two questions.
show ip bgp 203.0.113.0 show ip route 10.255.0.2 show ip bgp neighbors 192.0.2.1 routes show ip bgp neighbors 192.0.2.1 received-routes

The first lists every path with its attributes. The second checks the next hop is reachable. The third shows what passed inbound policy, and the last, with soft-reconfiguration inbound, what the neighbour actually sent.

Once you know the step, the fix is the tool from the table above. When a path lost on local preference, prepending or MED will not help. When it lost on AS_PATH length, a bigger weight will help only on that one router.

Technical reference: Cisco, BGP best path selection algorithm, and RFC 4271, section 9.1.2. Example output is illustrative.

Predict the winner

Predict · scenario 1

Path X has local preference 150 and AS_PATH length 4. Path Y has local preference 100 and AS_PATH length 1. Weights tie. Which wins?

Predict · scenario 2

Two paths tie on weight, local preference, AS_PATH length, origin and MED. One is from an eBGP peer and one from an iBGP peer. Which wins?

Predict · scenario 3

You raise the weight on a path that is not the best. The best path does not change. What is the most likely reason?

Predict · scenario 4

Two paths are from different neighbouring ASes and tie up to MED. One has MED 10, the other MED 500. No extra commands are configured. What happens at the MED step?

Predict · scenario 5

R1 and R2 both hold two equal paths, one via ISP A and one via ISP B, tied through the eBGP step. R1 is much closer to ISP A in the IGP, R2 closer to ISP B. What happens?

FAQ

Does BGP prefer the shortest AS path?
Only after weight, local preference and local origination have tied. AS path length is the fourth comparison on Cisco IOS XE. A path with a long AS path and a high local preference beats a short path with the default local preference.
Why are locally originated routes preferred?
A route the router originated itself, with a network statement, aggregate or redistribution, gets weight 32768 and so wins the first comparison against any learned path with the default weight of 0.
Can two paths be installed at once?
Only with BGP multipath, which is configured separately and requires the paths to tie on the steps that matter. By default BGP selects exactly one best path per prefix.