# Understanding BGP Deterministic-MED

Rashmi Bhardwaj | Blog,Protocol,Routing & Switching

To understand the use of BGP deterministic-MED we must first understand behavior of BGP algorithm when a route is received on a router via multiple paths.

## Understanding BGP Algorithm

BGP algorithm assigns the first valid path as the current best path. This first valid path is then compared with the next path listed. 1st and 2nd path are compared and out them the one chosen as best is then compared with 3rd path and so on until the end of paths in the list is reached.

## What is BGP deterministic-MED?

Because of the way BGP’s best path algorithm works, it can lead to MED being left uncompared between routes using identical AS_PATH’s towards a prefix.

As we all are aware the BGP always compare-MED feature ensures the MED gets compared when different AS are advertising the same route BGP Deterministic-med feature ensures the MED gets compared for a route being advertised from different paths in same AS.

## How does it work?

Let us take the example of below diagram to further understand how BGP deterministic MED works.

In the diagram below R5 advertises its loopback 5.5.5.5/32 towards R1.

R1 gets this update from 3 paths R2, R3 & R4 with different MED values.

R3 & R4 are within the same AS 300 and R2 is within AS 200.

### Routing Table on R1:

R1(config-router)#do sh ip bgp 5.5.5.5BGP routing table entry for 5.5.5.5/32, version 29

Paths: (3 available, best #3, table Default-IP-Routing-Table)

Flag: 0x4842

1

300 400

9.9.13.3 from 9.9.13.3

Origin IGP, metric 100, localpref 100, valid, external

200 400

9.9.12.2 from 9.9.12.2

Origin IGP, metric 150, localpref 100, valid, external

300 400

9.9.14.4 from 9.9.14.4

Origin IGP, metric 200, localpref 100, valid, external, best

According to the BGP best path algorithm the most recent path is evaluated against the path below it.

Between the Route from R3 and R2 , R2 is chosen as the best path. This is so because the MED isn’t compared for BGP updates from R2 & R3 as they are part of different AS and as per BGP algorithms working the older route is preferred one which here is from R2.

Now comparing routes from R2 with route from R4 again MED isn’t compared as they are both from different AS. R4 being the older route here is preferred over the route from R2 and hence is selected as the best. So we see in this scenario how the MED working of preferring the lower MED updates hasn’t taken effect in this scenario.

To overcome this situation we use BGP deterministic-med on R1. Deterministic-med rectifies this problem by listing one route from same AS_PATH under a group in BGP table. Paths within a group are then compared against each other. Then the best of the group is compared against the next group down.

### Enabling BGP Deterministic-MED

R1(config)#router bgp 100R1(config-router)#bgp deterministic-med

R1(config-router)#end

### Routing Table on R1 now:

R1(config-router)#do sh ip bgp 5.5.5.5BGP routing table entry for 5.5.5.5/32, version 29

Paths: (3 available, best #3, table Default-IP-Routing-Table)

Flag: 0x4842

1

200 400

9.9.12.2 from 9.9.12.2

Origin IGP, metric 100, localpref 100, valid, external

300 400

9.9.13.3 from 9.9.13.3

Origin IGP, metric 150, localpref 100, valid, external best

300 400

9.9.14.4 from 9.9.14.4

Origin IGP, metric 200, localpref 100, valid, external,

Now we see the table has been restructured and the routes with same AS path are grouped together.

Routes from R3 and R4 are compared and since route from R3 has lower MED it is best out of the group. Now route from R3 is compared with the only route of other group i.e. from R2 and since the route from R3 is older it is preferred and selected as best based on BGP’s algorithm default working.