OSPF vs BGP: differences, how they work and when to use each
In short: OSPF is an interior gateway protocol that quickly calculates the shortest path inside one organisation using link costs. BGP is the exterior gateway protocol of the internet: it exchanges routes between organisations and chooses paths using policy attributes such as AS path and local preference. Most larger networks use both, OSPF inside and BGP at the edges.
Quick answer
In short: OSPF is for routing inside your own network: it automatically finds the fastest path and reacts quickly to failures. BGP is for routing between networks, such as your company and its internet providers or cloud: it is slower and more manual but scales to the whole internet and lets you control paths with policy.
Side-by-side comparison
| Feature | OSPF | BGP |
|---|---|---|
| Type | Interior gateway protocol (IGP) | Exterior gateway protocol (EGP) |
| Algorithm | Link-state (Dijkstra shortest path first) | Path-vector |
| Path choice | Lowest total cost (based on bandwidth) | Attributes: local preference, AS path, MED and more |
| Neighbours | Discovered automatically on shared links | Configured manually, over TCP port 179 |
| Convergence | Fast, usually seconds or less with tuning | Slower by default, tuned with BFD and timers |
| Scale | Hundreds to a few thousand routers with areas | The whole internet, over a million routes |
| Administrative distance (Cisco) | 110 | 20 for eBGP, 200 for iBGP |
| Typical use | Campus, branch, data centre underlay | Internet edge, multi-homing, cloud, MPLS VPN, large data centres |
How OSPF thinks
Every OSPF router learns the full map of its area: which routers exist, which links connect them and the cost of each link. It then runs the shortest path first algorithm to build its routing table. If a link fails, the change is flooded and every router recalculates quickly. You mostly influence OSPF by changing link costs and designing areas. See OSPF design.
How BGP thinks
BGP does not know the full map. Each router tells its neighbours which networks it can reach and the path of autonomous systems (AS numbers) to get there. Routers choose between paths using an ordered list of attributes, so you can say "prefer ISP A for outbound traffic" (local preference) or "make ISP B less attractive for inbound traffic" (AS path prepending). It is a policy tool as much as a routing protocol. See BGP at the internet edge.
How they work together
A typical company runs OSPF inside its campus and data centres, and BGP with its internet providers, cloud and MPLS carrier. At the edge, the routers advertise a default route into OSPF and announce the company's public prefixes to the providers with BGP. Large data centres sometimes use BGP everywhere, because it scales well and gives fine control.
Which should you use?
- Single site or campus, one internet line: OSPF (or even static routes); no BGP needed.
- Two internet providers and your own public IP range: BGP with both providers, OSPF inside.
- Connecting to cloud private links or an MPLS VPN: usually BGP, because providers require it.
- Large spine-leaf data centre: often BGP (eBGP between switches) or OSPF underlay with BGP EVPN overlay.
Common mistakes
- Redistributing the full BGP table into OSPF, which overwhelms routers. Advertise a default route instead.
- Forgetting BGP filters, so you accidentally advertise someone else's routes (a route leak).
- Assuming BGP picks the fastest path; it picks the path your policy prefers.
Real-world example: two ISPs, one plan
A college with two internet providers used static default routes and lost internet for an hour whenever the main provider had problems upstream, because the link itself stayed up. Moving to eBGP with both providers, local preference for the main one, and a default route injected into the campus OSPF made failover automatic and fast.
Practice
Which protocol is an IGP and which is an EGP?
How does OSPF choose a path, and how does BGP?
Why should you not redistribute the full internet table into OSPF?
Key takeaways
- OSPF routes inside one organisation using the shortest-cost path.
- BGP routes between organisations using policy attributes.
- Most larger networks use OSPF inside and BGP at the edges.
- Choose BGP when multi-homing, connecting to clouds or carriers, or at very large scale.
Original content written by Mohamed Fayas for FM TecHub. © 2026 FM TecHub. You're welcome to share a link to this page; please don't copy the text elsewhere without permission.