Students often ask which one is better, OSPF or BGP. That’s the wrong question, and once you see why, both protocols get much easier to understand. OSPF vs BGP isn’t a contest. They solve different problems, and in many real networks you’ll find both running on the same routers. This guide covers how each one works, where they differ, and which situations call for which.
OSPF vs BGP in One Glance
OSPF is a link-state interior gateway protocol. It finds the shortest path between routers inside one organisation’s network. BGP is a path-vector exterior gateway protocol. It exchanges routes between different organisations (autonomous systems) and lets you control, through policy, which paths get used.
If routing itself is still new to you, start with our guide on what is networking, then come back here.
What Is OSPF?
OSPF stands for Open Shortest Path First. Every OSPF router builds a complete map of its area, then runs the Shortest Path First (Dijkstra) algorithm on that map to pick the best route to each destination. The current IPv4 version, OSPFv2, is defined in RFC 2328, and OSPFv3 extends the idea to IPv6.
How OSPF Works
- Routers send Hello packets to find neighbours. On Ethernet the default is a Hello every 10 seconds and a dead interval of 40 seconds.
- Neighbours become adjacent and exchange link-state advertisements (LSAs) that describe their links.
- Every router in the area stores these in a link-state database (LSDB), and all routers in the area end up with the same copy.
- Each router runs SPF on its LSDB and installs the best routes in its routing table.
OSPF doesn’t use TCP or UDP. It runs directly over IP (protocol number 89) and uses multicast addresses 224.0.0.5 and 224.0.0.6 on most links.
OSPF Areas and the DR/BDR
Large networks are split into areas, all connected to a backbone called Area 0. Keeping each area small limits the size of the LSDB and stops every change from disturbing the whole network. On multi-access networks like Ethernet, OSPF also elects a Designated Router (DR) and a Backup DR so that routers don’t all exchange updates with each other.
OSPF Metric
OSPF uses cost, which is based on interface bandwidth. On Cisco devices the default reference bandwidth is 100 Mbps, so Fast Ethernet, Gigabit and 10-Gigabit links can all end up with the same cost of 1. On modern networks, engineers usually raise the reference bandwidth so faster links are treated as better paths.
What Is BGP?
BGP stands for Border Gateway Protocol. It’s the protocol that connects the internet together, and BGP-4 is defined in RFC 4271. Instead of building a map of links, BGP keeps a list of AS paths, meaning the sequence of autonomous systems a route has passed through.
eBGP and iBGP
- eBGP runs between routers in different autonomous systems, such as your company and its ISP.
- iBGP runs between routers in the same autonomous system.
Neighbours in BGP are configured by hand, and the session runs over TCP port 179. TCP sits at Layer 4 of the OSI model, so a BGP session depends on the underlying IP connectivity being in place first.
How BGP Chooses a Path
BGP doesn’t look for the “shortest” path by default. It compares path attributes. Some of the important ones are:
- Weight (Cisco-specific) and Local Preference, which decide the preferred exit from your own AS
- AS_PATH length, the number of autonomous systems a route crosses
- Origin and MED, which help compare routes further
- Communities, tags used to apply policy
This is what gives BGP its strength. An engineer can say “prefer ISP-A for outbound traffic, use ISP-B as backup” with a few lines of policy.
Loop Prevention in BGP
An eBGP router rejects any route that already has its own AS number in the AS_PATH. Inside an AS, iBGP works differently: a router doesn’t pass routes learned from one iBGP peer to another iBGP peer. That’s why iBGP needs a full mesh of neighbours, or route reflectors or confederations in bigger designs.
OSPF vs BGP: Key Differences
| Point | OSPF | BGP |
|---|---|---|
| Protocol type | Interior gateway protocol (IGP) | Exterior gateway protocol (EGP) |
| Algorithm | Link-state (SPF) | Path-vector |
| Main scope | Inside one autonomous system | Between autonomous systems (also used inside large networks) |
| Transport | Directly over IP (protocol 89) | TCP port 179 |
| Neighbour discovery | Automatic with Hello packets | Manually configured |
| Metric | Cost (based on bandwidth) | Path attributes (AS_PATH, Local Preference, MED, etc.) |
| Administrative distance (Cisco) | 110 | 20 (eBGP), 200 (iBGP) |
| Policy control | Limited | Extensive |
| Structure | Areas, with Area 0 as backbone | Autonomous systems, peers and route policies |
| Typical use | Campus, data centre and WAN routing within one organisation | ISP connections, multihoming, service provider and large-scale designs |
Convergence and Scalability
OSPF generally reacts quickly to a link failure. Routers flood the change, recalculate SPF and update their tables. Its scaling limit comes from the size of the LSDB, which is why area design matters.
BGP is built for a different kind of scale: a huge number of prefixes across many independent networks. Its default timers are also more relaxed (on Cisco IOS, a 60-second keepalive and 180-second hold time), because stability across the internet matters more than speed. Both protocols can be tuned, and features like BFD help detect failures faster. So the honest answer to “which converges faster” is that it depends on the design and tuning.
When to Use OSPF
- Routing inside a campus, office or data centre network
- Connecting a head office and branch offices in one organisation
- Networks that need automatic neighbour discovery and quick reaction to changes
- The IGP that gives iBGP its reachability to loopback addresses
When to Use BGP
- Connecting your network to an ISP or to more than one ISP
- Controlling which path inbound and outbound traffic takes
- Service provider designs, including MPLS VPN customer connections
- Very large networks that need policy-based routing decisions, and some large data centre fabrics that use eBGP by design
Not every company needs BGP. A small office with a single ISP usually uses a static default route, and that’s perfectly fine.
Using OSPF and BGP Together: A Practical Example
Take a company with a head office, a data centre and 15 branches. It has two ISP connections and its own public IP block.
- Inside the company: OSPF connects the head office, data centre and branches, so every router knows how to reach every internal subnet.
- At the internet edge: two edge routers run eBGP, one to each ISP, and advertise the company’s public prefix. Local Preference makes ISP-A the main exit and ISP-B the backup.
- Between the two edge routers: an iBGP session runs over loopback addresses, and OSPF provides the reachability that keeps that session up.
Each protocol does the job it’s best suited for.
Basic Configuration Examples (Cisco IOS)
OSPF:
router ospf 1
router-id 1.1.1.1
network 10.0.0.0 0.0.0.255 area 0
BGP (eBGP to an ISP):
router bgp 65001
neighbor 203.0.113.2 remote-as 65002
network 192.0.2.0 mask 255.255.255.0
The IP addresses and AS numbers above are for practice only. Notice that BGP needs the neighbour and its AS number stated explicitly, while OSPF finds neighbours on its own.
Useful verification commands:
show ip ospf neighborandshow ip route ospfshow ip bgp summaryandshow ip bgp
Common Mistakes to Watch For
- OSPF neighbours stuck: mismatched Hello and dead timers, area IDs, subnet masks or MTU are the usual causes.
- Discontiguous Area 0: every area must connect to the backbone, or you need a proper fix such as a virtual link.
- BGP network statement not advertising: the exact prefix and mask must already exist in the routing table.
- iBGP routes missing on some routers: this is normally the iBGP rule about not passing routes between iBGP peers, and it needs a full mesh or route reflector.
- Careless redistribution: pushing BGP routes into OSPF, or the reverse, without filters can flood your internal network with routes or create loops.
OSPF vs BGP for CCNA and CCNP
OSPF appears early in a networking career. CCNA covers OSPFv2 basics such as neighbour formation, DR/BDR and simple configuration. BGP goes deeper at the professional level, where you work on path attributes, policy and multi-router designs. Exam blueprints change from time to time, so check Cisco’s official page for the current topic list. You can also look at our CCNP ENCOR syllabus guide, and if you want to start with the basics, our CCNA course covers routing from the ground up.
Frequently Asked Questions
1. What is the main difference between OSPF and BGP?
OSPF is a link-state IGP used to route inside one organisation. BGP is a path-vector EGP used to exchange routes between autonomous systems and to apply routing policy.
2. Is OSPF an IGP or an EGP?
OSPF is an interior gateway protocol (IGP). BGP is classified as an exterior gateway protocol (EGP).
3. Can OSPF replace BGP on the internet?
No. OSPF wasn’t designed for routing between independent organisations. Its flooding model and limited policy control don’t suit that job.
4. Can BGP be used instead of OSPF inside a company network?
Technically yes, and some large data centres do use BGP internally. For a typical campus or branch network, OSPF is often simpler to run because it discovers neighbours automatically. The right choice depends on the design and the team managing it.
5. Can OSPF and BGP run together on the same router?
Yes. This is common at the network edge, where OSPF handles internal routing and BGP handles the ISP connection.
6. What are the administrative distances of OSPF and BGP?
On Cisco devices, OSPF is 110, eBGP is 20 and iBGP is 200.
7. Do I need to learn BGP for CCNA?
CCNA focuses mainly on OSPF. BGP is studied in more depth at the CCNP level, though it’s worth checking the current blueprint before you plan your studies.
Conclusion
OSPF and BGP aren’t rivals. OSPF keeps a single organisation’s network connected and reactive, while BGP connects that network to the outside world and gives engineers control over paths. Learn OSPF first, get comfortable reading its neighbour and route outputs, and then move to BGP. If you want hands-on lab practice with both, explore the CCNP Encor + Enarsi course at PM Networking.

0 Comments