Introduction
Routing is the process by which routers determine the best path to forward a packet toward its destination network. Every router maintains a routing table, a list of known destination networks and the next hop or outgoing interface to reach each one, and consults it for every packet it forwards.
Cricket analogy: Just as a captain consults a mental playbook of field placements to decide where to throw the ball next, a router consults its routing table to determine the best next hop for each packet.
Explanation
Routes can be configured in two fundamental ways. Static routing means an administrator manually enters fixed routes into a router's routing table; these routes do not change unless someone edits them, which makes static routing simple and predictable for small, stable networks but impractical at scale since it cannot automatically adapt to link failures or topology changes. Dynamic routing uses routing protocols (such as RIP, OSPF, or BGP) that let routers automatically exchange information about reachable networks and recalculate routes when the topology changes, making it suitable for large or frequently changing networks despite added protocol overhead and complexity. When a packet arrives, the router compares the packet's destination IP address against every entry in its routing table and selects the route whose network prefix matches the most leading bits of the destination address — this is called longest prefix match. If multiple routes could match a destination, the router always prefers the more specific (longer prefix) match over a more general one, falling back to a default route (0.0.0.0/0) only when no other entry matches.
Cricket analogy: Static routing is like a fixed batting order set before the season that never changes, while dynamic routing like OSPF is like a captain adjusting the order live based on form; a specific /25-style plan for a spinner-heavy pitch beats a general default strategy.
Example
# Longest prefix match example
routing_table = [
("0.0.0.0", 0, "default-gateway"), # default route, least specific
("192.168.0.0", 16, "iface-A"),
("192.168.1.0", 24, "iface-B"),
("192.168.1.128", 25, "iface-C"), # most specific for 192.168.1.130
]
dest_ip = "192.168.1.130"
# Conceptually: the router checks each entry and picks the one with the
# longest matching prefix. 192.168.1.130 falls within:
# 192.168.0.0/16 (matches, 16 bits)
# 192.168.1.0/24 (matches, 24 bits)
# 192.168.1.128/25 (matches, 25 bits) <- longest prefix, wins
best_route = max(
(r for r in routing_table if r[1] <= 32),
key=lambda r: r[1],
)
print("Chosen route:", best_route)
# Chosen route: ('192.168.1.128', 25, 'iface-C')Analysis
Longest prefix match ensures traffic uses the most specific known path rather than a broad, generic one, which is essential for efficient and correct forwarding: a /25 route describing a small subnet takes priority over an overlapping /16 or /24 route that covers a much larger address block, and the /0 default route is used only as a last resort catch-all. Static routing works well for a small office with a single stable path to the internet, but in a data center or ISP network with hundreds of interconnected routers and links that fail or change, dynamic routing protocols are necessary so routers can automatically discover alternate paths without manual reconfiguration.
Cricket analogy: A specific field plan targeting one dangerous batter beats a generic all-purpose field setting, just as a /25 route beats an overlapping /16; a small club with one ground uses a fixed plan, but a national board with many venues needs adaptive scheduling.
Key Takeaways
- Static routes are manually configured and fixed; dynamic routes are learned and updated automatically by routing protocols.
- Routers use longest prefix match: the routing table entry with the most specific (longest) matching prefix wins.
- The default route (0.0.0.0/0) is used only when no more specific route matches.
- Dynamic routing adapts to topology changes automatically; static routing requires manual updates.
Practice what you learned
1. What is the key difference between static and dynamic routing?
2. What does 'longest prefix match' mean when a router forwards a packet?
3. When does a router use its default route (0.0.0.0/0)?
4. Why is dynamic routing generally preferred in large or frequently changing networks?
Was this page helpful?
You May Also Like
Network Layer Basics
Learn how the network layer handles logical addressing, packet forwarding, and fragmentation to move data across networks.
Routing Protocols
Distinguish distance-vector, link-state, and path-vector routing protocols including RIP, OSPF, and BGP.
IP Addressing and Subnetting
Master IPv4 addressing and CIDR subnetting with a fully worked, hand-verified example.
Hubs, Switches, and Routers
Learn how hubs, switches, and routers operate at different OSI layers and how they shape collision and broadcast domains.