HomeGuides › BGP best path

How BGP decides which route wins

BGP compares candidate routes attribute by attribute and stops at the first difference. Knowing the order explains almost every surprising routing decision.

When BGP has several routes to the same prefix it picks one. The comparison runs down a list and stops at the first attribute where two routes differ, which is why an attribute compared early beats every consideration below it.

The order

  1. Highest weight — platform-specific, local to one router, and does not exist everywhere.
  2. Highest local preference — carried across your AS. The usual lever for choosing an exit.
  3. Locally originated beats learned.
  4. Shortest AS path.
  5. Lowest origin type — IGP beats EGP beats incomplete.
  6. Lowest MED — with the restriction below.
  7. eBGP over iBGP.
  8. Lowest IGP metric to the next hop.
  9. Lowest router ID, then lowest neighbour address.

Local preference and MED point in opposite directions

Local preference decides how traffic leaves your network. It is set on incoming routes, carried between your own routers, and never leaves your AS. Higher wins.

MED is a hint to your neighbour about how to send traffic in. It goes out with your advertisements and asks a neighbouring AS to prefer one of your entry points. Lower wins, and your neighbour is free to ignore it entirely.

People reach for MED to control outbound traffic and it does nothing, because outbound is local preference's job.

The MED comparison rule

MED is only compared between routes learned from the same neighbouring AS. This is the default nearly everywhere, and it surprises people constantly.

The reasoning is sound: MED is a number your neighbour chose, meaningful only against their own other entry points. Comparing a MED of 50 from one provider with a MED of 5 from another compares numbers that were never on the same scale — nothing stops a provider setting all theirs to zero.

Most platforms have a knob to compare them anyway. Turning it on changes which routes win, and it should be a deliberate decision rather than a reaction to a MED that seemed to be ignored.

Hot potato routing

The IGP metric step means that when everything above is equal, a router hands traffic off at the nearest exit rather than carrying it across the network. That is deliberate — you spend your own bandwidth for as short a distance as possible. It also means traffic can leave at a different place from where the return traffic arrives, which is normal and confuses a lot of troubleshooting.

Policy runs before all of this. Route maps, prefix filters and communities acting on local preference shape what the candidates even are. A best-path model shows the algorithm, not your configuration.

Frequently asked questions

What is the difference between local preference and MED?

Local preference controls how traffic leaves your AS and never leaves it; higher wins. MED is a hint to a neighbouring AS about which of your entry points to use; lower wins, and they may ignore it.

Why is my MED being ignored?

Most likely because the routes come from different neighbouring autonomous systems, and MED is only compared within one by default. It may also simply be that your neighbour's policy discards it.

Does AS path prepending always work?

It influences networks that reach the shortest-AS-path step. Anyone who set local preference for their own reasons decided before AS path was considered, so prepending does nothing there.

What is hot potato routing?

Handing traffic off at the nearest exit when all earlier attributes are equal, decided by the IGP metric to the next hop. It minimises how far traffic travels on your own network.

Open the best-path simulator →