BGP · Topic 6 of 7

BGP Best Path Selection and Prefix Attributes

Learn the order BGP uses to choose a best path and how local preference, AS path prepend, MED, origin and communities are used to steer traffic.

  • 4 min
In a nutshell
  • The first decision step is the highest local preference.
  • Next come the shortest AS path, then the lowest origin, then the lowest MED.
  • Then eBGP is preferred over iBGP, then the lowest IGP cost, then the lowest router ID.

What it is

When a router hears one prefix from several peers, BGP compares the paths in a fixed order and installs one.

The comparison looks at the attributes that travel with each route. By changing those attributes in filters, an administrator influences which path wins.

Knowing the order and the direction of influence is the core skill for this topic.

Path 1via ISP AattributesComparedecision orderwinnerBest pathinstalled in routing tablePath 2via ISP BattributesComparedecision order

How it works

First the router drops unusable routes, then it applies the decision steps one at a time until one path remains.

The decision order

A route with an unreachable next hop is ignored before comparison. For the rest, the main steps are these, in order.

  1. Highest local preference.
  2. Shortest AS path.
  3. Lowest origin, where IGP is preferred over EGP, and EGP over incomplete.
  4. Lowest MED, normally compared only between routes from the same neighbouring AS.
  5. eBGP routes preferred over iBGP routes.
  6. Lowest IGP cost to the next hop.
  7. Lowest router ID as the final tie breaker.

The first step that gives a single winner ends the process. Later steps are ignored.

Attribute categories

BGP groups attributes by whether every router must understand them and whether they are passed on.

CategoryExamples
Well known mandatoryOrigin, AS path, next hop
Well known discretionaryLocal preference, atomic aggregate
Optional transitiveAggregator, communities
Optional non transitiveMED, originator ID, cluster list

Local preference

Local preference is the strongest tool for choosing the exit from your AS. A higher value wins. It is shared with iBGP peers but never sent to eBGP peers.

Set it in an in filter on the peer you want to prefer. The filter options shown on this page are the RouterOS v6 names, and RouterOS v7 writes its filters differently.

/routing filter add chain=from-isp1 set-bgp-local-pref=200 action=accept
/routing bgp peer set isp1 in-filter=from-isp1

AS path prepend

Prepending adds extra copies of your AS number to the path. A longer path is less attractive. It is used in an out filter to discourage other ASes from sending traffic over a given link.

/routing filter add chain=to-isp2 set-bgp-prepend=3 action=accept
/routing bgp peer set isp2 out-filter=to-isp2

MED

MED is a hint sent to a neighbouring AS about which of several links into your network you prefer. Lower is better. It is advisory, and the neighbour may ignore it.

/routing filter add chain=to-isp1 set-bgp-med=50 action=accept

Communities

A community is a tag attached to a route. Other routers can match the tag in a filter and act on it. Well known values such as no export and no advertise limit how far a route spreads.

Which attribute influences what

AttributeDirection it influencesWhere it is set
Local preferenceOutbound traffic from your ASIn filter
AS path prependInbound traffic to your ASOut filter
MEDInbound traffic, as a hintOut filter
CommunityWhatever the receiver definesEither
Local preferenceset on in filtersteersOutbound trafficleaving your ASAS path prependset on out filtersteersInbound trafficentering your AS
Exam tip

To remember the direction, local preference is local, so it decides where your traffic leaves. Prepend is a message to others about where they should send theirs.

What to remember for the exam

Eight facts cover most questions here.

  • The first decision step is the highest local preference.
  • Next come the shortest AS path, then the lowest origin, then the lowest MED.
  • Then eBGP is preferred over iBGP, then the lowest IGP cost, then the lowest router ID.
  • Local preference is sent to iBGP peers and not to eBGP peers.
  • Prepending lengthens the AS path and lowers the attraction of a link.
  • MED is a hint to a neighbouring AS, and lower is better.
  • Communities are tags used to trigger policy.
  • The filter options are set-bgp-local-pref, set-bgp-prepend, set-bgp-med and set-bgp-communities.
Watch out

Higher local preference wins, but lower MED wins. Do not mix up the two directions.

Common mistakes

These four errors are common.

Using prepend to steer outbound traffic. Prepending is seen by other ASes, so it affects traffic coming in.

Expecting local preference to cross an AS border. It stays inside your AS, so a neighbour never sees it.

Reading MED as a command. The neighbour can ignore it, and it is normally compared only between routes from the same AS.

Forgetting the next hop check. A route with an unreachable next hop never enters the comparison.

Check yourself

Work through these three questions.

  • Which attribute is checked first, and is a higher or lower value better?

    Local preference is checked first, and a higher value wins.

  • How can you make other ASes prefer your other link for inbound traffic?

    Prepend your AS number in an out filter on the link you want to discourage, using set-bgp-prepend. The longer AS path makes that link less attractive.

  • Is MED better when it is higher or lower, and who is it for?

    A lower MED is better. It is a hint sent to a neighbouring AS about which link into your network you prefer.

Topic 6 of 7 in BGP

Test yourself on BGP

The full MikroTik MTCINE question bank has 475 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 MikroTik. Vendor names, certification names and exam codes are trademarks of their respective owners and are used for identification only.