Internet Routing: Autonomous Systems and BGP
The internet is tens of thousands of independently run networks that tell each other which prefixes they can reach and by what path, and choose among those paths by commercial policy rather than by distance — which is why your packets take the route they take and why the return path is usually different.
Builds on Subnetting in the Networking roadmap.
The problem
Hop by hop across organisations
Inside one organisation, routing is a solved engineering problem: one administrator, one policy, one interior protocol (OSPF or IS-IS) computing shortest paths over a graph everybody agrees on. The internet is not one organisation. It is a federation of autonomous systems (ASes): networks under a single administrative control, each identified by a number. Your ISP is one; Google is AS15169; Cloudflare is AS13335; a university, a hosting company, a large bank each have one. There are around 75,000 active ASes.
Each AS runs whatever it likes internally. Between ASes, every one of them speaks one protocol — BGP, the Border Gateway Protocol — to say "I can reach these prefixes, and here is the list of ASes a packet would traverse to get there." Your packet’s journey is a sequence of AS-internal shortest-path hops stitched together by BGP decisions at each border.
- Home routerdefault route to the ISP↓
- ISP access and core (AS 64500)interior routing to the border router that BGP chose↓
- Transit carrier or peering exchangea paid upstream, or a settlement-free peer at an IXP↓
- Destination AS border (AS 64511)announced the destination prefix via BGP↓
- Destination data centreinterior routing, then a rack, then a server
BGP in one screen
BGP is a path-vector protocol. An announcement is a prefix plus the ordered list of ASes it has passed through (the AS path). When a router forwards an announcement to a neighbour AS it prepends its own number. Loops are detected trivially: if you see your own AS number in a path, discard it. That is the whole mechanism for avoiding the count-to-infinity problem of pure distance-vector routing.
The important thing is what BGP does *not* do: it does not pick the shortest path. Each AS applies policy first. An operator sets a local preference on routes learned from customers (who pay them) above routes from peers (free) above routes from transit providers (whom they pay). Only among routes of equal preference does AS-path length matter, and after that a chain of tiebreakers. The result is that your packet frequently takes a longer path because it is cheaper for the AS deciding, and that is by design, not a bug.
BGP also does not carry any notion of capacity or latency. A path through three ASes with congested links looks identical to one through three ASes with empty ones. Latency-aware traffic steering is done above BGP — by CDNs measuring from clients, by anycast, by DNS — see CDNs: The Networking View.
- Path-vector: prefix + AS path; loop detection by seeing yourself in the path.
- Decision: local preference (policy) → shortest AS path → several tiebreakers.
- No bandwidth, no latency, no load in the protocol.
Prefix AS path Learned from LocalPref 203.0.113.0/24 3356 13335 transit 100 203.0.113.0/24 6939 13335 peer 200 <- chosen: policy beats path length * best: via peer AS6939 (settlement-free) even though both paths are two ASes long
Peering, transit, IXPs — and why paths are asymmetric
ASes connect in two commercial shapes. Transit is paid: a smaller network pays a larger one to carry its traffic to and from the whole internet. Peering is settlement-free: two networks with roughly balanced traffic connect directly and exchange only each other’s (and their customers’) routes, saving both of them transit fees. Much peering happens at Internet Exchange Points (IXPs) — a shared switch fabric in a data centre where hundreds of networks plug in and peer with each other over one port. DE-CIX in Frankfurt and AMS-IX in Amsterdam each carry many terabits per second at peak.
These economics produce hot-potato routing: an AS wants to hand traffic to the next AS as early as possible, because carrying it across its own backbone costs money. So the outbound path from A to B leaves A’s network at the exit nearest the source, while the return path leaves B’s network at the exit nearest *its* source. The forward and return paths therefore go through different cities and different links. traceroute shows you only the forward path; the return is invisible from your end and can be the slow one — traceroute: Discovering the Path Hop by Hop and the asymmetric-latency-cross-region challenge deal with exactly this.
Anycast is the one trick you should know at this level: announce the same prefix from many locations, and BGP’s per-router choice sends each client to a nearby instance. The DNS root servers, 1.1.1.1, 8.8.8.8 and CDN edges all work this way. It is routing, not DNS; see CDNs: The Networking View.
Failure modes: leaks, hijacks, withdrawals
BGP was designed for a network of mutually trusting operators and has no built-in authentication of *who may announce what*. A route leak is an AS re-announcing routes it learned from one provider to another in violation of policy — typically a misconfigured customer announcing its transit provider’s full table to a second provider, which now sees a "customer" route and prefers it. The customer’s modest links then receive a continent’s traffic and collapse. A hijack is an AS announcing a prefix it does not own, or a more-specific of someone else’s prefix; by longest-prefix match (The Routing Table and Longest-Prefix Match) that more-specific wins everywhere it is heard. In 2008 a Pakistani ISP’s attempt to block YouTube domestically by announcing a more-specific of YouTube’s prefix leaked to the world and took the site offline for most of the internet for about two hours.
Withdrawals fail too. In October 2021 a configuration change inside Facebook withdrew the BGP announcements for the prefixes hosting its authoritative DNS servers. Within minutes the names facebook.com, instagram.com and whatsapp.com stopped resolving anywhere, because the resolvers could no longer reach the servers that would have answered — a routing failure that presented as a DNS failure, for roughly six hours. The lesson generalises: from the outside, most routing failures look like something else.
The mitigation being deployed is RPKI: cryptographic records of which AS is authorised to originate which prefix, which routers can validate against before accepting an announcement. Coverage is substantial but not universal, so hijacks still happen. From an application engineer’s point of view, the symptoms are the tell: a service reachable from some networks and not others, latency that jumped for one region only, or a traceroute that shows the wrong country.
- Leak: announcing routes to the wrong neighbour; symptom: a small network suddenly carrying huge traffic, and paths going through it.
- Hijack: announcing a prefix you do not own; more-specifics win globally.
- Withdrawal: pulling your own routes; the internet forgets you exist within minutes.
- Convergence: minutes of flapping paths and partial reachability after any of the above.
What this means for software you write
You cannot choose the path and you cannot see the whole of it, so design for its properties instead. Latency between two regions is not symmetric and not constant; measure round trips in both directions and over time before promising anything. "Reachable" is per source network, so an outage report from one customer and a healthy dashboard can both be true. Multi-region deployments and anycast front doors exist largely because the middle of the internet is outside your control.
When debugging, traceroute/mtr from the affected client and from the server *toward the client* show both halves of the path; a looking glass (public BGP route servers many networks expose) shows what the rest of the world is being told about your prefix. Knowing that a problem is "in the middle" ends the investigation of your own servers quickly, which is most of the value.
Key points
- The internet is ~75,000 autonomous systems, each routing internally as it likes and exchanging reachability with BGP at the borders.
- BGP is path-vector: prefix + AS path, loops rejected by seeing your own AS number.
- Route choice is policy first (customer > peer > transit), path length second; there is no latency or capacity in the protocol.
- Transit is paid, peering is free, IXPs are where peering happens; hot-potato routing makes forward and return paths differ.
- Anycast is the same prefix announced from many places; BGP sends each client to a nearby one.
- Leaks, hijacks and withdrawals are the failure modes; they usually present as something else (DNS, "slow for one region", "unreachable from one ISP").
Why does this exist?
Mechanisms are answers to constraints. Open each question before reading the answer.
▸Why not just run one global shortest-path protocol?
Because the networks are owned by different companies with different costs and contracts, and "shortest" would force an operator to carry traffic across its expensive backbone for free. BGP lets each operator encode its own economics; the price is that nobody optimises the global path.
▸Why are forward and return paths different?
Each AS independently picks the cheapest exit for the traffic it is holding, which is usually the nearest one to where the traffic entered. Applied in both directions, that yields two different paths.
▸Why can one misconfiguration take a site off the internet?
Because BGP trusts announcements, and longest-prefix match makes a more-specific announcement win everywhere. A wrong announcement is a global instruction; the protocol has no notion of who is allowed to give it.
BGP and the AS path
How it fails
What the failure looks like from inside real software.Reference · showHide
- A hijacked or leaked prefix: users on some ISPs reach your site, others get timeouts or someone else’s server; your own monitoring, on your own network, is green.
- Withdrawn routes after a bad config push: the site vanishes globally within minutes and cannot be fixed remotely because the tools used to fix it are behind the withdrawn routes.
- Asymmetric path with one congested direction: uploads fine, downloads slow, or vice versa;
traceroutefrom the client looks clean because it shows only the forward path. - Regional latency jump with no deployment: a peering link was withdrawn and traffic now detours through another country; nothing in your logs changed except p99.
- Anycast instance failing without withdrawing its route: clients nearest that instance black-hole while everyone else is fine.
Check yourself
Answer out loud or on paper first, then compare. These come from the interview questions linked to this lesson.
How does a CDN make a site faster at the network level? How does a client end up at a nearby edge, and what can a CDN do for content that cannot be cached?
Show a strong answerHide the answer
The physics first: a user 150 ms from the origin pays that RTT for the TCP handshake, once or twice more for TLS, and then again for every slow-start round of the response — so a 100 kB asset costs 4–6 round trips, close to a second, before bandwidth even matters (Bandwidth vs Latency). An edge 10 ms away shrinks each of those round trips by 15×. A CDN is a fleet of points of presence that terminate the user’s connection nearby and serve from cache when they can. See CDNs: The Networking View.
Getting the user to a nearby edge is done one of two ways. DNS steering: the CDN’s authoritative servers answer
assets.example.comwith an edge IP chosen by the *resolver’s* location (improved by EDNS Client Subnet, which passes part of the client’s address), using short TTLs so decisions can change. It is precise and controllable but only as good as the resolver’s location — a user on a distant public resolver without ECS is sent to the wrong continent — and the low TTL costs extra lookups (Following One Lookup Through Every Cache). Anycast: every PoP announces the *same* IP prefix over BGP and the internet’s routing delivers each packet to the topologically nearest PoP (Internet Routing: Autonomous Systems and BGP). No DNS games and instant failover, but "nearest by BGP" is not always nearest by latency, and a route change can shift a live TCP connection to a PoP that has no state for it — rare in practice, and one reason QUIC’s connection IDs help.The cache is keyed on URL (plus whatever
Varynames) and governed by the origin’s headers:Cache-Control: public, max-age/s-maxage,stale-while-revalidate,stale-if-error;private,no-storeor aSet-Cookiekeep a response out. Hit ratio is the metric, and it is destroyed by unkeyed variety:Vary: CookieorVary: User-Agent, query strings in random order, per-user URLs. Content-hashed filenames with a year-long TTL plus a short TTL on the HTML that references them is the standard pattern; purging is the escape hatch and it is eventually consistent across PoPs.For uncacheable responses the CDN is still a fast front door. The TCP and TLS handshakes happen against the 10 ms edge instead of the 150 ms origin; the edge holds warm, pooled, already-slow-started connections to the origin, so the user’s request rides an established path; the edge speaks HTTP/3 and Brotli to the user while the origin speaks HTTP/1.1; the CDN’s private backbone often beats the public internet between edge and origin; a shield or tiered cache collapses thousands of edge misses into one origin request; and DDoS traffic is absorbed at the edge. The trade-off is that the CDN is now an L7 proxy with everything that implies (Forward and Reverse Proxies): it has its own timeouts and error codes, the origin sees CDN IPs, and it is a single vendor whose outage takes every customer down at once.
Full interview question, with follow-ups and red flags →A router has routes for
10.0.0.0/8,10.4.0.0/16and0.0.0.0/0. A packet arrives for10.4.7.9. Which route wins, and why is that the rule?Show a strong answerHide the answer
All three routes match
10.4.7.9— the default matches everything,/8matches anything starting with 10,/16matches10.4.x.x— and the router picks the one with the longest prefix,10.4.0.0/16. The rule encodes specificity: a longer prefix is a more precise statement about where that address lives. It is what lets a table hold a broad route ("everything in 10/8 goes to the core") and carve exceptions out of it ("except 10.4/16, which goes to the Frankfurt link") without listing every subnet. The default route is simply the shortest possible prefix, so it loses to anything.Host routes (
/32in IPv4,/128in IPv6) are the other extreme: they always win, which is how VPN clients steer one server through the tunnel, and how a stray/32can hijack traffic to a single address. Between matches of equal length, metric/administrative distance decides; equal-cost routes can be used together (ECMP), hashing flows across paths.The same lookup runs on every host, not just routers: your laptop chooses between
Full interview question, with follow-ups and red flags →192.168.1.0/24 dev wlan0(on-link), a VPN’s10.0.0.0/8 dev tun0, anddefault via 192.168.1.1. When a VPN pushes a/16that overlaps the office network, longest-prefix match sends office traffic into the tunnel — silently.ip route get <dst>on Linux shows the winner.Your API is fully dynamic and uncacheable, yet putting it behind a CDN cut p50 latency for Australian users by 300 ms. Where did the 300 ms go?
Show a strong answerHide the answer
TCP and TLS handshakes now happen against a nearby edge (2–3 RTTs × ~150 ms saved) and the edge-to-origin leg uses warm pooled connections.
Full interview question, with follow-ups and red flags →