Networking · Topic 3 of 8

BGP Path Selection Algorithm

Learn the BGP best path selection order from weight and local preference to router ID, how MED and AS path tools change it, and how to read the result.

  • 5 min
In a nutshell
  • The first test is that the next hop is reachable.
  • Weight is highest wins, local to the router and Cisco specific.
  • Local preference is highest wins, default 100, and shared inside the AS.

What it is

BGP path selection is the ordered list of tests a router uses to pick one best path for each prefix.

A router may learn the same prefix from several neighbours. It compares the paths one test at a time and stops at the first test that finds a winner. Only the best path is installed in the routing table and advertised to other peers.

Many pathssame prefixvalid next hopWeight and local prefpolicy testsAS path origin MEDeBGP or iBGPthen IGP metricrouter IDOne best pathinstalled

How it works

The tests run in a fixed order, and the first difference decides the result.

The order of tests

This is the common order on Cisco routers. IOS XE and IOS XR differ in small details, so confirm the exact order for the platform in use.

StepTestPreferred
0Next hop reachablePath with a reachable next hop
1WeightHighest (Cisco local setting)
2Local preferenceHighest, default 100
3Locally originatedPaths originated on this router
4AS path lengthShortest
5OriginIGP, then EGP, then incomplete
6MEDLowest
7eBGP or iBGPeBGP over iBGP
8IGP metric to the next hopLowest
9Router IDLowest
10Cluster list length, then neighbour addressShortest, then lowest

A path with an unreachable next hop is not a candidate at all. Weight is local to the router and is never sent to a neighbour. Local preference travels inside the AS to all iBGP peers.

Policy tools in the early steps

Weight and local preference are the main tools for choosing an exit from your own AS. Set them with a route map.

R1(config)# route-map PREFER_R2 permit 10
R1(config-route-map)# set local-preference 200
R1(config)# router bgp 65000
R1(config-router)# neighbor 192.0.2.2 route-map PREFER_R2 in

That is IOS XE. The route map is covered in a later page. A longer AS path can be added on purpose with AS path prepending, which makes a path less attractive to other autonomous systems.

MED

MED (multi exit discriminator) suggests to a neighbouring AS which entry point to use. Lower is better.

By default MED is compared only between paths that come from the same neighbouring AS. One command changes that rule, and a second changes the AS path step:

SettingIOS XEIOS XR
Compare MED from any ASbgp always-compare-medbgp bestpath med always
Ignore the AS path lengthbgp bestpath as-path ignorebgp bestpath as-path ignore

On IOS XE a path with no MED is treated as zero by default. Changing the compare rules on only some routers can cause loops or oscillation, so apply such settings on every router.

eBGP versus iBGP, and the IGP metric

An eBGP path wins over an iBGP path, if everything above is equal. After that, the router chooses the path whose next hop is closest by IGP metric. This gives hot potato routing, which sends traffic out of the AS at the nearest exit.

The tie breakers

If paths are still equal, the lowest router ID wins. In a route reflector design, the originator ID is used in its place, and a shorter cluster list is preferred. The last test is the lowest neighbour address.

Reading the result

On IOS XE the best path is marked with > in the table.

R1# show ip bgp 203.0.113.0/24

On IOS XR the command below shows why each path was or was not chosen. The exact output varies with the software release.

RP/0/RP0/CPU0:R1# show bgp ipv4 unicast 203.0.113.0/24 bestpath-compare
Path Alocal pref 100comparedPath Blocal pref 200winner isPath Bstop at step 2

What to remember for the exam

Eight facts cover most questions on this topic.

  • The first test is that the next hop is reachable.
  • Weight is highest wins, local to the router and Cisco specific.
  • Local preference is highest wins, default 100, and shared inside the AS.
  • Locally originated paths beat learned paths, then the shortest AS path wins.
  • Origin order is IGP, then EGP, then incomplete.
  • MED is lowest wins, and compared only within the same neighbouring AS by default.
  • eBGP beats iBGP, and then the lowest IGP metric to the next hop wins.
  • The final tie breakers are router ID (or originator ID), cluster list and neighbour address.
Exam tip

To steer outbound traffic, change weight or local preference. To influence inbound traffic from another AS, use AS path prepending or MED.

Common mistakes

These four mistakes appear often.

Expecting weight to reach other routers. Weight stays on the router where it was set.

Comparing MED across different neighbouring ASes. By default MED is not compared between different neighbour ASes.

Forgetting the IGP metric step. Two equal paths are split by the distance to the next hop.

Changing a bestpath option on one router. The routers can then disagree on the best path.

Watch out

A route is not a candidate if its next hop is unreachable. Check next hop reachability before looking at any attribute.

Check yourself

Work through these three questions.

  • Which is stronger, weight or local preference?

    Weight is tested first, so a higher weight wins even when local preference differs.

  • When is MED compared by default?

    Only between paths from the same neighbouring AS.

  • What decides between an eBGP path and an iBGP path that are equal so far?

    The eBGP path is preferred.

Topic 3 of 8 in Networking

Test yourself on Networking

The full Cisco CCNP 350-501 SPCOR question bank has 738 questions, with timed practice exams and an explanation for every answer.

This study guide is independent and is not affiliated with, endorsed by, or sponsored by Cisco. Vendor names, certification names and exam codes are trademarks of their respective owners and are used for identification only.