OSPF vs BGP: When to Use Each Routing Protocol

OSPF runs inside your network. BGP connects your network to the world. Understanding when to use each is essential for any network engineer.

Run BGP, then compare it with OSPF

Free interactive lab. No signup. Runs in your browser.

Quick Summary

The Building vs City Analogy

Think about how you navigate inside an office building versus how you navigate between cities. Inside a building, you want the shortest path to the conference room -- take the elevator to floor 3, turn left, second door on the right. The building's internal navigation is fast, simple, and optimized for speed. That is what OSPF does for a network.

Now think about traveling between cities. You do not just want the shortest road -- you might prefer a toll-free highway, or a route that avoids a particular country, or a path through a city where your company has a peering agreement. Interstate travel involves policies, politics, and business decisions. That is what BGP does. BGP is not about finding the shortest path -- it is about finding the best path according to complex policies set by each network operator.

Both protocols are essential, but they solve fundamentally different problems. OSPF is designed for speed and efficiency within a trusted network. BGP is designed for policy-based routing across organizational boundaries where no single entity is in control. Trying to use one where the other belongs leads to serious problems -- which is why understanding the distinction matters.

How OSPF and BGP Work Together

OSPF handles routing inside each autonomous system, while BGP handles routing between them:

AS 100 (Company A) OSPF runs inside R1 R2 R3 R4 OSPF AS 200 (Company B) OSPF runs inside R5 R6 R7 R8 OSPF BGP eBGP peering "I have 10.0.0.0/8" "I have 172.16.0.0/12" OSPF (internal) BGP (between ASes) Autonomous System

Try It Yourself: Route Decision Simulator

See how OSPF picks the lowest-cost path while BGP picks the shortest AS-path.

OSPF: Cost-Based Selection

Set the cost on each link. OSPF picks the path with the lowest total cost.

SRC DST R1 R2 R3

BGP: AS-Path Selection

BGP prefers the route with the shortest AS-path (fewest autonomous systems to traverse).

Path A
AS-path: 64501 64502 64503 64504
Length: 4 ASes
Path B
AS-path: 64510 64511
Length: 2 ASes
Path C
AS-path: 64520 64521 64522
Length: 3 ASes

OSPF: The Internal Router

OSPF (Open Shortest Path First) is a link-state routing protocol. Every OSPF router builds a complete map of the entire network topology. When a router needs to send a packet, it uses Dijkstra's algorithm to calculate the shortest path from itself to every destination. This is fundamentally different from distance-vector protocols (like RIP) that only know the direction and distance to each destination without seeing the full picture.

OSPF routers share information using Link-State Advertisements (LSAs). When a link goes up or down, the affected router floods an LSA to all other OSPF routers in the area. Every router then recalculates its routing table based on the updated topology. This flooding mechanism means OSPF converges quickly -- typically within seconds of a topology change. Fast convergence is critical inside an organization where users expect uninterrupted service.

For large networks, OSPF supports areas. The backbone area (Area 0) connects all other areas, and routers only maintain a full topology database for their own area. This hierarchical design reduces the amount of information each router must process. An OSPF area in a typical enterprise might have 50-200 routers. OSPF uses IP protocol number 89 and communicates using multicast addresses 224.0.0.5 and 224.0.0.6.

BGP: The Internet's Routing Protocol

BGP (Border Gateway Protocol) is a path-vector routing protocol. While OSPF finds the shortest path, BGP finds the best path based on policies. BGP is the protocol that holds the internet together -- every ISP, cloud provider, and large enterprise uses BGP to exchange routing information with other networks. The internet is a collection of approximately 80,000 autonomous systems (ASes), and BGP is how they tell each other which IP address ranges they can reach.

BGP makes decisions based on a long list of attributes, not just hop count or link cost. The most important attribute is AS_PATH -- the list of autonomous systems a route has passed through. Shorter AS paths are generally preferred. But BGP also considers LOCAL_PREF (internal policy preference), MED (metric suggested by a neighbor), NEXT_HOP, and community tags. Network operators use these attributes to implement complex policies like "prefer routes through ISP-A over ISP-B" or "never send traffic through AS 12345."

BGP deliberately converges slowly compared to OSPF. When a route changes, BGP routers wait before propagating the update (using MRAI timers, typically 30 seconds for eBGP). This prevents cascading oscillations across the global internet. If BGP converged as fast as OSPF, a single flapping link could cause routing instability across thousands of networks. The internet prioritizes stability over speed.

Side-by-Side Comparison

1

Scope: Inside vs Outside

OSPF operates within a single autonomous system (AS). It is an IGP (Interior Gateway Protocol) designed for a network under one administrative control -- your company, your campus, your data center. BGP operates between autonomous systems. It is the EGP (Exterior Gateway Protocol) that connects different organizations. You run OSPF inside your building; you run BGP to talk to the building next door.

2

Decision Logic: Shortest Path vs Policy

OSPF calculates the shortest path based on link cost (usually derived from bandwidth). A 10 Gbps link has a lower cost than a 1 Gbps link, so OSPF prefers it. The algorithm is purely mathematical -- the shortest path wins, always. BGP uses a complex selection process with over a dozen criteria. An administrator can override any BGP decision using LOCAL_PREF, AS_PATH prepending, or route maps. BGP is a policy engine disguised as a routing protocol.

3

Convergence Speed

OSPF converges in seconds. When a link fails, the affected router immediately floods an LSA, and all routers recalculate within 1-5 seconds. OSPF also supports sub-second failover using BFD (Bidirectional Forwarding Detection). BGP converges in minutes. Default eBGP timers mean a peer failure takes 90 seconds to detect (without BFD), and route propagation uses 30-second MRAI timers. This slower convergence is intentional -- it prevents global routing instability.

4

Scalability

OSPF works well for networks up to a few thousand routes. Beyond that, the full link-state database becomes large and SPF calculations become expensive. OSPF areas help, but there are practical limits. BGP was designed from the ground up for massive scale. The global BGP routing table currently holds over 1 million prefixes, and BGP routers handle this without issue because BGP only stores and processes the best path, not the complete topology of every network.

5

Trust Model

OSPF assumes all routers in the area are trusted. Any OSPF router can inject routes, and there is limited ability to filter or verify advertisements within an area. This is fine inside your own network. BGP assumes nothing about trust. Every BGP session is explicitly configured, and operators apply extensive filtering to control which routes they accept and advertise. Route filtering is not optional in BGP -- without it, you could accidentally advertise your neighbor's routes as your own, causing traffic blackholes.

When to Use Each Protocol

🏢

Use OSPF When...

You are routing within a single organization. Enterprise campus networks, data center fabrics, and branch office WAN backbones are all classic OSPF deployments. If you control all the routers and want fast, automatic shortest-path routing, OSPF is the right choice. It is simple to configure and converges quickly.

🌎

Use BGP When...

You need to connect to other organizations -- ISPs, cloud providers, or partner networks. If you have your own AS number and IP address space, you need BGP. Also use iBGP (internal BGP) when you need to carry thousands of external routes through your network without redistributing them into OSPF, which would overwhelm it.

🔗

Use Both When...

You run an ISP or a larger enterprise network, where running both is the common design. OSPF handles internal reachability (how to reach your own routers and subnets), while BGP handles external routes (how to reach the internet and partner networks). The two protocols work together through careful redistribution at border routers, often little more than a default route.

How OSPF and BGP Work Together in Practice

In a typical enterprise network, OSPF runs across all internal routers to distribute routes for internal subnets -- server networks, user VLANs, management networks, and router loopback addresses. OSPF ensures that every internal router knows how to reach every other internal router. This is the foundation that BGP depends on.

At the network edge, one or more border routers run BGP sessions with upstream ISPs. These BGP sessions receive the full internet routing table (or a default route) from the ISP. The border routers then redistribute a default route or selected external routes into OSPF so that internal routers know to send internet-bound traffic toward the border. Alternatively, the border routers run iBGP sessions with other internal routers that need to know about external routes.

The key principle is separation of concerns. OSPF handles the fast, dynamic internal topology. BGP handles the slow, policy-driven external connectivity. If an internal link fails, OSPF reconverges in seconds while BGP does not need to do anything -- the internal path changes, but the external routes remain stable. If an upstream ISP fails, BGP shifts traffic to the backup ISP while OSPF continues operating normally inside. This layered approach provides both speed and stability.

Common Mistakes

Redistributing Everything into OSPF

Injecting the full BGP table (1 million+ routes) into OSPF will overwhelm your internal routers. OSPF was not designed for that scale. Instead, redistribute only a default route or specific summary routes into OSPF, and use iBGP for carrying external routes internally.

Using OSPF Between Different Organizations

OSPF trusts all routers in an area. If you run OSPF with a partner organization, their router misconfiguration can inject bad routes into your network. Always use BGP for inter-organizational connectivity -- it gives you full control over which routes you accept.

Forgetting iBGP Full Mesh

iBGP (internal BGP) requires a full mesh of sessions between all BGP routers in an AS, or the use of route reflectors. Without this, BGP routes learned from one peer will not be forwarded to other iBGP peers, creating routing black holes. This is the most common BGP deployment mistake.

Frequently asked questions about OSPF vs BGP

What is the difference between OSPF and BGP?

OSPF is an Interior Gateway Protocol that runs inside one organization and picks the shortest path by link cost. BGP is an Exterior Gateway Protocol that runs between organizations and picks the best path by policy and AS-path length. OSPF is for inside your network; BGP is for connecting your network to other networks.

Should I use OSPF or BGP for my data center?

For a small or mid-size data center, OSPF is simpler and converges faster. For large modern Clos / spine-leaf fabrics, many operators use BGP everywhere because it scales better, gives explicit per-prefix policy, and avoids OSPF area design constraints.

Can OSPF and BGP run together?

Yes — this is the common design in ISP and larger enterprise networks. OSPF distributes internal routes (router loopbacks, internal subnets). BGP carries external routes. At border routers a default route or specific prefixes are redistributed, but you should not redistribute the full BGP table into OSPF.

Why does BGP converge slowly compared to OSPF?

BGP is deliberately slow. Timers like MRAI (Minimum Route Advertisement Interval) damp out churn so a single flapping link doesn't ripple across the whole internet. OSPF runs inside one trusted network where speed matters; BGP runs between strangers where stability matters more than seconds of failover.

Do I need my own AS number to run BGP?

To run public eBGP with an ISP — yes, you need a public ASN from your regional registry (RIPE, ARIN, APNIC). For private use inside a single org you can use a private ASN (64512–65534, or the 4-byte private range). iBGP and lab BGP between your own devices does not require a public number.

Ready to try BGP and OSPF?

You just saw how OSPF picks the shortest path inside an AS while BGP picks the best path between ASes. Now run a real eBGP session and compare the two protocols hands-on.

Launch the BGP lab →