The most surprising thing about dynamic routing is that routers don’t actually "know" the best path; they only know the next hop to get to a destination, and they learn these hops from their neighbors.
Let’s see OSPF in action. Imagine two routers, R1 and R2, connected via a direct Ethernet link.
R1
!
interface GigabitEthernet0/0
ip address 192.168.1.1 255.255.255.0
ip ospf 1 area 0
!
router ospf 1
network 192.168.1.0 0.0.0.255 area 0
!
R2
!
interface GigabitEthernet0/0
ip address 192.168.1.2 255.255.255.0
ip ospf 1 area 0
!
router ospf 1
network 192.168.1.0 0.0.0.255 area 0
!
After configuring OSPF like this, R1 and R2 will exchange "hello" packets on their GigabitEthernet0/0 interface. If the network segment (192.168.1.0/24) matches the network command in the router ospf 1 configuration, they’ll attempt to form an adjacency. Once adjacent, they exchange their Link State Advertisements (LSAs). R1 tells R2 about its directly connected links, and R2 tells R1 the same. Both routers then run the Dijkstra algorithm on the collected LSAs to build their Link State Database (LSDB). From this LSDB, each router independently calculates the shortest path to every known destination and installs that path into its routing table, pointing to the next hop (in this case, the IP address of the neighbor on the shared link).
This is the core of OSPF (Open Shortest Path First), an Interior Gateway Protocol (IGP) designed for use within a single autonomous system (AS). OSPF uses a link-state routing algorithm. Each router in an OSPF area floods information about its directly connected links (LSAs) to all other routers in the same area. This ensures every router has a complete map of the network topology within that area. The Dijkstra algorithm then calculates the shortest path based on link costs, which are typically inversely proportional to bandwidth.
Now, let’s consider BGP (Border Gateway Protocol), the Exterior Gateway Protocol (EGP) that makes the internet work. BGP is fundamentally different. It’s a path-vector protocol, meaning it exchanges entire path advertisements, not just link states.
Consider two ISPs, AS100 and AS200, peering with each other.
ISP1 (AS100)
!
router bgp 100
neighbor 192.0.2.2 remote-as 200
neighbor 192.0.2.2 password YOUR_SHARED_SECRET
neighbor 192.0.2.2 update-source Loopback0
neighbor 192.0.2.2 prefix-list MY_PREFIXES in
neighbor 192.0.2.2 route-map FILTER_IN in
!
ip prefix-list MY_PREFIXES seq 5 permit 203.0.113.0/24
!
route-map FILTER_IN permit 10
match ip address prefix-list MY_PREFIXES
!
ISP2 (AS200)
!
router bgp 200
neighbor 192.0.2.1 remote-as 100
neighbor 192.0.2.1 password YOUR_SHARED_SECRET
neighbor 192.0.2.1 update-source Loopback0
neighbor 192.0.2.1 prefix-list THEIR_PREFIXES in
neighbor 192.0.2.1 route-map FILTER_IN in
!
ip prefix-list THEIR_PREFIXES seq 5 permit 198.51.100.0/24
!
route-map FILTER_IN permit 10
match ip address prefix-list THEIR_PREFIXES
!
ISP1 advertises its network 203.0.113.0/24 to ISP2. ISP2 receives this advertisement and sees the path as AS100. ISP2, in turn, advertises its network 198.51.100.0/24 to ISP1, and ISP1 sees the path as AS200. Crucially, BGP routers don’t have a full map of the internet. They only know about the paths (sequences of AS numbers) that lead to specific prefixes. When a BGP router receives multiple paths to the same prefix, it uses a complex set of attributes (like AS_PATH length, local preference, MED, etc.) to select the "best" path. The selected path is then added to the routing table, with the next hop being the IP address of the BGP neighbor that advertised that path.
EIGRP (Enhanced Interior Gateway Routing Protocol) is a Cisco-proprietary hybrid routing protocol. It combines aspects of both distance-vector and link-state protocols. EIGRP uses the Diffusing Update Algorithm (DUAL) to calculate and converge on loop-free paths. It advertises only changes, similar to link-state, but it doesn’t maintain a full topology map like OSPF. Instead, each router maintains a table of its neighbors and the routes they advertise, along with their feasible successors (backup paths).
When a router receives a routing update, it compares the reported metric with its own best metric for that destination. If the new path is better, it’s installed. If it’s a valid feasible successor (meaning it has a reported metric less than the feasible distance of the current successor), it’s stored as a backup. If the primary path fails, DUAL can immediately switch to a pre-calculated feasible successor, offering very fast convergence.
The one thing most people don’t grasp about routing protocols is how they handle "metrics." For OSPF, it’s cost, derived from bandwidth. For EIGRP, it’s a composite metric involving bandwidth and delay, and it’s not directly comparable to OSPF costs. For BGP, the "metric" is a complex set of path attributes, and the goal isn’t necessarily the "shortest" path in terms of hops or bandwidth, but the path that best aligns with the policy of the AS. This policy-driven nature is why BGP is the backbone of the internet.
The next concept to explore is route redistribution, where you inject routes learned by one protocol into another.