Cloud concept · Updated

Longest Prefix Match - Longest Prefix Match is the routing rule that selects the matching destination prefix with the greatest number of network bits.

In short

When several routes cover a packet's destination address, the most specific matching prefix normally determines its next hop. This principle explains how default routes, network summaries, and more-specific exceptions interact across cloud and hybrid networks, independently of the connectivity service carrying the traffic.

Cloud Concepts Hub →

6 key aspects of Longest Prefix Match

Longest prefix matching provides deterministic destination-based forwarding, while propagation, equal-prefix priorities, and failure handling determine which routes are usable.

01
Most-specific destination wins

Among applicable matching routes, the route with the longest prefix is selected. A matching /24 takes precedence over a /16, regardless of which covers more total addresses.

02
Default routes provide last-resort reachability

The IPv4 default route, 0.0.0.0/0, and IPv6 default route, ::/0, match all destinations in their respective address families. More-specific applicable routes take precedence over them.

03
Learning routes is separate from forwarding

BGP and automatic propagation determine which routes become available. Prefix specificity determines forwarding among different matching prefixes, while identical-prefix alternatives require platform-specific priority rules.

04
Each routing boundary makes its own decision

Subnet route tables, transit routing tables, and on-premises routers perform separate lookups. Validate every forwarding stage and the return path rather than inspecting only the source subnet.

05
Summaries and exceptions balance scale and control

Aggregated prefixes reduce routing-table size, while more-specific routes steer selected networks through different connections. Exceptions can increase route consumption, traffic costs, and asymmetric-routing risk.

06
Failure does not imply less-specific fallback

An unusable selected route can drop traffic even when a broader route exists. Reliable failover depends on supported withdrawal, health detection, or route-update behavior rather than prefix matching alone.

Cloud services for Longest Prefix Match

VPC route tables select the initial target for subnet traffic, such as a peering connection or transit gateway. Transit Gateway then performs another lookup using the route table associated with the ingress attachment. Site-to-Site VPN and Direct Connect provide hybrid paths whose prefixes can be learned through BGP and propagated where configured. Across these services, compare destination specificity before considering identical-prefix preferences. Validate table associations, propagation, route state, and return routing together; neither a preferred connectivity service nor a broader backup route overrides a more-specific selected route automatically.

Amazon VPCAmazon VPC

VPC route tables apply destination-prefix matching to direct subnet traffic toward supported targets. Inspect the subnet's actual route-table association, and account for AWS-specific local-route and equal-prefix priority rules.

AWS Transit GatewayAWS Transit Gateway

Transit Gateway applies longest prefix matching within the route table associated with the ingress attachment. Attachment association determines which table is consulted, while propagation determines which attachment routes populate selected tables.

AWS Site-to-Site VPNAWS Site-to-Site VPN

Site-to-Site VPN supports hybrid reachability using static routing or dynamic routing with BGP, depending on the configuration. More-specific VPN routes can steer traffic away from broader routes through other connections, so verify advertisements and withdrawal behavior.

AWS Direct ConnectAWS Direct Connect

Direct Connect uses BGP to exchange prefixes over virtual interfaces. A more-specific route through another connection can attract matching traffic even when Direct Connect is preferred for a broader prefix.

Longest Prefix Match exam questions

Real practice exam questions on Longest Prefix Match, with the correct answer and its explanation. Answer them with every option in the exam simulator.

SAA-C03

A company runs EC2 workers in a private subnet in Availability Zone A. The subnet is explicitly associated with a custom VPC route table that contains these routes:

• 0.0.0.0/0 → a public NAT gateway in Availability Zone A
• 198.51.100.0/24 → a public NAT gateway in Availability Zone B

Both NAT gateways must remain operational for other workloads. The workers upload large files to a vendor's public servers throughout 198.51.100.0/25. These servers now allow connections from either NAT gateway's Elastic IP address. Other vendor servers in 198.51.100.128/25 still allow connections only from the NAT gateway in Availability Zone B. Security rules permit all required traffic.

Which routing change will eliminate cross-Availability Zone transfer charges for the large uploads while preserving access to all vendor servers?

Show answerCORRECT ANSWER

Add a route for 198.51.100.0/25 targeting the NAT gateway in Availability Zone A to the custom route table.

For the large uploads, the /25 route takes precedence over both the /24 route and the default route. These uploads therefore use the NAT gateway in the workers' Availability Zone. Destinations in 198.51.100.128/25 do not match the new route and continue to use the existing /24 route through Availability Zone B.

ANS-C01

A company connects an application VPC to its on-premises network, 172.28.0.0/16, through two existing paths: a legacy AWS Site-to-Site VPN attached to a virtual private gateway and a higher-capacity Site-to-Site VPN attached to an AWS Transit Gateway.

The application subnet's VPC route table contains these routes:
• 172.28.0.0/16 → transit gateway
• 172.28.64.0/20 → virtual private gateway

The transit gateway route table associated with the application VPC attachment contains an active route for 172.28.0.0/16 to the higher-capacity VPN attachment, with no more-specific routes for that network.

A new bulk-transfer workload sends traffic to servers in 172.28.72.0/24. Monitoring shows that this traffic uses the legacy VPN and saturates its capacity. The company must move traffic destined for 172.28.72.0/24 to the higher-capacity VPN without changing the forwarding path for any other on-premises destination. Both paths are healthy, security controls permit the traffic, and on-premises return routing is already configured appropriately.

Which single routing change meets these requirements?

Show answerCORRECT ANSWER

Add a route for 172.28.72.0/24 to the transit gateway in the application subnet's VPC route table.

The /24 route takes precedence over the existing /20 and /16 routes for the bulk-transfer destinations. After reaching the transit gateway, the traffic follows its active /16 route to the higher-capacity VPN. Other destinations retain their existing route selections.

Practice Longest Prefix Match in a full exam

Questions on Longest Prefix Match in each practice exam, with every answer option and an explanation for each:

Start a free practice exam

What is Longest Prefix Match

3 min read

Longest Prefix Match compares a packet's destination IP address against destination prefixes in the applicable forwarding table. A prefix consists of an address and a prefix length, such as 10.20.0.0/16. The length identifies how many leading bits must match. A longer matching prefix describes a smaller address range and is therefore more specific. IPv4 and IPv6 use the same principle, but their routes belong to separate address families and are evaluated independently.

Suppose a routing table contains 0.0.0.0/0 toward an internet gateway, 10.0.0.0/8 toward a hybrid connection, and 10.20.0.0/16 toward a private network attachment. Traffic for 10.20.5.7 matches all three destinations, but the /16 route wins. Traffic for 10.30.5.7 matches the /8 and default routes, so the /8 wins. The default route is the least specific IPv4 route and is used only when no more-specific applicable route matches.

Route selection must be evaluated at each routing boundary. In AWS, a subnet's associated VPC route table first determines whether traffic goes toward a peering connection, transit gateway, virtual private gateway, or another supported target. If traffic reaches a transit gateway, the transit gateway route table associated with the ingress attachment determines its next hop. A correct route in one table does not compensate for a missing or incorrect route in another. Return traffic also needs a valid path, although its route need not be identical to the forward path.

Distinguish forwarding lookup from learning and selecting routes. BGP exchanges reachability information, and route propagation makes eligible learned routes available in configured routing tables. Neither process replaces longest prefix matching. A learned /24 can be preferred for matching destinations over a static /16. When routes have the same destination prefix, however, prefix length cannot distinguish them; route-source priority and platform-specific selection rules apply. For example, AWS VPC and Transit Gateway route tables prefer a static route over a propagated route for an identical destination prefix. AWS also defines special routing rules for certain targets and destinations, including VPC local routing, so not every theoretically possible override is supported.

For a hybrid design, consider a transit gateway table containing 172.16.0.0/16 toward a Direct Connect gateway attachment and 172.16.40.0/24 toward a VPN attachment. Traffic to 172.16.40.10 takes the VPN path despite the broader Direct Connect route. This can intentionally steer a branch network, but it can also unexpectedly move traffic onto a higher-latency or differently priced connection. BGP path attributes for the /16 do not make it outrank the matching /24 during forwarding lookup.

More-specific routes provide precise traffic steering, while summarized routes reduce route count and operational complexity. Excessive exceptions consume route quotas, complicate troubleshooting, and can create asymmetric paths through stateful firewalls. Summaries also require care: an aggregate can continue attracting traffic even when a component network is unreachable. A selected route with an unavailable target, or an explicit blackhole route, does not generally cause forwarding to retry a less-specific route. Failover requires the relevant route to be withdrawn, replaced, or otherwise changed by the platform's supported mechanisms.

When troubleshooting, identify the destination address, inspect the table actually used at each hop, find every matching prefix, and then evaluate the longest match and any same-prefix priority rules. Check route propagation settings, BGP advertisements, route state, and the return path. Finally, verify security groups, network ACLs, and appliance policies separately: selecting a route does not authorize traffic. Longest prefix matching also does not make VPC peering transitive or turn an unsupported connectivity topology into a supported one.

Learn Longest Prefix Match in lessons

1 short lesson on Longest Prefix Match in 1 certification course.

Open the lessons