OSPF vs BGP: IGP vs EGP, Route Selection and Scale

The internet is made up of interconnected ASes (autonomous systems), networks managed by organizations such as service providers, universities and companies. A protocol that exchanges routes inside an AS is called an IGP, and one that exchanges routes between ASes is called an EGP; OSPF is the typical IGP and BGP the typical EGP. OSPF automatically finds the shortest route from the link costs inside an AS and aims to switch quickly after a failure. BGP chooses routes between ASes based on attributes such as the sequence of ASes passed through (AS_PATH) and on each organization's policy, such as which neighbor to prefer. Because it handles routes for the entire internet, it is designed for stability and control rather than speed.

AspectOSPFBGP
Classification and scopeIGP; inside one AS (organization)EGP; between ASes; also used between BGP routers inside the same AS (iBGP)
Protocol typeLink-statePath vector
Route selectionFinds the route with the lowest total link cost using Dijkstra's algorithmCompares attributes such as LOCAL_PREF, AS_PATH length and MED in a fixed order; attributes can be set to apply the organization's policy
Loop preventionAll routers in an area compute shortest paths from the same mapDiscards a received route whose AS_PATH contains its own AS number
How neighbors connectRuns directly over IP (protocol number 89); sends Hellos by multicast and discovers neighbors automaticallyConnects over TCP port 179; each peer is configured by the administrator one by one
How updates are sentWhen something changes, LSAs are spread through the area; Hellos check that neighbors are alive (every 10 seconds in the RFC 2328 example)Sends the full set of routes first, then only changes in UPDATE messages; routes are not resent periodically, and KEEPALIVEs keep the session up
Convergence speedGenerally fast; recomputes right away once it notices a link failureGenerally slower; detecting a failure can mean waiting for the hold time (90 seconds suggested in RFC 4271), and there are many more routes
Number of routes and scaleRoutes inside the organization; large networks are split into areas to limit computation and informationCan handle the very large number of routes of the whole internet; used between providers and by organizations connected to several providers

Figures are typical orders of magnitude and vary by process generation and product.

When to use which

OSPF
Inside one organization's network, when the shortest route should be chosen automatically among redundant paths and switched quickly after a failure. It suits routing within sites and data centers.
BGP
When connecting to several providers (multihoming) and controlling which link to prefer, or when exchanging routes with other ASes as a provider. It gives fine control, by policy, over which routes are announced outside the organization and which are accepted.

FAQ

Why is OSPF used inside an organization and BGP between organizations?

Because the goals differ. Inside an organization, the whole network is run by the same administrators, so what matters is choosing the best route automatically and switching quickly after a failure. Between organizations, each neighbor has its own policies and does not reveal its internal layout. Which neighbor's routes to prefer and which routes to announce must be decided by contracts and policies, and BGP is built to express this with attributes and filters.

What is the difference between iBGP and eBGP?

eBGP is a BGP session between routers in different ASes; iBGP is one between routers in the same AS. When announcing a route to another AS over eBGP, a router adds its own AS number to the AS_PATH. Over iBGP, within the same AS, the AS number is not added, so to prevent loops a route learned over iBGP is not passed on to other iBGP peers. iBGP routers are therefore either all connected to each other (full mesh) or use route reflectors.

Are OSPF and BGP ever used together?

Yes. In providers and large organizations, a common design uses an IGP such as OSPF to find routes to the addresses of devices inside the AS, runs BGP sessions on top of that, and carries external routes in BGP. The IGP tells routers how to reach the next hop (NEXT_HOP) of BGP routes. Some large data centers instead exchange routes with BGP alone, without an IGP (RFC 7938).

Is there also a protocol called EGP?

Yes. EGP (Exterior Gateway Protocol) is also the name of an old protocol used between ASes before BGP. It is no longer used, and "EGP" now refers to the class of protocols that exchange routes between ASes, of which BGP is the typical example.

Check your understanding

On the internet, which protocol exchanges routing information between the networks run by organizations such as ISPs (ASes, autonomous systems) and chooses routes based on attributes such as the sequence of ASes a route has passed through (AS_PATH) and on each organization's policy?

  1. AOSPF
  2. BBGP
  3. CRIP
Show answer and explanation

Answer: B. BGP

BGP (Border Gateway Protocol) is the protocol that organizations identified by AS numbers use to exchange routes, and routes on the internet are basically exchanged with BGP. Routes carry attributes such as the sequence of ASes they have passed through (AS_PATH) and the next hop (NEXT_HOP), and a router prevents loops by discarding routes that contain its own AS number. Route selection is not just a matter of the shortest AS_PATH; an organization's policies, such as which neighbor to prefer, can be expressed in attributes and applied. OSPF and RIP are routing protocols used inside a single organization (IGPs).

In OSPF, router R1 has three paths to a network beyond R4. Path A uses a 10 Gbps link R1–R5 and a 1 Gbps link R5–R4; path B is a direct 100 Mbps link R1–R4; and path C uses 1 Gbps links R1–R2, R2–R3 and R3–R4. The cost of each link is "reference bandwidth 10 Gbps ÷ link bandwidth", and the cost beyond R4 is the same for all paths. Which path does R1 choose?

  1. APath A
  2. BPath B
  3. CPath C
Show answer and explanation

Answer: A. Path A

The costs are 10 ÷ 10 = 1 for a 10 Gbps link, 10 ÷ 1 = 10 for 1 Gbps and 10 ÷ 0.1 = 100 for 100 Mbps. Path A costs 1 + 10 = 11, path B 100 and path C 10 × 3 = 30, so path A, with the lowest total cost, is chosen. If the reference bandwidth is left at 100 Mbps, long the default on much equipment, every link of 100 Mbps or faster costs 1, giving path A 2, path B 1 and path C 3, and the slowest path, B, is chosen. Networks with fast links therefore set the reference bandwidth to a larger value, the same on every router.

Which statement correctly describes a link-state routing protocol?

  1. AEach router builds its routing table only from the destinations and distances (such as hop counts) it receives from neighboring routers
  2. BEach router floods the state of its own links so that all routers share the topology of the whole network, and each router computes shortest paths with Dijkstra's algorithm
  3. CAn administrator configures routes on each router by hand, and routers exchange no routing information
Show answer and explanation

Answer: B. Each router floods the state of its own links so that all routers share the topology of the whole network, and each router computes shortest paths with Dijkstra's algorithm

In a link-state protocol (such as OSPF or IS-IS), each router advertises the state of its links (its neighbors and the costs to them), and all routers in the same area hold the same map. From this map, each router computes a shortest-path tree rooted at itself with Dijkstra's algorithm. A distance-vector protocol (such as RIP) chooses routes by adding its own distance to the "distance to each destination" heard from its neighbors; this is simple, but routers do not know the topology of the whole network and can take time to settle after a failure. Link-state protocols need more computation and memory but react to changes quickly.

  1. Which correctly describes dynamic routing?

Practice Network Design and Operations questions

Key terms explained

OSPF (Open Shortest Path First)
A link-state routing protocol used inside an organization. Each router floods the state of its own links so that all routers share the network topology, and computes the path with the lowest total cost using Dijkstra's algorithm. Costs are generally derived from link bandwidth, and splitting the network into several areas lets it scale to large networks.
BGP (Border Gateway Protocol)
The routing protocol that exchanges routing information between ASes (autonomous systems, the units of network run by organizations such as ISPs) on the internet. It chooses routes based on attributes attached to them, such as the sequence of ASes they have passed through (AS_PATH), and on each organization's policy. Routers connect to each other over TCP port 179 to exchange information.
IP routing
Deciding which path packets take to reach their destination on an IP network. Each router holds a routing table, picks the route that matches the destination IP address with the longest prefix (longest-prefix match) and passes the packet to the next router. The table is built by static routing, configured by hand, or by dynamic routing, built automatically with routing protocols such as OSPF, RIP and BGP.
Router
A network-layer (layer 3) device that forwards packets between different networks. It compares the destination IP address of each packet with its routing table, picks the next router (next hop) and the outgoing interface, and sends the packet on. Home routers also bundle NAPT, a DHCP server and a firewall.
Dijkstra's algorithm
An algorithm that finds the shortest paths from one starting vertex to every vertex in a graph whose edge weights are 0 or greater. It repeatedly finalizes the nearest vertex whose distance is not yet final and updates the distances of its neighbors. It does not work correctly with negative edge weights.
TCP (Transmission Control Protocol)
A connection-oriented transport-layer protocol. It sets up a connection with a three-way handshake and uses sequence numbers, acknowledgments (ACKs) and retransmission to deliver data in order, without gaps or duplicates. Flow control and congestion control adjust how much it sends. It carries web traffic over HTTP/1.1 and HTTP/2, email, SSH and more.

Related comparisons