BGP MED: always-compare-med and deterministic-med
Send the same three BGP paths to a router six times, changing nothing but the order in which they arrive, and you would expect the same best path every time. In our lab it picked three different winners, and in two of the runs it chose a path with BGP MED 100 while a path with MED 20 from the same neighbouring AS sat right next to it in the table. That is not a bug in the usual sense. It is how MULTI_EXIT_DISC is defined: a metric that may only be compared between some pairs of paths, so "better than" is not transitive and the result depends on how the router walks its list. This part covers the attribute byte by byte, where it sits in best-path selection, what always-compare-med, deterministic-med and missing-as-worst really change, the oscillation it can cause with route reflectors, and how often the real Internet table carries it.
- What it is: MULTI_EXIT_DISC, type code 4, flags
0x80(optional non-transitive), one 32-bit unsigned value. Lower wins. It tells a neighbouring AS which of your entry points to use.[1] - Scope: may be carried over iBGP, MUST NOT be passed to another AS. GoBGP stripped it towards eBGP by default; IOS XR replaced it with its own outbound value.[1][15][16]
- Default comparison: only between paths whose neighbouring (first) AS is the same, and only after LOCAL_PREF, AS_PATH length and ORIGIN tie. A missing MED counts as 0, the best value.[1][6]
- Arrival order matters: with default rules GoBGP 4.11 chose 3 different winners for the same 3 paths over 18 runs; with
always-compare-medit chose the MED 20 path 6 times out of 6.[15] - Real table: 57.9 % of IPv4 routes at RIS rrc14 carry a MED, but each peer sends it on all of its routes or on almost none, and just 140 distinct values cover 3.2 million routes.[17]
What BGP MED is
RFC 4271 defines the attribute in two places. In the UPDATE format section: "This is an optional non-transitive attribute that is a four-octet unsigned integer. The value of this attribute MAY be used by a BGP speaker's Decision Process to discriminate among multiple entry points to a neighboring autonomous system." And in section 5.1.4: "All other factors being equal, the exit point with the lower metric SHOULD be preferred."[1] The attribute was called INTER_AS_METRIC before BGP-4 renamed it.[2]
The direction is what confuses people. MED is set by the network that owns the destination and is read by the network that sends traffic to it. If AS 65001 connects to AS 65002 in Frankfurt and in Amsterdam, AS 65001 can advertise its Amsterdam prefixes with a lower MED on the Amsterdam session. AS 65002 then hands traffic over in Amsterdam instead of carrying it to Frankfurt and making AS 65001 haul it back. MED is therefore a request to the neighbour, not an instruction. The neighbour can ignore it, overwrite it or remove it, and RFC 4271 even requires implementations to offer a way to remove it.[1] That makes it the mirror image of LOCAL_PREF (part 9), which you set yourself to choose your own exit.
| Attribute | Who sets it | What it steers | How far it travels |
|---|---|---|---|
| LOCAL_PREF | you, on import | your exit out of your AS | your AS only (iBGP) |
| MED | the neighbour, on export | the neighbour's entry into its AS | one AS boundary, then iBGP inside the receiving AS |
| AS_PATH prepend | you, on export | inbound traffic, all neighbours | the whole Internet |
Four rules from RFC 4271 decide everything else in this article:[1]
- Propagation: "If received over EBGP, the MULTI_EXIT_DISC attribute MAY be propagated over IBGP to other BGP speakers within the same AS. The MULTI_EXIT_DISC attribute received from a neighboring AS MUST NOT be propagated to other neighboring ASes."
- Editing: removing or changing a received MED must be done before route selection.
- Comparison: "MULTI_EXIT_DISC is only comparable between routes learned from the same neighboring AS (the neighboring AS is determined from the AS_PATH attribute)."
- Missing value: "Routes that do not have the MULTI_EXIT_DISC attribute are considered to have the lowest possible MULTI_EXIT_DISC value."
Aggregation adds two more: routes with different MEDs "SHALL NOT be aggregated", and an aggregate whose AS_PATH starts with an AS_SET should not carry MED at all.[1] Parts 10 and 11 covered aggregation in detail.
BGP MED on the wire
The attribute is always 7 bytes: flags 0x80 (Optional = 1, Transitive = 0, Partial = 0, Extended Length = 0), type 04, length 04 and the value in network byte order. Nothing else can vary, which makes it a good attribute to learn error handling on. Figure 2 shows the same structure in two independent sources: Cisco IOS XR captures from the kazulog.info MED lab and our own GoBGP lab.[16][15]
In the IOS XR lab R1 received 192.168.6.0/24 from R6 (AS 65004) with MED 77. In the 67-byte iBGP UPDATE to R3 the attribute is 80 04 04 00 00 00 4d, so 77 crossed iBGP unchanged next to LOCAL_PREF 100. In the 64-byte eBGP UPDATE to R4 it is 80 04 04 00 00 00 c8 = 200: the 77 is gone and R1's outbound route-policy MED-200-OUT wrote a new value.[16] R3's view shows both metrics that IOS prints on one path, which is a classic exam trap:
RP/0/RP0/CPU0:R3#show bgp 192.168.6.0/24 (STEP 1, trimmed)
Paths: (1 available, best #1)
65004, (received & used)
10.0.0.1 (metric 2) from 10.0.0.1 (10.0.0.1)
Origin IGP, metric 77, localpref 100, valid, internal, best, group-best
(metric 2) after the next hop is the IGP cost to reach 10.0.0.1. The metric 77 after Origin IGP is the MED.[16] On IOS and IOS XR the MED appears under the column title Metric in show bgp; Junos calls it MED and FRR prints Metric as well.
Where MED sits in best-path selection
MED comes after LOCAL_PREF, AS_PATH length and ORIGIN in every implementation: step 6 in Cisco's numbering (after WEIGHT and the locally-originated check), step 7 in Juniper's.[7][11] In practice this means three things:
- A neighbour that sets a higher LOCAL_PREF on one of your links, which most providers do for customer routes, will never look at your MED for those routes.
- AS_PATH prepending beats MED. If you prepend on one link and set MED on the other, the prepend decides.
- Cisco's "neighbouring AS" is the first AS in the AS_SEQUENCE, and confederation sub-AS segments in front of it are skipped. With
bgp bestpath med-confed, MEDs are also compared for paths that consist only of AS_CONFED_SEQUENCE, i.e. routes originated inside the local confederation.[7]
RFC 4271 writes the comparison as a double loop over all candidates: "if (neighborAS(m) == neighborAS(n)) and (MED(n) < MED(m)) remove route m from consideration".[1] Read literally, the procedure removes every path that has a better path from the same neighbouring AS, and only then lets later rules choose among the survivors. That works on the full set at once. Routers, however, usually compare paths two at a time, and that is where the trouble starts.
The same section adds a rule that matters when MED is removed at the border: if an implementation strips MED before advertising into iBGP, the comparison on the received value may still be done, but only among eBGP-learned routes. "Including the MULTI_EXIT_DISC of an EBGP-learned route in the comparison with an IBGP-learned route, then removing the MULTI_EXIT_DISC attribute, and advertising the route has been proven to cause route loops."[1]
The lab: real IOS XR captures plus a GoBGP lab
Two independent sets of evidence are used below. The first is kazulog.info's published Cisco XRd 26.1.1 MED lab: six routers, AS 65001 connected to R4 (AS 65002) over two links (R1 and R2), a third path through AS 65003 (R5), and per-step show bgp, syslog, running configuration and packet captures. We downloaded all 114 files and decoded the captures ourselves.[16] The second is our own lab on this server: two GoBGP 4.11 routers and five raw Python BGP speakers that send handmade UPDATEs and record every byte r1 sends back.[15]
Every prefix in the GoBGP lab has an AS_PATH of length 2 and ORIGIN IGP, and none of the paths gets a LOCAL_PREF from policy, so the decision always reaches MED. Phase A uses GoBGP defaults, phase B turns on always-compare-med and two MED policies, phase C repeats only the arrival-order test with different spacing.
Step 1: two links into the same AS
A1 sends 198.51.100.0/24 with MED 200, A2 sends it with MED 50. Both paths come from AS 65001, so MED is compared and A2 wins. Then B (AS 65003) sends the same prefix with MED 10:
$ gobgp global rib 198.51.100.0/24 -a ipv4 (r1)
Network Next Hop AS_PATH Age Attrs
*> 198.51.100.0/24 127.0.2.135 65001 65004 00:00:03 [{Origin: i} {Med: 50}]
* 198.51.100.0/24 127.0.2.134 65001 65004 00:00:03 [{Origin: i} {Med: 200}]
* 198.51.100.0/24 127.0.2.136 65003 65004 00:00:01 [{Origin: i} {Med: 10}]
$ gobgp global rib 198.51.100.0/24 -a ipv4 (r2)
Network Next Hop AS_PATH Age Attrs
*> 198.51.100.0/24 127.0.2.135 65001 65004 00:00:03 [{Origin: i} {Med: 50} {LocalPref: 100}]
The path with the lowest MED in the table, 10, does not win. It comes from another neighbouring AS, so its MED is never compared with the other two; the tie is broken further down (here: the older eBGP path). r2, the iBGP peer, sees only r1's best path with MED 50 and LOCAL_PREF 100. IOS XR does exactly the same in kazulog's STEP 2, where R5 attaches MED 10 from AS 65003:[16]
RP/0/RP0/CPU0:R4#show bgp 192.168.6.0/24 (STEP 2, trimmed)
Paths: (3 available, best #2)
Path #1: ...
65001 65004, (received & used)
10.1.4.1 from 10.1.4.1 (10.0.0.1)
Origin IGP, metric 200, localpref 100, valid, external
Path #2: ...
65001 65004, (received & used)
10.2.4.2 from 10.2.4.2 (10.0.0.2)
Origin IGP, metric 50, localpref 100, valid, external, best, group-best
Path #3: ...
65003 65004, (received & used)
10.4.5.5 from 10.4.5.5 (10.0.0.5)
Origin IGP, metric 10, localpref 100, valid, external, group-best
Note the IOS XR flag group-best: IOS XR marks the best path of each neighbour-AS group, which is the deterministic-MED grouping described below, visible in the output.[16]
Step 2: what crosses iBGP and what crosses eBGP
3.026 A1 tx type=2 len=58 ORIGIN=0 | AS_PATH=65001 65010 | NH=127.0.2.134 | MED=77 | NLRI 203.0.113.0/24 T1 3.026 X rx type=2 len=55 ORIGIN=0 | AS_PATH=65002 65001 65010 | NH=127.0.2.131 | NLRI 203.0.113.0/24 5.027 I rx type=2 len=65 ORIGIN=0 | AS_PATH=65001 65010 | NH=127.0.2.134 | MED=77 | LP=100 | NLRI 203.0.113.0/24 # phase B, export policy set-med "300" towards X: 3.030 X rx type=2 len=62 ORIGIN=0 | AS_PATH=65002 65001 65010 | NH=127.0.2.131 | MED=300 | NLRI 203.0.113.0/24
A1 sent MED 77. The iBGP observer I received it unchanged (with LOCAL_PREF 100 added, as for every iBGP update), and the eBGP observer X got a 55-byte UPDATE with no MED at all, which is exactly what RFC 4271 asks for. In phase B the export policy set-med "300" towards X put a new value in, and that value is r1's own statement about its own network, not A1's.[1][15] RFC 4451 warns that vendors historically disagreed here: some sent MED to eBGP peers by default, others did not, and some did not even send it to iBGP peers.[3]
Why the arrival order changes the winner
Cisco's TAC note on MED uses three paths to explain the problem: entry1 from AS 65001 with MED 100, entry2 from AS 65002 with MED 50, entry3 from AS 65001 with MED 20, everything else equal. Cisco walks through two arrival orders and gets two different best paths, because IOS compares the newest path with the current best, one pair at a time.[8] We sent the same three values (AS 65003 instead of 65002) to GoBGP in all six possible orders, three times, with 0.02 s, 0.4 s and 1.5 s between the updates:
Here is one of the two forbidden results, order e1 → e2 → e3 with 1.5 s gaps:
## C order 1 e1>e2>e3 gap 1.50 s -> 10.10.1.0/24
3.028 A1 tx type=2 len=58 ORIGIN=0 | AS_PATH=65001 65004 | NH=127.0.2.134 | MED=100 | NLRI 10.10.1.0/24 C10.1 e1
4.528 B tx type=2 len=58 ORIGIN=0 | AS_PATH=65003 65004 | NH=127.0.2.136 | MED=50 | NLRI 10.10.1.0/24 C10.1 e2
6.029 A2 tx type=2 len=58 ORIGIN=0 | AS_PATH=65001 65004 | NH=127.0.2.135 | MED=20 | NLRI 10.10.1.0/24 C10.1 e3
$ gobgp global rib 10.10.1.0/24 -a ipv4 (r1)
Network Next Hop AS_PATH Age Attrs
*> 10.10.1.0/24 127.0.2.134 65001 65004 00:00:42 [{Origin: i} {Med: 100}]
* 10.10.1.0/24 127.0.2.136 65003 65004 00:00:41 [{Origin: i} {Med: 50}]
* 10.10.1.0/24 127.0.2.135 65001 65004 00:00:39 [{Origin: i} {Med: 20}]
The GoBGP source explains it. GoBGP keeps the paths of a prefix in a list sorted from best to worst and inserts each new path with a binary search (insertSort() using Go's sort.Search). The comparison function walks the usual steps; compareByMED() returns a winner only when always-compare-med is on, both paths are internal, or both have the same first non-confederation AS. For two eBGP paths that are still tied after that, compareByAge() prefers the older path, and if the age is equal too, the lower peer address wins.[14] Path age is stored in whole seconds (timestamp.Unix() in path.go), which is why the spacing of the updates changed the result.[14]
We checked this reading with a short script (model.py) that replays each run through the same binary-search insertion, using the per-path ages that gobgp global rib prints. Model and router agree on the full order of the list, not only on the best path, in 18 of 18 default runs. In the 0.02 s run of order 3, for example, the first path was stamped one second earlier than the other two, and the CLI shows exactly that: an age of 7 s for e2 and 6 s for e1 and e3.[15] This is a model of GoBGP specifically. Cisco IOS without deterministic-med walks the paths newest-first and keeps the winner of each pair, so its outcomes differ in detail but have the same cause.[8]
deterministic-med: group first, then compare
The fix most vendors chose is to stop comparing paths in arrival order. With bgp deterministic-med on Cisco IOS and IOS XE, "routes from the same autonomous system are grouped together, and the best entries of each group are compared".[9] Inside each group MED is always comparable, so each group's winner is well defined, and the group winners are compared without MED (unless always-compare-med is also on). In Cisco's example, group AS 65001 produces entry3 (MED 20 beats 100), group AS 65002 produces entry2, and the final pair is decided by the rules after MED, in any arrival order.[8] Cisco's 2005 note recommends "enabling the bgp deterministic-med command in all new network rollouts" and adds that on existing networks it must be enabled on all routers at the same time, or incrementally with care, to avoid iBGP routing loops.[9]
The knob exists in different forms:
| Implementation | Default | Knobs |
|---|---|---|
| Cisco IOS / IOS XE | arrival order (non-deterministic) | bgp deterministic-med (since 11.1), bgp always-compare-med (since 11.0), bgp bestpath med missing-as-worst, bgp bestpath med confed[10] |
| Cisco IOS XR 26.1.1 | groups by neighbour AS (group-best) | bgp bestpath med always, missing-as-worst, confed; bgp deterministic-med is rejected with % Invalid input detected[16] |
| Junos | deterministic | path-selection always-compare-med | cisco-non-deterministic | external-router-id, med-plus-igp[11][12] |
| FRR | knob | bgp deterministic-med, bgp always-compare-med, bgp bestpath med missing-as-worst[13] |
| GoBGP 4.11 | sorted insertion, order-dependent | always-compare-med, external-compare-router-id; no deterministic-med or missing-as-worst option[14] |
Juniper documents its cisco-non-deterministic option with a plain recommendation: "We recommend that you do not configure this option, because the nondeterministic behavior sometimes prevents the system from properly comparing the MEDs between paths."[11] FRR's documentation adds a cost note: its deterministic-MED implementation "scales significantly worse than most sorting algorithms" as the number of paths per prefix grows.[13]
One documentation conflict is worth knowing about. Cisco's TAC note 13759, revised on 30 July 2026, closes with "Cisco recommends you enable bgp always-compare-med in all new network deployments".[8] The older note 16046 and the IOS command reference recommend deterministic-med and describe always-compare-med as useful only when the neighbouring networks "agree on a uniform policy for setting MED".[9][10] The two commands do different things, so read the newer sentence with care before you turn on cross-AS MED comparison because of it.
always-compare-med: one yardstick for everyone
With always-compare-med the MED comparison applies to every pair, the preference becomes a total order and the arrival order stops mattering. Phase B shows the effect on the same prefixes:
$ gobgp global rib 198.51.100.0/24 -a ipv4 (r1)
Network Next Hop AS_PATH Age Attrs
*> 198.51.100.0/24 127.0.2.136 65003 65004 00:00:01 [{Origin: i} {Med: 10}]
* 198.51.100.0/24 127.0.2.135 65001 65004 00:00:03 [{Origin: i} {Med: 50}]
* 198.51.100.0/24 127.0.2.134 65001 65004 00:00:03 [{Origin: i} {Med: 300}]
$ gobgp global rib 10.7.4.0/24 -a ipv4 (r1)
Network Next Hop AS_PATH Age Attrs
*> 10.7.4.0/24 127.0.2.136 65003 65004 00:00:03 [{Origin: i}]
* 10.7.4.0/24 127.0.2.135 65001 65004 00:00:04 [{Origin: i} {Med: 20}]
On 198.51.100.0/24 B's MED 10 now beats A2's MED 50 (and A1's 200 + 100 from the import policy). The second output is the side effect: on 10.7.4.0/24 B sent no MED at all, and a missing MED counts as 0, so it beat A2's MED 20 from the other AS. As soon as you compare MEDs across ASes, a neighbour that simply forgets to set MED wins everything. The same switch on IOS XR moved R4 to the MED 10 path in kazulog's STEP 3 (Paths: (3 available, best #3)).[16]
The problem is the yardstick. Each AS picks its MEDs from its own internal metric: one uses 0 and 100, another uses IGP costs in the thousands, a third sends nothing. RFC 4451 says operators "should be wary of the potential side effects" of cross-AS comparison, and Cisco requires it to be enabled (or disabled) consistently across the whole AS, or routing loops can occur.[3][7] The common safe pattern, also suggested in the FRR documentation, is to combine always-compare-med with a policy that rewrites MED on ingress, so that all values are yours.[13]
Edge cases: missing MED, 232-1, overflow and malformed values
$ gobgp global rib 10.7.1.0/24 -a ipv4 (r1)
Network Next Hop AS_PATH Age Attrs
*> 10.7.1.0/24 127.0.2.134 65001 65004 00:00:07 [{Origin: i}]
* 10.7.1.0/24 127.0.2.135 65001 65004 00:00:07 [{Origin: i} {Med: 1}]
$ gobgp global rib 10.7.3.0/24 -a ipv4 (r1)
Network Next Hop AS_PATH Age Attrs
*> 10.7.3.0/24 127.0.2.135 65001 65004 00:00:01 [{Origin: i} {Med: 4294967294}]
* 10.7.3.0/24 127.0.2.134 65001 65004 00:00:02 [{Origin: i} {Med: 4294967295}]
On 10.7.1.0/24 A1 sent no MED and A2 sent MED 1, both from AS 65001: the path without MED won, as RFC 4271 requires. This rule has a history. RFC 4451 records that "earlier versions of the specification called for a missing MED to be assigned the highest possible MED value (i.e., 2^32-1)", that some implementations considered MED 0 worse than no MED, and that some rewrote a received 232-1 to 232-2 because they used the maximum internally as "infinity".[3] Cisco still documents that last behaviour for IOS: a MED of 4,294,967,295 is changed to 4,294,967,294 before the path enters the BGP table.[7] GoBGP stored 4,294,967,295 unchanged (second output). IOS XR shows the other direction: with bgp bestpath med missing-as-worst, R4 displayed the path without MED as metric 4294967295.[16] RFC 4451's practical advice follows from all this: many operators reset MED on ingress and avoid sending 0 and 232-1 at all.[3]
Policy arithmetic has its own edge. In phase B the import policy on A1 added 100 to every MED. For a received 4,294,967,250 the sum does not fit in 32 bits, and GoBGP refused the action rather than wrapping around:
level=WARN msg="action failed" Topic=policy Error="med value invalid. it's overflow threshold: 4294967395" level=WARN msg="action failed" Topic=policy Error="med value invalid. it's overflow threshold: 4294967350"
The route was still accepted with its original MED. The first log line is the same refusal for A1's 4,294,967,295 from the max-value test, which also kept its value. On a path without MED the same +100 created MED 100, so "add" starts from 0.[15]
Malformed MED: treat-as-withdraw
RFC 7606 makes MED one of the attributes whose errors are handled by "treat-as-withdraw": the prefixes in that UPDATE are withdrawn, the session stays up.[4] The attribute is malformed "if its length is not 4", and wrong Optional or Transitive flag bits count as malformed too.[4] We sent every prefix first with a valid MED and then with a broken one. All five broken versions were withdrawn from the table and from I and X, and r1 logged:
level=WARN msg="the received Update message was treated as withdraw" Error="med length isn't correct" level=WARN msg="the received Update message was treated as withdraw" Error="med length isn't correct" level=WARN msg="the received Update message was treated as withdraw" Error="flags are invalid. attribute type: BGP_ATTR_TYPE_MULTI_EXIT_DISC, expect: OPTIONAL, actual: TRANSITIVE" level=WARN msg="the received Update message was treated as withdraw" Error="flags are invalid. attribute type: BGP_ATTR_TYPE_MULTI_EXIT_DISC, expect: OPTIONAL, actual: TRANSITIVE|OPTIONAL" level=WARN msg="the received Update message was treated as withdraw" Error="med length isn't correct"
After the five tests the session to A1 was still Established with Flops = 0, and the neighbour statistics counted 5 discarded updates.[15] Before RFC 7606, an attribute length error closed the whole BGP session with a NOTIFICATION, together with every other route on it.[1][4]
Changing a MED: one UPDATE, one flip
A MED change is an ordinary UPDATE that replaces the previous path from the same peer. When A2 raised its MED from 50 to 300, r1 moved the best path to A1 (MED 200) and told its iBGP peer:
25.619 A2 tx type=2 len=58 ORIGIN=0 | AS_PATH=65001 65004 | NH=127.0.2.135 | MED=300 | NLRI 198.51.100.0/24 T6 A2 300
25.620 B rx type=2 len=55 ORIGIN=0 | AS_PATH=65002 65001 65004 | NH=127.0.2.131 | NLRI 198.51.100.0/24
26.121 I rx type=2 len=65 ORIGIN=0 | AS_PATH=65001 65004 | NH=127.0.2.134 | MED=200 | LP=100 | NLRI 198.51.100.0/24
26.621 X rx type=2 len=55 ORIGIN=0 | AS_PATH=65002 65001 65004 | NH=127.0.2.131 | NLRI 198.51.100.0/24
$ gobgp global rib 198.51.100.0/24 -a ipv4 (r1)
Network Next Hop AS_PATH Age Attrs
*> 198.51.100.0/24 127.0.2.134 65001 65004 00:00:22 [{Origin: i} {Med: 200}]
* 198.51.100.0/24 127.0.2.136 65003 65004 00:00:20 [{Origin: i} {Med: 10}]
* 198.51.100.0/24 127.0.2.135 65001 65004 00:00:02 [{Origin: i} {Med: 300}]
The iBGP observer received the new best path (via A1, MED 200) 0.50 s after A2's update; the eBGP neighbours got a re-advertisement without MED, as before. That is the intended use: the owner of the destination moves inbound traffic from one link to another without touching the neighbour's configuration. It is also the operational risk RFC 4451 describes. If the MED is derived from an IGP metric, every IGP change becomes a BGP change seen by your neighbours, and "if your MED 'flaps' every time your IGP metric flaps, your routes are likely going to be suppressed" by route flap damping.[3] Junos therefore offers metric-out igp delay-med-update, which holds back increases of an IGP-derived MED, by default for 10 minutes (med-igp-update-interval), and minimum-igp, which only ever lowers the advertised value.[11] RFC 4451 adds that MED variety reduces update packing, because prefixes can share one UPDATE only if all their attributes, MED included, are identical.[3]
MED and route reflectors: oscillation without a stable state
Deterministic MED fixes the local problem: one router no longer depends on arrival order. A worse problem appears when several routers each see only part of the paths, as with route reflection and confederations. RFC 3345 describes "Type I churn", which occurs only when both of these hold: "a single-level Route Reflection or AS Confederations design is used in the network AND the network accepts the BGP MULTI_EXIT_DISC (MED) attribute from two or more ASs for a single prefix and the MED values are unique."[5]
In the RFC's topology the route reflector Ra hears 10.0.0.0/8 from client Rb (via AS 10, MED 10, IGP cost 5) and from Rc (via AS 6, MED 1, IGP cost 4). The MEDs are from different ASes, so the IGP cost decides and Ra picks Rc. Rd, the other reflector, compares Rc's path (MED 1) with its own client Re (AS 6, MED 0) and picks Re. Now Ra sees Re's path too: Re beats Rc on MED, Rb beats Re on IGP cost, so Ra switches to Rb. Rd compares Rb (IGP 6) with Re (IGP 12) and switches to Rb, which withdraws Re's path from Ra, which brings Ra back to step 1.[5] FRR's documentation states the point clearly: deterministic evaluation "can not fix" this, because the cycle comes from the non-transitive preference spread over speakers that each hold a subset of the routes.[13]
The known remedies are design remedies: keep IGP costs inside a cluster lower than between clusters, stop comparing MEDs from different ASes or rewrite them to one value on ingress, or give the reflectors more paths. RFC 7964 specifies the last option: advertise each neighbour-AS group's best path with ADD-PATH (RFC 7911), which is enough to remove MED-induced oscillation.[3][5][6] The route-reflector parts of this series come back to this.
MED in the real Internet table
To see how networks really use MED we parsed the full RIPE RIS dump of collector rrc14 (Palo Alto, 3 December 2026 00:00 UTC) with our own MRT reader. A collector session is an eBGP session, so every MED in it was put there by the peer AS itself, or passed on by it against the rule.[17]
- 3,233,397 of 5,581,714 IPv4 routes (57.9 %) and 754,345 of 1,398,887 IPv6 routes (53.9 %) carry a MED. 1,080,515 of 1,106,938 IPv4 prefixes have a MED on at least one path.[17]
- The decision is per network, not per route. AS6762 sends MED 100 on all of its 1,073,229 routes. AS2914 sends a MED on all 1,073,181 routes with 86 different values, the most common being 2011 (468,661 routes). AS19151 uses 18 values, MED 0 on 73 % of its routes. AS1280 sends a MED on 13 of 1,101,241 routes, AS199524 on 2 of 1,073,852 and AS6939 on 17 of 173,084.[17]
- Only 140 distinct values exist across all IPv4 routes; 100 and 0 alone account for 61 %. Every one of the 3.99 million MED attributes (IPv4 and IPv6) has flags 0x80 and length 4, and no route carries 232-1 or 232-2.[17]
The same pattern shows on RouteViews' Cisco route server. On 3 December 2026 at 04:06 UTC, show ip bgp 8.8.8.0/24 listed 15 paths; three carried metric 0 (via 3356, 3549 3356 and 57866) and twelve carried none. The best path, via AS 101, had no MED, and since all first ASes differ, MED played no role in the choice.[18] The paths through AS 3356 carry MED 0 on every prefix we queried, which matches the RIS picture of networks that set MED for all routes or for none. Both data sets show the same thing: across the public Internet MED is mostly a constant, and the networks that rely on it do so with their direct customers and peers, where both sides have agreed what the numbers mean. That matches RFC 4451's observation that "commercial networks exchange MEDs with customers but not with bilateral peers" in many cases.[3]
Configuration examples
Configuration examples (syntax from vendor documentation, not run in our lab except where marked). Setting a MED towards a neighbour that has two links into your network, Cisco IOS XE:[8]
route-map MED-TO-ISP-LINK1 permit 10 set metric 100 ! route-map MED-TO-ISP-LINK2 permit 10 set metric 200 ! router bgp 65502 neighbor 192.168.20.2 remote-as 65501 neighbor 192.168.30.3 remote-as 65501 address-family ipv4 neighbor 192.168.20.2 route-map MED-TO-ISP-LINK1 out neighbor 192.168.30.3 route-map MED-TO-ISP-LINK2 out exit-address-family bgp deterministic-med
Cisco IOS XR, as used in the kazulog lab (R1 sets 200 towards R4; R4 changes the comparison scope):[16]
route-policy MED-200-OUT set med 200 pass end-policy ! router bgp 65001 neighbor 10.1.4.4 address-family ipv4 unicast route-policy MED-200-OUT out ! router bgp 65002 bgp bestpath med always bgp bestpath med missing-as-worst
Junos: a fixed MED per neighbour, an IGP-derived MED with delayed increases, and the path-selection knobs:[11][12]
set protocols bgp group external neighbor 172.16.24.2 metric-out 20 set protocols bgp group external-igp metric-out igp delay-med-update set routing-options med-igp-update-interval 10 set protocols bgp path-selection always-compare-med
GoBGP 4.11, exactly as run in phase B of our lab (TOML):[15]
[global.route-selection-options.config]
always-compare-med = true
[[policy-definitions]]
name = "x-med-300"
[[policy-definitions.statements]]
name = "med300"
[policy-definitions.statements.actions]
route-disposition = "accept-route"
[policy-definitions.statements.actions.bgp-actions]
set-med = "300"
Common mistakes and troubleshooting
- "My MED is ignored." Check the steps before MED on the neighbour's router: LOCAL_PREF (most providers set it per customer or per peer type, part 9), AS_PATH length, ORIGIN. MED only acts when those tie.[7]
- The two links land in different neighbour ASes. If you are dual-homed to two providers, MED from you to provider A means nothing to provider B. Use AS_PATH prepending or the providers' community-based actions (part 12) instead.
- The neighbour resets MED on ingress. RFC 4271 allows it and RFC 4451 describes it as common practice. Ask for the provider's routing policy.[1][3]
- Different exits from routers in the same AS. Mixed defaults (IOS without deterministic-med, Junos deterministic, IOS XR grouping) mean two routers with the same paths can choose differently. Configure the same MED behaviour everywhere, and change it on all routers together.[9][10]
- always-compare-med on only some routers. Cisco warns that this can cause routing loops. It is an AS-wide decision.[7]
- A path without MED wins unexpectedly. Missing = 0. Use
missing-as-worstwhere available, or set a MED on ingress for every route.[1][13] - MED churn from IGP-derived values. Neighbours see every IGP change. Use delayed updates (Junos) or a fixed MED per link.[3][11]
- Route reflectors and MEDs from several ASes. If best paths flap without any external change, look at RFC 3345 first: IGP costs between clusters, MED normalisation, ADD-PATH.[5][6]
- Reading the wrong metric. In
show bgpthe IGP cost to the next hop is in brackets after the next hop; the MED ismetric NafterOrigin.
FAQ
What is BGP MED?
MED (MULTI_EXIT_DISC, path attribute type 4) is an optional non-transitive 32-bit metric that a network attaches to the routes it advertises to a neighbouring AS. When that neighbour has several links into your network, a lower MED tells it which entry point you prefer. RFC 4271 defines it in section 5.1.4; lower values win.
Why is my BGP MED ignored?
Usually because something earlier in best-path selection already decided: a different LOCAL_PREF, a shorter AS_PATH or a better ORIGIN. The second common cause is that the competing paths come from different neighbouring ASes, and by default MED is only compared between paths from the same neighbouring AS. The neighbour may also reset or remove MED on ingress, which RFC 4271 explicitly allows.
What is the difference between always-compare-med and deterministic-med?
always-compare-med compares MED between all paths, even when they come from different neighbouring ASes. deterministic-med keeps the default rule (compare only within one neighbouring AS) but first groups the paths by neighbouring AS, picks the best of each group and then compares the group winners, so the result no longer depends on the order in which the routes arrived. They are independent knobs; enabling one does not enable the other.
Is a missing MED better or worse than MED 0?
Under RFC 4271 a missing MED is treated as 0, the best possible value, so a path without MED ties with MED 0 and beats every other MED. Cisco (bgp bestpath med missing-as-worst) and FRR offer a knob to treat a missing MED as 4,294,967,295 instead. In our GoBGP lab a path without MED beat a path with MED 1 from the same AS.
Does BGP MED pass to other autonomous systems?
No. RFC 4271 says a MED received from a neighbouring AS MUST NOT be propagated to other neighbouring ASes. It may be carried over iBGP inside the receiving AS. In our lab GoBGP passed MED 77 unchanged to its iBGP peer and sent the route to an eBGP peer without any MED, unless an export policy set a new value.
Can I set MED from the IGP metric?
Yes. Cisco IOS uses set metric-type internal in an outbound route-map, Junos uses metric-out igp (optionally with delay-med-update, default 10 minutes) and FRR has set metric igp. It gives cold-potato routing, but RFC 4451 warns that IGP-derived MEDs change whenever the IGP changes, which can create BGP churn and trigger flap damping at your neighbours.
Why do I need deterministic-med if my vendor already has it?
Junos orders paths deterministically by default and IOS XR 26.1.1 does not even accept the command, but Cisco IOS and IOS XE have it disabled by default and FRR has it as a knob. In a mixed network, every router must evaluate MED the same way, otherwise routers in one AS can choose different exits for the same prefix. Our GoBGP lab picked three different winners for the same three paths depending on arrival order.
Next in the series: part 14 looks at ORIGINATOR_ID, the second optional non-transitive attribute in Pat's tree, which a route reflector adds so that a route can never be reflected back into the router that originated it.
Series hub: BGP in 26 Parts: From EGP to EVPN
Previous: BGP Communities: Wire Formats and Well-Known Values
Next: ORIGINATOR_ID: The BGP Attribute Route Reflectors Add (coming soon)
References
[1] Y. Rekhter, T. Li, S. Hares (Eds.), RFC 4271: A Border Gateway Protocol 4 (BGP-4), January 2006: s.4.3 d, s.5.1.4, s.9.1.2.2 c, s.9.2.2.2.
[2] Y. Rekhter, T. Li, RFC 1771: A Border Gateway Protocol 4 (BGP-4), March 1995; name history per RFC 4451 s.2.1.
[3] D. McPherson, V. Gill, RFC 4451: BGP MULTI_EXIT_DISC (MED) Considerations, March 2006.
[4] E. Chen, J. Scudder, P. Mohapatra, K. Patel, RFC 7606: Revised Error Handling for BGP UPDATE Messages, August 2015: s.3 c, s.3 e, s.7.4.
[5] D. McPherson, V. Gill, D. Walton, A. Retana, RFC 3345: Border Gateway Protocol (BGP) Persistent Route Oscillation Condition, August 2002.
[6] D. Walton, A. Retana, E. Chen, J. Scudder, RFC 7964: Solutions for BGP Persistent Route Oscillation, September 2016.
[7] Cisco, Select BGP Best-path Algorithm (Document 13753), read 3 December 2026.
[8] Cisco, Use MultiExit Discriminator to Influence BGP Best Path Selection (Document 13759), revision 3.0, 30 July 2026.
[9] Cisco, How the bgp deterministic-med Command Differs from the bgp always-compare-med Command (Document 16046), August 2005.
[10] Cisco, Cisco IOS IP Routing: BGP Command Reference, bgp always-compare-med and bgp deterministic-med.
[11] Juniper Networks, BGP MED Attribute (Junos OS BGP User Guide): always-compare-med, med-plus-igp, cisco-non-deterministic, metric-out, read 3 December 2026.
[12] Juniper Networks, Understanding BGP Path Selection (Junos OS), read 3 December 2026.
[13] FRRouting, BGP documentation: MED section, bgp deterministic-med, bgp always-compare-med, bgp bestpath med missing-as-worst, read 3 December 2026.
[14] GoBGP, internal/pkg/table/destination.go (insertSort, compareByMED, compareByAge) and path.go, master branch, read 3 December 2026; policy.md (set-med).
[15] Network freak lab: GoBGP 4.11 routers r1 (127.0.2.131) and r2 (127.0.2.132), raw Python BGP speakers A1, A2, B, I, X (127.0.2.134-138), phases A, B and C, 3 December 2026, 05:0x CET. Configs, logs and every captured UPDATE are kept with the article sources.
[16] kazulog.info, BGP MED Attribute: Cisco XRd IOS XR 26.1.1 lab, STEP 0-5 show/syslog/running-config and packet captures (No.75, No.143), downloaded and decoded 3 December 2026.
[17] RIPE NCC, RIS rrc14 bview.20261203.0000.gz, parsed with our own MRT TABLE_DUMP_V2 reader on 3 December 2026.
[18] University of Oregon RouteViews, route-views.routeviews.org (telnet, user rviews, Cisco IOS), show ip bgp for 8.8.8.0/24, 1.1.1.0/24, 193.0.0.0/21 and 9.9.9.0/24, 3 December 2026 04:06-04:07 UTC.
Figures 1-9: Network freak, drawn from the sources above and our lab.
Try it: BGP Route Lookup: origin AS, AS paths and RPKI check - look up any prefix and see the paths that carry it, free and in your browser.
Related reading
- How BGP Best-Path Selection Picks a Route
- BGP MED: Influencing Inbound Traffic and Where It Fails
- How to Control Inbound BGP Traffic: AS_PATH Prepending, MED and Provider Communities
- How to Influence Inbound and Outbound BGP Traffic: LOCAL_PREF, Prepending and MED
- How BGP Route Reflectors Prevent Loops with CLUSTER_LIST
- How the BGP ORIGIN Attribute Affects Best-Path Selection
Comments
Post a Comment