Networks and sessions
The internet is not one network. It is more than 75,000 separate networks called autonomous systems (ASes). Each is run by one organisation and has its own AS number. Here are four.
Border routers of neighbouring ASes talk BGP-4 over a TCP connection to port 179. Between two ASes this is external BGP (eBGP). Routers inside one AS share what they learn over internal BGP (iBGP).
Spreading the news
A owns the address block 203.0.113.0/24. It sends B an UPDATE message: "reach 203.0.113.0/24 through me". Its AS_PATH lists every AS the news has crossed, so far just 64496.
A pays two providers, B and C, so it has two ways into the internet. It sends C the same announcement, AS_PATH 64496.
B passes the news on to D, its peer. On the way out B puts its own number in front of the AS_PATH, so D hears 64497 64496.
B tells every neighbour, A included. A finds its own number, 64496, in the AS_PATH 64497 64496 and drops the update. This one rule stops routes from going round in circles.
C prepends its number too and tells D, its customer: AS_PATH 64498 64496. D now knows two ways to reach 203.0.113.0/24, one through B and one through C.
Choosing a path
Policy comes first. B is a peer and costs D nothing; C is a provider D pays. So D gives B's route the higher local preference and uses it. Only on a tie would the shorter AS_PATH decide.
A user in D opens a site on 203.0.113.10. D sends the packets to B, and B hands them to A. News travels from A outwards; traffic travels back towards A along the chosen path.
When a link fails
The cable between A and B breaks, so their BGP session closes, at the latest after the 90-second hold time. B deletes every route it learned from A.
B has no other way to 203.0.113.0/24. It sends D an UPDATE that withdraws the prefix, and D crosses out the route through B.
D still has the route through C, AS_PATH 64498 64496. It becomes the best path, and packets for 203.0.113.10 now flow D to C to A. Nobody rewired anything by hand.
The messages, field by field
;; step 1/12 · as-map · local ;; at A, B, C, D A · AS64496 Origin network · owns 203.0.113.0/24 B · AS64497 Transit network B C · AS64498 Transit network C D · AS64499 Your ISP · owns 198.51.100.0/24 links (who pays whom): AB 192.0.2.0/30 A pays B for transit AC 192.0.2.4/30 A pays C for transit BD 192.0.2.8/30 B and D are peers: nobody pays CD 192.0.2.12/30 D pays C for transit AS numbers 64496-64511 are reserved for documentation (RFC 5398).
AS numbers 64496-64511 (RFC 5398) and the prefixes 192.0.2.0/24, 198.51.100.0/24 and 203.0.113.0/24 (RFC 5737) are reserved for documentation. Local preference values 200 / 100 / 50 are illustrative. Each network sets its own, usually customer > peer > provider.