← All posts
BGP

The BGP Update Message and How Best-Path Selection Actually Works

The BGP Update Message and How Best-Path Selection Actually Works

Once a BGP session reaches Established, almost everything interesting happens inside one message type: UPDATE. It is how a router says "here is a prefix you can reach through me" or "forget that prefix." This post covers two things only: what an UPDATE carries, and how a router picks one winner when it learns the same prefix from several places.

Session setup is covered in BGP peering: the TCP session and FSM states, and the AS_PATH attribute's role in loop prevention is covered in BGP path vector and AS_PATH loop prevention. Here we stay on the UPDATE format and the decision process defined in RFC 4271.

What an UPDATE message contains

Every BGP message starts with a 19-byte header: a 16-byte marker, a 2-byte length and a 1-byte type (UPDATE is type 2). The base maximum message size is 4096 bytes; RFC 8654 allows larger messages if both peers negotiate the Extended Message capability. After the header, an UPDATE has four parts:

  • Withdrawn Routes Length and Withdrawn Routes: prefixes that are no longer reachable.

  • Total Path Attribute Length: size of the attribute block that follows.

  • Path Attributes: properties that apply to every prefix announced in this message.

  • NLRI (Network Layer Reachability Information): the IPv4 prefixes being announced, each as a length plus the minimum number of bytes needed.

The key design point: one UPDATE describes one set of attributes and many prefixes sharing them. Prefixes with different attributes need different UPDATEs. An UPDATE can also carry only withdrawals, or only announcements. For IPv6 and other address families, reachability moves into the MP_REACH_NLRI and MP_UNREACH_NLRI attributes from RFC 4760, so the classic withdrawn and NLRI fields stay empty.

The attributes that matter for path selection

RFC 4271 sorts attributes into categories. Knowing the category tells you what a router does when it sees (or does not see) the attribute.

  • Well-known mandatory: ORIGIN, AS_PATH, NEXT_HOP. Must be in every UPDATE that announces IPv4 NLRI and must be recognized by every implementation.

  • Well-known discretionary: LOCAL_PREF (used inside an AS, over iBGP) and ATOMIC_AGGREGATE.

  • Optional non-transitive: MULTI_EXIT_DISC (MED). A receiving AS does not pass it on to other ASes.

  • Optional transitive: AGGREGATOR, and later additions such as communities (RFC 1997).

A few details that trip people up:

  • ORIGIN has three values: IGP (0), EGP (1) and incomplete (2). "Incomplete" typically means the route was redistributed.

  • NEXT_HOP is normally rewritten to the sender's own address when sending to an eBGP peer (a router can leave it unchanged when the original next hop is on the same shared segment), but by default is left unchanged when a route is passed to an iBGP peer. That is why iBGP needs the next hop to be reachable through the IGP.

  • AS_PATH gets the local AS prepended when advertising to an eBGP peer.

  • LOCAL_PREF is never sent to eBGP peers; it only travels inside one AS.

Why a decision process is needed

A router can learn the same prefix from several peers. Each copy is stored in the Adj-RIB-In. RFC 4271 splits the work into phases: Phase 1 calculates a degree of preference for each route, Phase 2 picks the best route per prefix and installs it in the Loc-RIB, and Phase 3 decides what goes into each peer's Adj-RIB-Out. Within Phase 2, a route is only eligible if its NEXT_HOP is resolvable and its AS_PATH does not contain the local AS (section 9.1.2).

The best path selection order

RFC 4271 defines this in two parts: the degree of preference (highest LOCAL_PREF for iBGP-learned routes, or locally configured policy for eBGP-learned ones) in section 9.1.1, then the tie-breaking rules in section 9.1.2.2. Flattened into one ordered list, the first step that produces a single winner ends the process:

  1. Highest LOCAL_PREF. Operator policy comes first. Raise it on the exit you prefer.

  2. Shortest AS_PATH. Fewer AS hops wins. (An AS_SET counts as one hop regardless of its size.)

  3. Lowest ORIGIN. IGP is preferred over EGP, which is preferred over incomplete.

  4. Lowest MED, compared only among routes from the same neighboring AS in the base specification. Many implementations offer knobs to change that comparison, so check your platform's documentation.

  5. eBGP over iBGP. Prefer a route learned from an external peer.

  6. Lowest IGP cost to the NEXT_HOP. This is "hot potato" routing: leave the AS at the nearest exit.

  7. Lowest BGP Identifier (router ID) of the advertising peer.

  8. Lowest peer address. The final tie-breaker.

Real implementations add steps. Many vendors have a proprietary pre-step before LOCAL_PREF (Cisco's "weight" is the well-known example, and it is local to the router and never advertised), and most add options such as multipath or comparing the age of the route. These are vendor behaviors beyond RFC 4271, so verify them for the platform you run. The RFC list above is the portable baseline.

A worked example

The values below are illustrative, not captured from a real network. Router R1 learns 198.51.100.0/24 from three places:

Path  Learned from   LOCAL_PREF  AS_PATH         ORIGIN  MED  Type
A     203.0.113.1    100         65010 65100     IGP     50   eBGP
B     203.0.113.5    100         65020 65030 65100  IGP  10   eBGP
C     10.0.0.2       200         65100           IGP     0    iBGP

Step 1 compares LOCAL_PREF. C has 200, the others 100, so C wins immediately. Nothing after that step is consulted. The shorter AS_PATH of C does not even matter here; the policy decision came first.

Now suppose C is gone and LOCAL_PREF is equal for A and B. Step 2 compares AS_PATH length: A has 2 hops, B has 3, so A wins. B's lower MED never gets a vote, because MED is step 4. This is the classic surprise: people set MED expecting it to steer traffic, then find that an earlier attribute overrides it.

Reading it on a router

The exact syntax differs between vendors. As an illustration in a Cisco IOS-style format (output shortened and not from a real device):

R1# show ip bgp 198.51.100.0/24
BGP routing table entry for 198.51.100.0/24
  Paths: (2 available, best #1)
  65010 65100
    203.0.113.1 from 203.0.113.1 (192.0.2.1)
      Origin IGP, metric 50, localpref 100, valid, external, best
  65020 65030 65100
    203.0.113.5 from 203.0.113.5 (192.0.2.5)
      Origin IGP, metric 10, localpref 100, valid, external

Read each block as one candidate path. The header line shows the AS_PATH, the next line the NEXT_HOP and the peer's router ID, and the attribute line shows ORIGIN, MED ("metric"), LOCAL_PREF and the flags. When two candidates differ, walk the list from step 1 and find the first attribute where they differ; that is your reason for the winner.

To change the outcome, set the attribute that is evaluated earliest and is under your control. A small illustrative route-map that prefers one exit by raising LOCAL_PREF:

route-map PREFER-ISP-A permit 10
 set local-preference 200
!
router bgp 65001
 neighbor 203.0.113.1 route-map PREFER-ISP-A in

Troubleshooting checklist

  • Route is missing entirely: check the NEXT_HOP is reachable and the AS_PATH does not contain your own AS.

  • Wrong exit chosen: compare candidates attribute by attribute in the order above and stop at the first difference.

  • MED seems ignored: confirm both paths come from the same neighboring AS, and that no earlier step already decided.

  • Traffic leaves via a far exit: look at the IGP cost to each NEXT_HOP, since step 6 can quietly decide the winner.

  • Session resets during large updates: BGP runs over TCP, so loss or MTU problems can delay keepalives until the Hold Timer expires. See TCP reliability explained for how TCP handles delivery underneath.

Key takeaways

  • An UPDATE carries withdrawals, a shared set of path attributes and the prefixes (NLRI) that use them.

  • ORIGIN, AS_PATH and NEXT_HOP are mandatory for IPv4 announcements; LOCAL_PREF stays inside the AS; MED is optional and non-transitive.

  • Best path selection is an ordered list, not a score. The first attribute that differs decides.

  • Policy (LOCAL_PREF) beats path length, which beats origin, MED, eBGP/iBGP, IGP cost and finally router ID.

Comments (0)

Please sign in to comment.

  • Be the first to comment.