longest-prefix match
Longest-prefix match is the rule a router uses to pick which forwarding-table entry applies when several entries match the same destination address: it chooses the most specific one — the entry whose network prefix is the longest. Think of postal sorting rules: 'mail for the USA goes to bin 1, but mail for California specifically goes to bin 7'. A letter to California matches both rules, and the more specific California rule wins.
Here is why overlap happens. A router's table holds CIDR prefixes of different lengths that can cover the same address. Suppose it has entries for 10.0.0.0/8 → port 1, 10.1.0.0/16 → port 2, and 10.1.2.0/24 → port 3. A packet to 10.1.2.99 matches all three prefixes, because its leading bits fit each one. Longest-prefix match resolves the tie by picking the entry with the most matching bits — here /24 (24 bits) beats /16 and /8 — so the packet leaves via port 3. This is what lets a network advertise a general route while a customer overrides a slice of it with a more specific one.
Longest-prefix match is the heart of the data plane, and doing it fast is a serious engineering problem because tables can hold a million prefixes and the match must finish in nanoseconds at line rate. Routers use specialized data structures (tries) or special memory (TCAM) to find the longest match in roughly constant time. A handy corner case: the default route 0.0.0.0/0 is a prefix of length zero that matches everything, so it always loses to any more specific entry — which is exactly why it serves as the catch-all of last resort.
Table: 10.0.0.0/8 → port 1, 10.1.0.0/16 → port 2, 10.1.2.0/24 → port 3. A packet for 10.1.2.99 matches all three; the /24 has the most matching bits, so it wins and the packet goes out port 3.
When prefixes overlap, the most specific (longest) one wins.
It is most-specific, not first-match: a router considers all matching prefixes and picks the longest, regardless of table order. The default route (/0) matches everything but always loses to any longer prefix.