BGP vs OSPF: how they differ and where each belongs
BGP is a policy-driven exterior protocol (EGP) between autonomous systems. OSPF is an interior IGP that picks routes from a cost metric. Scale and use cases.
Last updated:
BGP and OSPF are two routing protocols that work at different layers of the network. BGP (Border Gateway Protocol, RFC 4271) is an exterior gateway protocol (EGP) that exchanges routes between autonomous systems and selects paths based on policy. OSPF (Open Shortest Path First, RFC 2328 for IPv4 and RFC 5340 for IPv6) is an interior gateway protocol (IGP) that computes shortest paths within a single administrative domain using a cost metric. OSPF knows how to reach every router inside your network, while BGP decides which neighbour to use to reach the rest of the internet.
| Aspect | BGP | OSPF |
|---|---|---|
| Protocol class | EGP, between autonomous systems | IGP, inside one AS |
| Algorithm | Path vector | Link state (Dijkstra) |
| Route selection basis | Policy: local-pref, AS path, MED | Cost metric derived from bandwidth |
| Scale | Full DFZ table, over a million routes | Hundreds to thousands of routes per area |
| Convergence | Slower, measured in minutes | Fast, measured in seconds |
| Transport | TCP, port 179 | Directly over IP, protocol 89 |
| Typical use | Peering, transit, multihoming | Internal routing and paths to next hops |
EGP versus IGP: where the boundary sits
The protocols differ first of all in administrative scope. OSPF runs inside a single autonomous system (AS) and keeps a full topology map in its link-state database (LSDB). BGP runs at the edge with other networks. Every eBGP session connects two different AS numbers, while iBGP distributes external routes inside your own AS. That is why OSPF is an IGP and BGP is classed as an EGP.
- OSPF: one AS, one trust domain, full topology visibility.
- BGP: many separate AS, no trust between networks, visibility at the prefix and attribute level.
- OSPF runs directly over IP (protocol number 89). BGP uses a TCP session on port 179.
- OSPF is not driven by operator policy. BGP route selection is.
Policy versus metric: how each picks a path
OSPF selects a path with the SPF algorithm (Dijkstra) based on cumulative link cost, so the decision is deterministic and purely technical. BGP picks a path through a multi-step decision process where policy attributes come first and path length only later. This lets an operator steer traffic independently of the hop count.
- OSPF: link cost (default, vendor-dependent, 10^8 / bandwidth), lowest total wins.
- BGP: in order Weight, Local Preference, AS-Path, Origin, MED, then a preference for eBGP over iBGP.
- OSPF converges in seconds thanks to fast LSA flooding.
- BGP changes decisions via communities, AS-Path prepend and Local Preference, not the physical link metric.
Scale: route count and stability
OSPF is designed for tens to a few hundred routers within one organisation. Large domains are split into areas to limit LSDB size and SPF load. BGP is built for the global table, which by 2026 had passed one million IPv4 prefixes and over 200,000 IPv6 prefixes. OSPF could not carry a full internet table, because every change would trigger an SPF recalculation across the whole domain. So you reach the internet over BGP, while OSPF supplies internal reachability for the next hops.
BGP and OSPF in the AS202520 SkyPass network
In AS202520 SkyPass, OSPF (or another IGP) provides internal reachability between edge routers across our points of presence (PoPs) in Warsaw and Wrocław, while BGP handles all edge policy with the outside world: IP transit, peering at Polish internet exchanges (THINX, TPIX, WRIX, 1-IX) and remote IXP access. If you run your own AS, your IGP keeps your topology converged while eBGP sessions with us decide how traffic reaches the rest of the internet. Where needed, we support traffic engineering through communities and Local Preference.
Frequently asked questions
Can BGP replace OSPF inside a network?
Not in practice. BGP does not discover topology or react to link changes as fast as an IGP. Inside an AS you almost always use OSPF or IS-IS for reachability, and iBGP to distribute external routes.
Do I need OSPF if I only have one edge router?
Not necessarily. With a single router, static routes plus one eBGP session to your provider are enough. OSPF starts to pay off when you have multiple routers and need automatic internal reachability.
Why does BGP inside an AS need an IGP?
iBGP advertises a next-hop, but it is the IGP (for example OSPF) that must know how to physically reach that next-hop. Without a working IGP, iBGP routes are unresolved and stay inactive in the table.
Can OSPF and BGP run on the same router?
Yes, that is standard on edge routers. OSPF maintains the internal topology while BGP handles sessions with providers and IXPs. Redistribution between them is done carefully so a full table is never injected into the IGP.
