Topic Overview

Routing Protocols (OSPF, BGP)

Learn routing protocols: OSPF (link-state) and BGP (path-vector) for internet routing. Understand how routers exchange routing information and build routing tab

Medium13 min read

Routing Protocols (OSPF, BGP)

Why This Matters

Think of routing protocols like GPS navigation systems. When you want to go somewhere, GPS calculates the best route based on current traffic and road conditions. Routing protocols do the same for networks—routers exchange information about network topology and calculate the best paths for data. OSPF is like city navigation (within a network), BGP is like highway navigation (between networks).

This matters because routers need to know how to forward packets. Without routing protocols, routers would have to be manually configured with routes, which doesn't scale. Routing protocols automatically discover routes, adapt to network changes, and find the best paths. Understanding this helps you configure networks and troubleshoot routing issues.

In interviews, when someone asks "How do routers know where to send packets?", they're testing whether you understand routing protocols. Do you know how OSPF works? Do you understand BGP? Most engineers don't. They just configure routers and assume routes work.

What Engineers Usually Get Wrong

Most engineers think "routing protocols just find paths." But routing protocols do more—they exchange topology information, calculate shortest paths, detect failures, and adapt to changes. OSPF uses link-state routing (each router knows the full topology), while BGP uses path-vector routing (routers exchange paths). Understanding this helps you understand how routing works.

Engineers also don't understand that routing protocols prevent loops. If routers just forwarded packets without coordination, packets could loop forever. Routing protocols use algorithms (like Dijkstra's for OSPF) to ensure loop-free paths. Understanding this helps you understand why routing protocols are necessary.

How This Breaks Systems in the Real World

A service was using static routes (manually configured). When a network link failed, routers didn't know about it. Packets were still sent to the failed link, causing packet loss. The service became unreliable. The fix? Use routing protocols (OSPF, BGP). They automatically detect failures and reroute traffic. This makes networks more resilient.

Another story: A service was using OSPF but didn't configure it properly. Routers were advertising incorrect routes, causing routing loops. Packets bounced between routers, never reaching their destination. The service was unreachable. The fix? Configure routing protocols correctly. Verify route advertisements. Use route filters to prevent incorrect routes.


What are Routing Protocols?

Routing protocols allow routers to:

  • Exchange routing information: Share network topology
  • Build routing tables: Determine best paths
  • Adapt to changes: Update when network changes
  • Avoid loops: Prevent routing loops

Types:

  • Interior Gateway Protocol (IGP): Within autonomous system (OSPF, RIP)
  • Exterior Gateway Protocol (EGP): Between autonomous systems (BGP)

OSPF (Open Shortest Path First)

Type: Link-state routing protocol (IGP) Algorithm: Dijkstra's shortest path Use: Within autonomous system (AS)

How OSPF Works

1. Discover Neighbors

Router sends Hello packets
Neighbors respond
Establish adjacency

2. Exchange Link-State Advertisements (LSAs)

Each router describes its links
Flood LSAs to all routers
All routers have same topology database

3. Build Shortest Path Tree

Each router runs Dijkstra's algorithm
Calculates shortest path to all destinations
Builds routing table

OSPF Areas

Hierarchical design:

Area 0 (Backbone)
  ├─ Area 1
  ├─ Area 2
  └─ Area 3

Benefits:

  • Reduced LSA flooding: LSAs only within area
  • Scalability: Large networks divided into areas
  • Stability: Changes isolated to area

OSPF Metrics

Cost based on bandwidth:

Cost = Reference Bandwidth / Interface Bandwidth

Fast link: Low cost
Slow link: High cost

BGP (Border Gateway Protocol)

Type: Path-vector routing protocol (EGP) Algorithm: Best path selection Use: Between autonomous systems (internet routing)

How BGP Works

1. Establish BGP Sessions

Routers establish TCP connections
Exchange BGP messages

2. Exchange Routes

Routers advertise routes with attributes
  - AS Path (list of ASes)
  - Next Hop
  - Origin
  - Local Preference

3. Best Path Selection

BGP selects best path based on attributes:
  1. Local Preference (highest)
  2. AS Path (shortest)
  3. Origin
  4. MED (Multi-Exit Discriminator)

BGP Attributes

AS Path:

Route: 192.168.1.0/24
AS Path: [AS100, AS200, AS300]

Shows path through autonomous systems

Local Preference:

Higher value = preferred
Used within AS

MED:

Lower value = preferred
Used between ASes

OSPF vs BGP

FeatureOSPFBGP
TypeLink-state (IGP)Path-vector (EGP)
ScopeWithin ASBetween ASes
AlgorithmDijkstraBest path selection
ConvergenceFastSlower
ScalabilityLimited (areas)Very scalable
Use caseEnterprise networksInternet routing

Examples

OSPF Configuration

# Router configuration
router ospf 1
  network 192.168.1.0 0.0.0.255 area 0
  network 10.0.0.0 0.0.0.255 area 1
  
interface GigabitEthernet0/0
  ip ospf cost 10
  ip ospf priority 100

BGP Configuration

# Router configuration
router bgp 100
  neighbor 192.168.1.1 remote-as 200
  neighbor 192.168.1.1 update-source Loopback0
  
  network 10.0.0.0 mask 255.0.0.0
  redistribute ospf 1

OSPF Dijkstra Algorithm

1class OSPFRouter {
2 private routerId: string;
3 private lsdb: Map<string, Map<string, number>>; // Link State Database
4 private routingTable: Map<string, string> = new Map();
5
6 constructor(routerId: string) {
7 this.routerId = routerId;
8 this.lsdb = new Map();
9 }
10
11 runDijkstra(): void

BGP Best Path Selection

1interface BGPRoute {
2 localPreference: number;
3 asPath: string[];
4 origin: 'IGP' | 'EGP' | 'INCOMPLETE';
5 med: number;
6}
7
8class BGPRouter {
9 selectBestPath(routes: BGPRoute[]): BGPRoute | null {
10 // Select best BGP path
11 if (routes.length === 0) {
12 return null;
13 }
14
15 // Sort by BGP attributes
16 routes.sort(a b

Common Pitfalls

  • OSPF area design: Poor area design causes issues. Fix: Design areas carefully, use area 0 as backbone
  • BGP route filtering: Not filtering routes causes problems. Fix: Filter routes, use route maps
  • Convergence time: Slow convergence. Fix: Tune timers, optimize network design
  • Route loops: Incorrect configuration causes loops. Fix: Verify configuration, use loop prevention

Interview Questions

Beginner

Q: What is the difference between OSPF and BGP? When would you use each?

A:

OSPF (Open Shortest Path First):

  • Type: Link-state routing protocol (IGP)
  • Scope: Within autonomous system (AS)
  • Algorithm: Dijkstra's shortest path
  • Use: Enterprise networks, data centers

BGP (Border Gateway Protocol):

  • Type: Path-vector routing protocol (EGP)
  • Scope: Between autonomous systems
  • Algorithm: Best path selection
  • Use: Internet routing, ISPs

Key Differences:

FeatureOSPFBGP
ScopeWithin ASBetween ASes
ConvergenceFastSlower
ScalabilityLimitedVery scalable
ComplexityModerateHigh

When to use:

  • OSPF: Enterprise networks, data centers, within organization
  • BGP: Internet routing, ISPs, between organizations

Intermediate

Q: Explain how OSPF builds routing tables using link-state advertisements.

A:

OSPF Process:

  1. Discover Neighbors

    Router sends Hello packets
    Neighbors respond
    Establish adjacency
    
  2. Exchange Link-State Advertisements (LSAs)

    Each router describes its links:
      - Connected networks
      - Link costs
      - Neighbor routers
    
    Flood LSAs to all routers in area
    All routers have same topology database
    
  3. Build Shortest Path Tree

    Each router runs Dijkstra's algorithm:
      - Start from itself
      - Calculate shortest path to all destinations
      - Build routing table
    

Example:

Topology:
  Router A --10-- Router B --5-- Router C
       \                    /
        \                  /
         \--20-- Router D--/

Router A's LSDB:
  A: [(B, 10), (D, 20)]
  B: [(A, 10), (C, 5)]
  C: [(B, 5), (D, 15)]
  D: [(A, 20), (C, 15)]

Dijkstra from A:
  A → B: 10
  A → C: 15 (via B)
  A → D: 20

Routing Table:
  C: Next hop B, Cost 15
  D: Next hop D, Cost 20

Benefits:

  • Fast convergence
  • Loop-free
  • Scalable with areas

Senior

Q: Design a routing system for a large ISP that uses both OSPF internally and BGP for internet routing. How do you handle route selection, load balancing, and failover?

A:

1class ISPRoutingSystem {
2 private ospf: OSPFRouter;
3 private bgp: BGPRouter;
4 private routeManager: RouteManager;
5
6 constructor() {
7 this.ospf = new OSPFRouter();
8 this.bgp = new BGPRouter();
9 this.routeManager = new RouteManager();
10 }
11
12 // 1. OSPF for Internal Routing
13 class OSPFRouter {
14 async buildTopology(): Promise<void

Features:

  1. OSPF: Internal routing, fast convergence
  2. BGP: External routing, internet connectivity
  3. Route selection: Prefer OSPF for internal, BGP for external
  4. Load balancing: ECMP for multiple paths
  5. Failover: Automatic route updates on failure

  • IP Addressing (IPv4/IPv6) - Routing protocols use IP addresses to determine paths, understanding IP addressing is essential for routing

  • Subnetting & CIDR - Routing protocols use subnet information to make routing decisions, understanding subnetting helps configure routing

  • OSI Model (7 Layers) - Routing protocols operate at Layer 3 (Network), understanding the OSI model provides context

  • Load Balancers (L4 vs L7) - Load balancers can use routing protocols, understanding routing helps configure load balancing

  • Anycast, Unicast & Multicast - Routing protocols determine how traffic is routed, understanding routing types explains protocol behavior

  • OSPF: Link-state IGP, uses Dijkstra, fast convergence, within AS

  • BGP: Path-vector EGP, best path selection, internet routing, between ASes

  • OSPF areas: Hierarchical design for scalability

  • BGP attributes: AS Path, Local Preference, MED for path selection

  • Route selection: OSPF for internal, BGP for external

  • Convergence: OSPF fast, BGP slower but more stable

  • Best practices: Design OSPF areas carefully, filter BGP routes, monitor routing tables

Key Takeaways

OSPF: Link-state IGP, uses Dijkstra, fast convergence, within AS

BGP: Path-vector EGP, best path selection, internet routing, between ASes

OSPF areas: Hierarchical design for scalability

BGP attributes: AS Path, Local Preference, MED for path selection

Route selection: OSPF for internal, BGP for external

Convergence: OSPF fast, BGP slower but more stable

Best practices: Design OSPF areas carefully, filter BGP routes, monitor routing tables


About the author

InterviewCrafted helps you master system design with patience. We believe in curiosity-led engineering, reflective writing, and designing systems that make future changes feel calm.