Home/Learn/Computer Networks/Routing — OSPF, BGP & Static Routes

Routing — OSPF, BGP & Static Routes

Intermediate
Network Layer

Routing determines the path packets take across a network. Static routes are manually configured; OSPF dynamically finds the shortest path within one organisation; BGP routes traffic between autonomous systems across the internet.

Overview

Routing is the process of selecting the best path for a packet to travel from source to destination across multiple networks. Routers maintain a routing table — a list of network prefixes and the next-hop router to send packets toward each destination. Routes can be static (manually configured, never change) or dynamic (learned via routing protocols). OSPF (Open Shortest Path First) is a link-state interior gateway protocol — each router broadcasts the state of its links and all routers independently compute the shortest-path tree using Dijkstra's algorithm. BGP (Border Gateway Protocol) is the exterior gateway protocol that glues the internet together — it routes between Autonomous Systems (AS) operated by different organisations (ISPs, cloud providers, enterprises). BGP is a path-vector protocol that considers policies and AS-path length, not just bandwidth. For backend engineers, understanding routing matters for cloud multi-region architectures, BGP failover, and VPC routing table configuration.

Routing Tables and Static Routes

Every router and host maintains a routing table. When a packet arrives, the router does a longest-prefix match against the table to find the best next hop. Static routes are simple and predictable but require manual update when topology changes.

Routing table, longest-prefix match, AWS VPC routes
// Routing table on a Linux host (ip route show):
// default via 10.0.0.1 dev eth0           — default gateway for all traffic
// 10.0.0.0/24 dev eth0 proto kernel       — directly connected subnet
// 10.0.1.0/24 via 10.0.0.254 dev eth0    — static route to another subnet
// 172.16.0.0/12 via 10.0.0.2 dev eth0    — route to corporate network via VPN

// Longest-prefix match — most specific wins:
// Destination: 10.0.1.50
// Matches: 10.0.0.0/8  (prefix /8)
// Matches: 10.0.1.0/24 (prefix /24) ← more specific, used
// Matches: default 0.0.0.0/0 (prefix /0)

// AWS VPC Route Table example:
// ┌───────────────┬───────────────────────────────────────────┐
// │ Destination   │ Target                                    │
// ├───────────────┼───────────────────────────────────────────┤
// │ 10.0.0.0/16   │ local (within VPC)                        │
// │ 0.0.0.0/0     │ igw-xxx (Internet Gateway — public subnet)│
// │ 0.0.0.0/0     │ nat-xxx (NAT Gateway — private subnet)    │
// │ 10.1.0.0/16   │ pcx-xxx (VPC Peering to another VPC)      │
// └───────────────┴───────────────────────────────────────────┘

OSPF vs BGP

OSPF finds shortest paths within one network (intra-AS). BGP connects different networks across the internet (inter-AS). They solve different problems at different scales.

OSPF link-state vs BGP path-vector and cloud usage
// OSPF (Open Shortest Path First) — intra-AS, link-state:
// 1. Each router discovers neighbours (Hello packets)
// 2. Each router floods Link State Advertisements (LSAs) to all others
// 3. Every router builds identical topology map (LSDB)
// 4. Each router runs Dijkstra to compute shortest-path tree
// 5. Routes installed in routing table
//
// OSPF is fast to converge (~1-5 seconds on failure)
// Used inside enterprise networks and cloud provider backbones
//
// BGP (Border Gateway Protocol) — inter-AS, path-vector:
// 1. BGP peers establish TCP session on port 179
// 2. Exchange full routing table initially, then incremental updates
// 3. Each route carries the AS-PATH attribute (list of ASes traversed)
// 4. BGP selects best path based on: LOCAL_PREF → AS_PATH length → MED → IGP cost
//
// The internet is ~70,000 Autonomous Systems connected by BGP
// Your ISP runs BGP to advertise your IP block to the internet
//
// AWS uses BGP for:
// — Direct Connect (private leased line to AWS)
// — Site-to-Site VPN with dynamic routing
// — Transit Gateway cross-region routing
//
// BGP hijacking — security risk:
// A malicious AS advertises someone else's IP prefix → traffic misdirected
// Mitigation: RPKI (Route Origin Validation) — cryptographic prefix ownership

Key Points to Remember

  • 1Routing table lookup uses longest-prefix match — most specific route wins.
  • 2Static routes: manually configured, predictable, no overhead, breaks on topology change.
  • 3OSPF: link-state, intra-AS, uses Dijkstra, fast convergence (~seconds).
  • 4BGP: path-vector, inter-AS, governs internet routing between ~70,000 Autonomous Systems.
  • 5BGP path selection: LOCAL_PREF → AS_PATH length → MED → IGP cost.
  • 6AWS VPC route tables are static routes; Direct Connect and VPN use BGP for dynamic routing.

Interview Questions

Sign in to ask Aria
1

What is the difference between static routing and dynamic routing?

EasyWipro
2

What is the difference between OSPF and BGP?

MediumIBM
3

What is longest-prefix match and why does it matter in routing?

MediumAmazon
4

How does BGP route traffic across the internet between different organisations?

HardCloudflare
5

What is BGP hijacking and how is it mitigated?

HardThoughtWorks

Ask Aria about Routing — OSPF, BGP & Static Routes

Your personal AI tutor — ask anything about this concept

Revision Status

Personal Notes

Sign in to save personal notes for this topic.

Discussion

Sign in to join the discussion.

Loading discussion…