OSPF and BGP aren’t just different routing protocols; they’re fundamentally different tools for entirely different jobs, and picking the "wrong" one is like trying to hammer a nail with a screwdriver.

Let’s see OSPF in action. Imagine a small, self-contained network, like a company’s internal network.

// OSPF Configuration Snippet (Cisco IOS-like)

// Enable OSPF on the router
router ospf 1

// Assign a network to be advertised by OSPF, with a wildcard mask
// This means OSPF will run on interfaces within the 192.168.1.0/24 range
network 192.168.1.0 0.0.0.255 area 0

// Assign a router ID (unique identifier for this OSPF process)
// Often derived from an interface IP, but can be set manually
router-id 1.1.1.1

// Interface configuration for an interface participating in OSPF
interface GigabitEthernet0/1
 ip address 192.168.1.1 255.255.255.0
 ip ospf 1 area 0

In this snippet, router ospf 1 starts the OSPF routing process. The network 192.168.1.0 0.0.0.255 area 0 command tells OSPF to look at interfaces that fall within the 192.168.1.0/24 subnet and enable OSPF on them, advertising that network into "Area 0" (the backbone area). The router-id is crucial for uniquely identifying each router participating in OSPF. On the interface itself, ip ospf 1 area 0 explicitly enables OSPF on that specific interface, making it a neighbor to other OSPF routers on the same link.

OSPF is a link-state routing protocol. Every router running OSPF in an area builds a complete map (a Link-State Database or LSDB) of the entire area. When a link’s status changes (goes up or down), a Link-State Advertisement (LSA) is flooded throughout the area. Each router then runs the Dijkstra algorithm on its LSDB to calculate the shortest path to every other destination. This means OSPF converges quickly and has a detailed view of the network topology. It’s designed for speed and efficiency within a single administrative domain.

Now, let’s contrast this with BGP. BGP is an Exterior Gateway Protocol (EGP). Its primary job isn’t to figure out the fastest path within your network, but to figure out the best path between different autonomous systems (ASes) on the internet. An AS is typically a large network managed by a single entity (like an ISP or a major cloud provider).

// BGP Configuration Snippet (Cisco IOS-like)

// Enable BGP and define the AS number
router bgp 65001

// Define a neighbor (another AS) and its IP address
// The remote-as is the AS number of the neighbor
neighbor 203.0.113.2 remote-as 65002

// Advertise a network into BGP (your own network)
// This tells other ASes that you own this IP block
network 198.51.100.0 mask 255.255.0.0

// Optional: Set attributes for the advertised route
// For example, setting a lower local preference to make this path more desirable
// within your own AS if you have multiple BGP peers to the same external network.
// neighbor 203.0.113.2 local-preference 200

Here, router bgp 65001 starts the BGP process for AS 65001. neighbor 203.0.113.2 remote-as 65002 establishes a peering session with a router in a different AS (65002) at a specific IP address. The network 198.51.100.0 mask 255.255.0.0 command tells BGP to advertise the 198.51.100.0/16 IP block to its neighbors. The commented-out local-preference line shows how you can influence path selection by manipulating BGP attributes, which is a core function of BGP.

BGP is a path-vector routing protocol. Instead of having a full map, BGP routers exchange reachability information about prefixes (network blocks) along with the full path of ASes that must be traversed to reach that prefix. It’s less about calculating the shortest path and more about enforcing policies and reachability across the vast, interconnected internet. BGP’s convergence is much slower than OSPF, and it uses a complex set of attributes (like AS-PATH, MED, Local Preference, Origin) for path selection to implement business and routing policies.

The key difference boils down to scope and objective. OSPF is designed for internal routing (an Interior Gateway Protocol or IGP) within a single AS, prioritizing rapid convergence and efficient topology discovery. BGP is designed for external routing (an EGP) between ASes, prioritizing policy enforcement and inter-AS reachability.

You can run BGP internally, and some very large networks do, but it’s generally overkill and much more complex to manage than OSPF for that purpose. Conversely, trying to use OSPF to route between ISPs would be impossible, as it’s not designed to handle the scale or trust model of the global internet.

Most people don’t realize that BGP’s "best path" selection isn’t purely about hop count or speed; it’s a weighted decision based on a sequence of attributes, where policies can heavily influence which path is chosen. A route advertised by a peer might be technically reachable via fewer AS hops, but if your local preference is set higher for a different path, BGP will choose that one. This attribute manipulation is how organizations control traffic flow and interact with their upstream providers.

The next logical step after understanding BGP’s path selection is learning how to manipulate those attributes to influence traffic engineering.

Want structured learning?

Take the full Computer Networking course →