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
- 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.
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.
- Highest local preference.
- Shortest AS path.
- Lowest origin, where IGP is preferred over EGP, and EGP over incomplete.
- Lowest MED, normally compared only between routes from the same neighbouring AS.
- eBGP routes preferred over iBGP routes.
- Lowest IGP cost to the next hop.
- 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.
| Category | Examples |
|---|---|
| Well known mandatory | Origin, AS path, next hop |
| Well known discretionary | Local preference, atomic aggregate |
| Optional transitive | Aggregator, communities |
| Optional non transitive | MED, 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
| Attribute | Direction it influences | Where it is set |
|---|---|---|
| Local preference | Outbound traffic from your AS | In filter |
| AS path prepend | Inbound traffic to your AS | Out filter |
| MED | Inbound traffic, as a hint | Out filter |
| Community | Whatever the receiver defines | Either |
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-medandset-bgp-communities.
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.