Skip to content
Optimal Lab

Networking · interactive

Routing: longest prefix wins

A router doesn't know the whole path, only which neighbour to send each packet to next. Its table lists address ranges; when several match, the most specific (longest prefix) wins.

packet to 10.1.2.77

prefixnext hopmatch?
0.0.0.0/0ISP uplink (default route)✓
10.0.0.0/8Core router (all of 10.x)✓
10.1.0.0/16Office VPN (office network)✓
10.1.2.0/24Lab switch (one lab subnet)✓ longest
192.168.0.0/16LAN (local devices)–

→ forward to Lab switch

4 routes match 10.1.2.77. /24 is the most specific, so the packet goes to Lab switch.

Destination

Step 2 of 4

  1. 1

    Hop by hop

    Each router decides only the next hop. The full path emerges from many local decisions.

    ip route show

  2. 2

    Most specific wins

    10.1.2.77 is inside /8, /16 and /24. The /24 is the narrowest, so it's the most deliberate rule.

    Routers use tries to do this in nanoseconds.

  3. 3

    The default route

    0.0.0.0/0 matches everything and is the fallback: “send it to the ISP”.

    Your laptop's table is mostly this one line.

  4. 4

    BGP fills the internet's tables

    Networks announce which prefixes they can reach. A mistaken, more specific announcement can hijack traffic, which is how big outages happen.

    Cloud route tables work the same way.

In production

Tools and technologies

Every router and cloud network uses longest-prefix routing.

Not tied to one language — use these names when you search docs or design a system.