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.
- 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.
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:
- Customer routes — routes learned from a network paying you for transit are usually preferred above all else (you're being paid to carry that traffic).
- Peer routes — routes learned from a settlement-free peering partner (roughly equal-sized networks that exchange traffic for mutual benefit, no money changing hands) come next.
- Provider routes — routes learned from a network you pay for transit are typically the least preferred, since carrying that traffic yourself, if a cheaper path exists, saves money.
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
- Every ISP and cloud provider — AWS, Google Cloud, and every major ISP each operate their own AS and peer with others; this is the actual mechanism behind "which path does my traffic take to reach a data center."
- Anycast CDNs — a CDN can advertise the exact same IP prefix from data centers on multiple continents; BGP naturally routes each user to whichever advertisement looks closest/cheapest from their AS's perspective, with no extra logic needed.
- Multi-homing — a company connecting to two different ISPs for redundancy is running its own small AS, using BGP to advertise its address space out both providers so traffic can still reach it if one ISP link fails.
- Internet-wide outages — a large-scale, multi-hour "the internet is down" event reported in the news is very often a BGP route leak or misconfiguration at a major network, not a problem with IP, TCP, or any single website.
Gotchas
- A BGP route leak doesn't need malice — a single fat-fingered configuration change at one mid-sized AS has, historically, been enough to make a large chunk of the internet briefly unreachable by accidentally advertising routes it had no business carrying.
- "Shortest AS_PATH" is only a tiebreaker, applied after several policy-based preferences — assuming BGP always finds the shortest real-world path is a common and incorrect mental model.
- Route convergence delay is a structural property of path-vector routing at internet scale — no amount of hardware upgrade at one AS fixes it, since it's bounded by how fast the whole chain of neighboring ASes propagates the change.
- RPKI adoption is still partial across the internet — a route hijack against a prefix whose owner hasn't published RPKI records can still succeed even on a modern, well-run network elsewhere.