Free tech learning for everyone, from basics to pro

Routing & SwitchingNetworking SecurityIntermediate

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.

OSPF and BGP at a glanceOSPFInterior gateway protocolLink-state, shortest pathFast convergenceInside one organisationBGPExterior gateway protocolPath-vector, policyVery large scaleBetween organisations, internet
Figure 1. OSPF finds the fastest path inside your network; BGP chooses paths between networks based on policy.

Side-by-side comparison

FeatureOSPFBGP
TypeInterior gateway protocol (IGP)Exterior gateway protocol (EGP)
AlgorithmLink-state (Dijkstra shortest path first)Path-vector
Path choiceLowest total cost (based on bandwidth)Attributes: local preference, AS path, MED and more
NeighboursDiscovered automatically on shared linksConfigured manually, over TCP port 179
ConvergenceFast, usually seconds or less with tuningSlower by default, tuned with BFD and timers
ScaleHundreds to a few thousand routers with areasThe whole internet, over a million routes
Administrative distance (Cisco)11020 for eBGP, 200 for iBGP
Typical useCampus, branch, data centre underlayInternet 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?
OSPF is an interior gateway protocol; BGP is the exterior gateway protocol.
How does OSPF choose a path, and how does BGP?
OSPF picks the lowest total cost; BGP uses an ordered list of attributes such as local preference and AS path.
Why should you not redistribute the full internet table into OSPF?
It is far too many routes for an IGP; advertise a default route instead.

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.
Tracks your learning on this device only. See your progress on the learning paths.
Verified author✓ Valid
Issued to
Mohamed Fayas
Role
Founder of FM TecHub
Credentials
CCNABTEC HND Network EngineeringBEng (Hons) Computer Networking & Cloud Security
Issuer
FM TecHub · fmtechub.com

He writes practical, plain-English guides on networking, security and cloud.

More about the author →

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.

Email updates: coming soon

New notes, tools and security news from FM TecHub by email, explained simply. Free. We're not collecting email addresses yet; see our privacy policy.

PortServiceStatus
SMTP/25Email updatesopening soon

Meanwhile, stay connected: