How a router chooses: prefix length, AD, metric, ECMP, and the four algorithm families

Path selection has three parts, in order of importance: prefix length, administrative distance, metric. Each protocol first picks its own best path by its metric; the routing table then compares administrative distance between sources offering the same prefix; and forwarding always uses the longest matching prefix. Equal-cost paths can share the load.

Free to read17 min read · 9 min video · lab: EIGRP beside OSPF

By , founder of Daveth · Updated

Opening frame of the video walkthrough of How a router chooses: prefix length, AD, metric, ECMP, and the four algorithm families

Watch this lesson

9 min · Video walkthrough

In Daveth Pro (Network)

The full 9 min walkthrough is in Daveth Pro (Network). See what the plan opens

Upcoming practical lab

Lab E5: EIGRP beside OSPF

What the lab covers

What this page answers

How does a router choose between routes?

In order: the longest matching prefix always wins; among sources offering the same prefix, the lowest administrative distance is installed; within one protocol, the lowest metric is the best path. Read it in “4.1 Where routes come from”

What is the difference between an IGP and BGP?

Interior gateway protocols, RIPv2, EIGRP, OSPF and IS-IS, route inside an autonomous system; BGP, the exterior gateway protocol, routes between them, though iBGP also runs inside one. Read it in “4.1 Where routes come from”

4.1 Where routes come from

A router's job is to move a packet from one network to another. Networks it's attached to, it knows. Everything else it learns: from static routes someone typed, or from dynamic routing protocols. Dynamic protocols share what they know about the topology and adjust by themselves when something changes. Static routes don't adapt, and at scale they become a burden. It's basically like the difference between a map you drew yourself (static) and directions that update themselves when a road closes (dynamic). With dynamic protocols, the routers try to pick the best path that doesn't loop.

A network under one administration is an autonomous system (AS), or routing domain; the Internet is thousands of them. The common protocols are RIPv2, EIGRP, OSPF, IS-IS and BGP. All but BGP are Interior Gateway Protocols (IGPs): they route inside an AS. Exterior Gateway Protocols (EGPs) route between ASes, and BGP is the one. BGP can run inside an AS too: iBGP inside, eBGP between. You can think of it as a country's roads: every town maintains its own streets its own way, and the motorway authority only cares how towns connect.

Static routing scales beautifully, right up until the engineer who typed it goes on holiday.

A distance vector protocol, like RIP, advertises each route as a distance and a direction: the distance is the metric, such as hop count, and the vector is the next hop or interface. A router stores what its neighbours send, runs an algorithm such as Bellman-Ford to find the best paths, installs them, and advertises them on. Each router advertises from its own point of view, changed from what it heard. So nobody has a map of the network, only "my neighbour says it's this far". The upside: little CPU and memory.

The guide's own comparison is the right one: a road sign at a junction saying the destination is 2 miles west. Drivers follow it without knowing whether there's a shorter road, or whether the sign is even right. And because distance is all it counts, it ignores link speed. In Figure 6-2 RIP sends R3's traffic over the 64 kbps serial link from R1 to R7, because it's fewer hops, and that link drops packets when it fills.

The enhanced kind is EIGRP, whose algorithm is DUAL (the diffusing update algorithm). It still advertises to neighbours like a distance vector protocol, but it converges fast, sends updates only when something changes (a plain distance vector protocol sends its whole table periodically), forms neighbour relationships with hellos, "just as link-state protocols do", uses bandwidth, delay, reliability, load and MTU instead of hops, and can load-balance over equal or unequal paths. (The guide lists these here as the family's traits; how EIGRP builds its metric and chooses its paths is 02-8.) Distance vector advertising plus link-state-style neighbours: that mix is why it's sometimes called hybrid (section 4.3 explains the link-state side). By default it advertises the path's total delay and minimum bandwidth, and each router works out the best path from what its neighbours tell it. In Figure 6-3, a plain distance vector protocol takes R3→R1→R7, two hops through the 64 kbps link; EIGRP takes R3→R1→R2→R7, a hop longer and much faster.

Think about the last time you took the "shortest" route on a map and it turned out to be a single-track lane behind a tractor. Hop count is that map.

D1

Network drawing: 64 kbps, R2, R1, R7, R3, R4, R5, R6, 192.168.1.0/24
With hop count, R3→R1→R7 is two hops and wins, straight across the 64 kbps serial link. R3 can’t know that link is slow: all it holds is “via R1, 2”.

RIP will happily route your video call down a 64 kbps link, because it's one hop shorter. Confidence is a wonderful thing.

A link-state protocol works the other way round. Each router advertises the state and metric of each of its own links, and which routers it connects to, to every router. OSPF calls these advertisements LSAs; IS-IS calls them LSPs. A router stores each one in its link-state database (LSDB) and passes it on exactly as it arrived. Nothing is re-written along the way, so every router ends up with the same map. If you've ever had a message forwarded to you word for word, with nobody's edits in it, you've seen the flooding rule; here that faithfulness is the whole point, because it's what makes every router's map the same. Then each router runs Dijkstra's shortest path first (SPF) algorithm over that map, from itself, and installs the results.

That usually costs more CPU and memory than distance vector, but it's less prone to loops and makes better choices. The protocols also carry extensions, opaque LSAs in OSPF and TLVs (type/length/value fields) in IS-IS, that service providers use for things like MPLS traffic engineering. (OSPF's LSA types are 02-4's; MPLS isn't in this course.) The guide's comparison: a GPS with the whole map, rather than a road sign. In Figure 6-4, R3 reaches 192.168.1.0/24 through R1 (10 + 10 = 20) rather than its own direct link to R4 (50), and R1, running the same algorithm on the same map, takes its direct link to R4. In a way, it's just a family sharing one floor plan of the shop on their phones: everyone sees the same plan, unedited, and each person works out their own route round it.

D2

  1. Network drawing: 20, 10, 50, R2, R1, R4, R3, 192.168.1.0/24

    Step 1 of 5 Each router knows only its own links.

  2. Network drawing: 20, 10, 50, R2, R1, R4, R3, 192.168.1.0/24, has R3’s LSA

    Step 2 of 5 R3 advertises its own links. Each router that receives the LSA stores it and passes it on unchanged: nobody adds their own cost or rewrites it.

  3. Network drawing: 20, 10, 50, R2, R1, R4, R3, 192.168.1.0/24, LSDB: 4 LSAs

    Step 3 of 5 Every router does the same with its own links. Now every router holds the same map, the same four LSAs, synchronised.

  4. Network drawing: 20, 10, 50, R2, R1, R4, R3, 192.168.1.0/24, LSDB: 4 LSAs

    Step 4 of 5 R3 runs Dijkstra’s SPF from itself. Its own direct link to R4 costs 50; through R1 it’s 10 + 10 = 20. R3 uses R1.

  5. Network drawing: 20, 10, 50, R2, R1, R4, R3, 192.168.1.0/24, LSDB: 4 LSAs

    Step 5 of 5 R1 runs the same algorithm on the same map and takes its direct link to R4. Same map, each router its own shortest path: that’s link state.

Link-state routers gossip, but with a strict rule: repeat it word for word. The rest of us should try it.

BGP is a path vector protocol. It's like distance vector in that it advertises to neighbours, but it doesn't choose by distance. It chooses by path attributes: AS_Path, MED, origin, next hop, local preference, atomic aggregate and aggregator. (Which one wins, in what order, is 02-11.)

It stays loop-free by writing down every AS a route has passed through. A router that receives a route with its own AS already in the path rejects it: accepting it would be a loop. In Figure 6-5, 10.1.1.0/24 leaves AS 1, picks up AS 2, then AS 4, then AS 3. When R3 sends it back to R1 and R2 with AS_Path 3 4 2 1, R1 sees AS 1 and R2 sees AS 2, and both throw it away.

It's basically like a parcel covered in customs stamps: if one arrives with your own country's stamp on it already, it's been round the world and come back, and you don't sign for it. Just like when you forward an email and see your own name three replies down the thread: you know it's gone in a circle.

D3

  1. Network drawing: AS 1, AS 2, AS 3, AS 4, 10.1.1.0/24, R1, R2, R3, R4

    Step 1 of 5 R1 in AS 1 advertises 10.1.1.0/24. Follow the AS_PATH.

  2. Network drawing: AS 1, AS 2, AS 3, AS 4, 10.1.1.0/24, R1, R2, R3, R4, AS_PATH 1

    Step 2 of 5 Step 1: R1 adds its AS, 1, as it advertises to R2.

  3. Network drawing: AS 1, AS 2, AS 3, AS 4, 10.1.1.0/24, R1, R2, R3, R4, AS_PATH 1, AS_PATH 2 1

    Step 3 of 5 Step 2: R2 adds AS 2 in front and advertises to R4.

  4. Network drawing: AS 1, AS 2, AS 3, AS 4, 10.1.1.0/24, R1, R2, R3, R4, AS_PATH 1, AS_PATH 2 1, AS_PATH 4 2 1

    Step 4 of 5 Step 3: R4 adds AS 4 and advertises to R3.

  5. Network drawing: AS 1, AS 2, AS 3, AS 4, 10.1.1.0/24, R1, R2, R3, R4, AS_PATH 1, AS_PATH 2 1, AS_PATH 4 2 1, AS_PATH 3 4 2 1, AS 1 already in path: rejected, AS 2 already in path: rejected

    Step 5 of 5 Steps 4 and 5: R3 adds AS 3 and advertises back to R1 and R2. Each finds its own AS in 3 4 2 1 and discards the route: accepting it would be a loop. No metric, no timer: just a record of where the route has been.

BGP's loop prevention is the only filing system in networking that works by reading its own name.

Now the rulebook. Path selection has three parts, in order of importance: prefix length, administrative distance, metric. The forwarding decision is made from the FIB (Forwarding Information Base), which is programmed from the RIB (the routing table), which is filled by the routing processes. One thing about timing: each protocol has already used its own metric to pick its best path before it offers anything to the RIB; the RIB then compares AD; and the forwarding lookup uses the longest prefix. So "metric third" says which comparison counts least, not which happens last.

Prefix length is the number of leading 1 bits in the mask. Routes with different prefix lengths are different destinations, even when they start at the same address, so all of them go into the table. Table 6-2 has three: 10.0.3.0/28, /26 and /24, each with its own next hop. The longest prefix always wins. A packet to 10.0.3.14 matches all three and uses the /28. A packet to 10.0.3.42 matches the /26 and the /24 and uses the /26. A packet to 10.0.3.100 matches only the /24. Think about the last time you gave a taxi driver the street and then the house number: the street gets you close, and the more specific detail decides where you actually stop.

The same rule applies to IPv6, because prefix length means the same thing there: a packet to 2001:db8:1::5 with 2001:db8:1::/64 and 2001:db8::/32 in the table uses the /64.

Predict before you read on: RIP offers 10.0.3.0/28 (AD 120) and EIGRP offers 10.0.3.0/24 (AD 90). Where does a packet to 10.0.3.14 go? Keep your answer; D4 lets you type it in.

Here's the part people get wrong. AD and metric only ever compare routes to the same prefix, so a /28 from a protocol you trust less still beats a /24 from one you trust more, for every address the /28 covers. It works behind the scenes just like post sorting: a letter addressed to a flat number goes to that flat, not just the building, however official the building's address looks.

D4

Routing table lookup for 10.0.3.42: two prefixes cover it, and the longest, 10.0.3.0/26, is chosen before administrative distance is compared.
10.0.3.42 is covered by 2 prefixes, and the longest, 10.0.3.0/26, wins. EIGRP (AD 90) is more trusted, and it doesn’t matter: AD only compares routes to the same prefix.

Longest match is the only rule in networking where being more specific always wins the argument. Engineers should try it in meetings.

When more than one routing source offers the same prefix, the router compares administrative distance (AD): how much it trusts the source. Lower is more trusted. You can think of it as finding your way in a strange town: what you can see with your own eyes (connected) beats the directions you wrote down yourself (static), which beat what the locals tell you (the IGPs, most trusted first), which beat a stranger passing through (iBGP). The defaults:

SourceAD
Connected0
Static1
EIGRP summary route5
eBGP20
EIGRP (internal)90
OSPF110
IS-IS115
RIP120
EIGRP (external)170
iBGP200

Each protocol offers the RIB its prefix, next hop, AD and metric. If the RIB doesn't have that prefix, it takes it. If it has one with a lower AD, the new one is rejected and its protocol is told; if the new one has the lower AD, it replaces the old, and the old protocol is told. In Table 6-4, EIGRP (90), OSPF (110) and IS-IS (115) all offer 10.3.3.0/24: EIGRP's goes in. The losers keep their routes and ask to be told if the winner fails. If EIGRP's route goes, OSPF's takes over, being the next lowest AD. Have you ever noticed how, when your trusted source is out of reach, you fall back on the next one without a second thought? That's the RIB calling OSPF when EIGRP's route goes.

A subtlety: each protocol offers only its own best path. If BGP's own selection prefers an iBGP path, BGP offers it at AD 200, not 20, so it doesn't preempt a route from OSPF at 110: the RIB then compares 200 against 110, and the lower AD wins. The guide says such cases are "almost never seen"; the point is the order: the protocol picks first, then the RIB compares AD. And yes, you can change AD. The guide's warning: it can cause routing loops and odd behaviour, so only with extreme caution, and only after thinking it through.

D5

  1. Network drawing: EIGRP · AD 90, OSPF · AD 110, IS-IS · AD 115, RIB: 10.3.3.0/24

    Step 1 of 4 Several routing processes know the same prefix. Each will offer the RIB its best path.

  2. Network drawing: EIGRP · AD 90, OSPF · AD 110, IS-IS · AD 115, RIB: 10.3.3.0/24, installed: EIGRP, rejected · notified · waiting

    Step 2 of 4 EIGRP (90) goes in. OSPF and IS-IS are told they lost, and hang on to their routes, asking to be told if EIGRP’s fails.

  3. Network drawing: EIGRP · AD 90, OSPF · AD 110, IS-IS · AD 115, RIB: 10.3.3.0/24, route lost, waiting

    Step 3 of 4 EIGRP loses its route. The RIB entry goes with it.

  4. Network drawing: EIGRP · AD 90, OSPF · AD 110, IS-IS · AD 115, RIB: 10.3.3.0/24, route lost, installed: OSPF, installed, still waiting

    Step 4 of 4 The RIB calls the waiting processes, and the lowest AD among them, OSPF at 110, goes in. Nothing had to be re-learned: OSPF had the route all along.

OSPF and IS-IS spend their whole lives as understudies, word-perfect, waiting for EIGRP to catch a cold.

Within one protocol, the metric decides. Each protocol measures differently, and most IGPs prefer internal routes to external ones before comparing metrics. (What makes a route external: 02-5 for OSPF, 02-8 for EIGRP.)

When a protocol finds several paths with the same best metric, and allows several, the router installs them all, up to the maximum allowed: equal-cost multipathing (ECMP). RIP, EIGRP, OSPF and IS-IS all support it, and traffic is split equally across the paths. That's roughly equivalent to two checkout queues moving at the same speed: you split the trolleys evenly. In Figure 6-6, R1 has two equal OSPF paths to 10.3.3.0/24, and the routing table shows both:

O     10.3.3.0/24 [110/30] via 10.12.1.2, 00:49:12, GigabitEthernet0/2
                  [110/30] via 10.14.1.4, 00:49:51, GigabitEthernet0/4

(The figure labels each path "Metric 20"; the output's 30 also counts the cost to reach the subnet behind R3.)

By default, only the lowest-metric paths go in. EIGRP can be configured, and isn't by default, to install paths with different metrics too, and send traffic in proportion: unequal-cost load balancing. Here you only read the result; which paths qualify, and the knob that allows it, are 02-8's. In Figure 6-7 the delay on R1's link to R2 was raised, so the path through R4 is better. R1 installs both, [90/3328] via R4 and [90/5632] via R2, and show ip route 10.3.3.0 shows how it shares them: a traffic share count of 120 through R4 and 71 through R2. (The guide's text on p133 swaps the two paths' metrics; the figure and the output agree with each other, and with the raised delay.) If you scaled that down to a household, it's two people carrying shopping in from the car: the stronger one takes more bags, but both carry some.

Unequal-cost load balancing is the only place in networking where the slow path still gets invited to the party, just to fewer songs.

Commands and configuration

This lesson configures nothing: it reads the routing table. The protocols' configuration is 02-2 onward.

TaskCommand
The whole routing table, with [AD/metric] per pathshow ip route
One protocol's routesshow ip route eigrp
One prefix, with every path and its traffic shareshow ip route 10.3.3.0

Verification and troubleshooting

CommandProvesDoes NOT prove
show ip routewhich route won each prefix, its [AD/metric], and every ECMP next hophow traffic is shared between the paths ("the explicit path must be viewed"); that the next hop actually forwards end to end
show ip route <prefix>the source, AD, metric, every path and its traffic share countthat losing protocols still hold the prefix: check each protocol's own table
show ip route <protocol>what that protocol installedwhat it knows but lost on AD; the traffic ratio between its paths (view the prefix)

Funnel ("traffic isn't taking the route I expected"). Is there a more specific prefix covering the destination? (It wins regardless of AD.) → Same prefix from several sources: which AD is lowest? → Same protocol: which metric? → Several equal: it's ECMP.

Bad instinct: lowering a protocol's AD to "fix" a path, when the real cause is a more specific route elsewhere.

Real-world examples

  • A /32 host route injected for a server beats the aggregate everyone forgot about, and traffic takes an odd path.
  • A site running OSPF and EIGRP in parallel during a migration: EIGRP internal routes (90) win every prefix both protocols offer at the same length, until they're removed; EIGRP external routes (170) lose to OSPF (110).
  • A WAN pair where one link is slower: EIGRP's unequal-cost load balancing uses both, in proportion.

Common misunderstandings

WrongRight
"The lowest AD always wins."Only between routes to the same prefix; longest match comes first.
"A /24 and a /28 for the same network conflict."They're different destinations; both are installed.
"Distance vector routers see the topology."They see neighbours' distances only.
"Link-state routers re-write what they pass on."LSAs are flooded unchanged.
"A protocol offers all its paths to the RIB."Only its best path(s), chosen by its own rules first (several with ECMP or EIGRP unequal cost); BGP may offer iBGP at 200.
"Unequal-cost load balancing is on by default."It's an EIGRP option, off by default.

In production

Design summaries and specific routes with longest match in mind: it overrides everything else. Leave AD alone unless a migration genuinely needs it, and write down why. Expect ECMP with OSPF on equal links; reach for EIGRP's unequal cost only where the paths really differ.

The interactive lesson

The whole lesson, with its drawings and its questions. Free with a Daveth account.

Open the lesson