Networking

BGP — Border Gateway Protocol

IP's own page describes routing as "a packet is handed to a gateway/router, which forwards it based on its routing table" — but never says how that table gets built in the first place, at the scale of the entire internet. BGP is the answer: the protocol that lets thousands of independently-run networks tell each other what they can reach, so a routing table anywhere on the internet can have an answer for almost any destination on it.

Quick reference
What it is
The protocol that glues ~70,000+ independently-operated networks (Autonomous Systems) into one routable internet by exchanging reachability information.
Mental model
Word-of-mouth route gossip between ISPs: "I can reach these address ranges, and here's the chain of networks the route passes through."
Key numbers
Runs over TCP port 179; each Autonomous System has a 16- or 32-bit AS Number (ASN).
Reach for it when
Never directly — an embedded device or application never speaks BGP. Understanding it explains why the internet has no single central router, and what "the internet is down for everyone" outages usually actually are.

Autonomous Systems

An Autonomous System (AS) is a network under one organization's independent administrative control — an ISP, a university, a large company like a CDN or cloud provider. Each one has a unique AS Number (ASN) and owns one or more blocks of IP address space it's responsible for. Nobody owns or centrally controls "the internet" — it's simply the sum of every AS that has agreed, bilaterally, to exchange traffic with its neighbors.

How route advertisement actually works

Routers at the edge of each AS form a BGP session (a long-lived TCP connection) with routers in neighboring ASes. Over that session, they exchange advertisements: "I can reach this IP prefix, and here is the path of ASes a packet would cross to get there." Each AS that hears an advertisement can choose to re-advertise it onward to its own neighbors, adding itself to the path — which is how a route to one small network can eventually become known almost everywhere.

AS 400 advertises a route to its own prefix; AS 300 and AS 200 each re-advertise it onward, prepending their own AS number to the path each time AS 100 AS 200 AS 300 AS 400 owns 8.8.8.0/24 originates "…/24 via [400]" re-advertises "…/24 via [300,400]" re-advertises "…/24 via [200,300,400]" AS 100 now knows: to reach 8.8.8.0/24, the path crosses AS 200, AS 300, then AS 400
Every hop prepends its own ASN to the path before passing the route along — the growing list is the AS_PATH.

Path-vector routing and AS_PATH

Protocols used inside a single network (like OSPF) are typically link-state — every router builds a full map of the network's topology. That doesn't scale to the entire internet, where no single AS can or should see every other AS's internal topology. BGP instead is a path-vector protocol: each route carries only the ordered list of ASes it passed through (the AS_PATH), not the full topology.

That list does double duty. First, it's a built-in loop guard — if a router ever sees its own ASN already present in an incoming route's AS_PATH, it rejects the route outright, since accepting it would mean routing through itself. Second, AS_PATH length is the simplest input into route selection — all else being equal, a shorter path is usually preferred, the same instinct as a shortest-path algorithm, just measured in AS hops rather than physical distance.

Policy beats shortest path

This is the part that surprises people coming from simpler routing protocols: BGP does not always pick the shortest available path. Each AS applies its own routing policy on top of what it hears, shaped by real business relationships:

A longer path through a paying customer can and often does win over a shorter path through an expensive upstream provider — because BGP optimizes for each AS's own business interests, not for "the internet's" overall efficiency. There is no global authority making that trade-off; it emerges from thousands of independent, self-interested routing policies.

Convergence, flapping, and hijacks

When a link or router fails, every AS that had a route through it has to hear a withdrawal message and recompute an alternative — hop by hop, the same way the original advertisement propagated. This takes real time, typically seconds to a couple of minutes at internet scale, not the near-instant failover a local network might achieve — a structural limit of path-vector routing at this scale, not a bug in any one implementation.

Because any AS can, in principle, advertise a route to any IP prefix — including one it doesn't actually own — a misconfiguration (or, rarely, a deliberate attack) can cause a route hijack: traffic meant for a legitimate network gets pulled toward the wrong AS instead, because that AS advertised a more specific or more attractive-looking route. BGP had no built-in way to verify "does this AS actually own this prefix" for most of its history; RPKI (Resource Public Key Infrastructure) is the modern, increasingly-deployed fix, letting an AS cryptographically prove it's authorized to originate a given prefix.

Real-world applications

Gotchas

Field notes