JOVANA
Explore Library Glossary Getting Started Three Levels Fields How it works Mission
Join the mission
All guides

How BGP Chooses a Route: Policy over Distance

Inside a network, routing hunts for the shortest path. Between networks, BGP often ignores the shortest path on purpose, because money and trust matter more than mileage. This guide walks the decision process a router runs to pick one route out of many, and shows why local preference beats AS-path length.

The same destination, several offers

In the previous guide you saw that the Border Gateway Protocol is a path-vector protocol: a network advertises not a cost but a whole AS path, the list of autonomous systems a packet would cross to reach a prefix. The catch is that a real router usually hears about the same prefix from more than one neighbour. Your provider offers you a way to reach 10.0.0.0/8, but so does a peer at an exchange point, and so does a second provider. Three offers for one destination. BGP's central job is to pick exactly one of them to use and to (maybe) pass on.

Here is the idea that surprises everyone arriving from interior routing. Inside one network, an interior gateway protocol like OSPF picks the least-cost path, the genuinely shortest route. Between networks, BGP frequently does not pick the shortest AS path at all. It picks the route that is best for business, and only uses path length as a late tie-breaker. The reason is everything from the earlier guides in this rung: a route through a customer earns money, a route through a peer is free, and a route through a provider costs money. Mileage is cheap; the wrong relationship is expensive.

The decision ladder, top to bottom

BGP chooses by walking down an ordered list of comparisons, like a series of increasingly fine tie-breakers. It starts at the top with the most powerful, most political attribute and only drops to the next rule when the current one leaves two or more routes tied. The first rule that separates the contenders decides the winner, and the rest are never even consulted. This top-down structure is the whole secret to why policy wins: the business levers sit at the top of the ladder, and the shortest-path-ish rules sit near the bottom.

BGP best-path selection (simplified, per prefix)

  1.  Highest LOCAL PREFERENCE   <- our own policy knob (business)
  2.  Shortest AS_PATH           <- fewest ASes to cross (distance-ish)
  3.  Lowest ORIGIN type
  4.  Lowest MED                 <- neighbour's hint, only vs same neighbour
  5.  eBGP over iBGP             <- learned externally beats internally
  6.  Lowest IGP cost to next hop  ("hot-potato": leave our net fast)
  7.  Lowest router-ID            <- final arbitrary tie-break

  Walk down. The FIRST rule that breaks the tie wins; stop there.
A simplified BGP best-path ladder. Local preference (policy) sits above AS-path length (distance), which is why a longer route can still win.

Notice the order is not arbitrary marketing. Rule 1 is local to your network and reflects what you want. Rule 2 is the closest thing to distance, fewest ASes crossed, and it is only the second tie-breaker, not the first. By the time you reach rule 6 you are choosing between routes that are already equal on policy, length, and origin, so the network falls back to plain interior cost. The famous label hot-potato routing lives here: among equally-good exits, shove the packet out of your own network as fast as possible so someone else carries it the long way.

Local preference: the business knob at the top

The most important attribute is local preference, a number a network attaches to incoming routes to say, in effect, how much it likes each one. Higher wins. Crucially it is purely internal: it is set by you when a route arrives and shared only among your own routers over iBGP, never sent to other networks. Because it sits at the very top of the ladder, it overrides AS-path length completely. A route with local preference 200 and a five-AS path beats a route with local preference 100 and a one-AS path, even though the second is shorter.

This is exactly how an operator encodes the relationship economics from earlier in this rung. The standard recipe: give routes learned from a customer the highest local preference, routes from a peer a medium value, and routes from a provider the lowest. Now the decision ladder will always prefer sending traffic out via a customer (you get paid), then via a peer (free), and only reluctantly via a provider (you pay), regardless of which path looks shortest. That single knob turns the abstract provider-customer relationship into concrete routing behaviour.

AS-path length and the weaker hints below it

When local preference ties, BGP finally looks at distance, but a coarse kind: the number of ASes in the AS path, not hops, not delay, not bandwidth. A path crossing three autonomous systems is preferred over one crossing five, even if those three are sprawling continent-spanning networks and the five are tiny and fast. It is a deliberately blunt measure, but it doubles as loop prevention: a router that sees its own AS number already in a received path rejects that route outright, because it would mean the packet looped back through itself.

Operators exploit this rule in reverse. By artificially repeating their own AS number in the path they advertise, called AS-path prepending, they make a route look longer and therefore less attractive to everyone downstream, gently nudging inbound traffic onto a different link. It is a hint, not a command: any neighbour whose local preference favours the prepended path will still use it, because preference sits above path length on the ladder. This is the recurring theme of inter-domain routing, you can influence your neighbours but you cannot dictate to them.

Lower still sits the multi-exit discriminator (MED), the gentlest tool of all. When two networks connect at several points, one can attach a MED to each route to say, in effect, 'if you are sending traffic to me, please use the link with the lower MED.' But it only breaks a tie between routes learned from the same neighbouring AS, and your neighbour is free to ignore it entirely, because MED sits far down the ladder beneath local preference and AS-path length. It is the most polite of suggestions, easily overruled by anyone's policy.

Putting it together: a worked choice and the wider picture

Let us trace one decision. A router in your network holds three routes to the prefix 10.0.0.0/8. Route A came from a customer, AS path of length 4. Route B came from a peer, AS path of length 2. Route C came from a provider, AS path of length 1, the shortest of all. Which wins? Your policy set local preference to 200 for the customer route, 100 for the peer, 50 for the provider. The ladder never even reaches AS-path length.

  1. Rule 1, highest local preference: A has 200, B has 100, C has 50. A wins outright. Stop.
  2. The shortest route, C through the provider, is rejected even though it crosses only one AS, because earning revenue from the customer link beats minimising distance.
  3. Route A is installed in the forwarding table and re-advertised; B and C stay in memory as backups. If A is ever withdrawn, the ladder re-runs and B (next-highest preference) takes over.

Now zoom out. The routers running this decision for the full Internet, holding routes to roughly a million prefixes with no fallback default route, live in the default-free zone: the core where 'I don't know, send it to my upstream' is not an option because there is no upstream. Every operator in that zone runs their own private version of the ladder with their own routing policy, so the path your packet takes is the emergent result of thousands of independent business decisions, not a global optimum anyone planned.

This is why BGP is both powerful and fragile, the theme of the next guide. The same flexibility that lets each network encode its economics also means a single mistaken or malicious advertisement, a more-attractive route to a prefix you do not actually own, can be accepted and spread by the ladder before anyone notices. Policy over distance gives the Internet its real-world shape; it also gives it route leaks and hijacks. Hold on to the ladder, and the wrong turns in guide 5 will make complete sense.