The idea: one gateway address, two routers
A PC has one default gateway, set once by hand or by DHCP. If that router dies, the PC cannot reach anything outside its LAN, even when a second router sits right next to it. The PC will not switch to the other router by itself. First-hop redundancy fixes this without touching the PC: two routers share one gateway address, and one of them does the work at a time.
VRRP (Virtual Router Redundancy Protocol, RFC 5798) is the open standard. On the page, R1
(192.168.10.2) and R2 (192.168.10.3) form virtual router VRID 10 with the
virtual IP 192.168.10.1 and the virtual MAC
00:00:5e:00:01:0a (always 00:00:5e:00:01: + the VRID). PC1 uses
192.168.10.1 as its gateway. The clock counts in milliseconds; frames take no time.
Master and Backup
One router is the Master. It answers ARP for the virtual IP with the virtual MAC, and it
forwards every frame sent to the virtual MAC. Every Advertisement_Interval (1 s by default) it
multicasts an advertisement to 224.0.0.18: IP protocol 112, TTL 255, VRID 10, its
priority. The other router is a Backup. It stays silent: no ARP replies for the virtual IP, no
forwarding for the virtual MAC. It only listens. (Demo 1: Normal)
The Master sends its adverts from the virtual MAC. That keeps the switch's MAC table pointing the virtual MAC at the Master's port. How the PC finds the gateway's MAC in the first place is ordinary ARP, explained on How ARP Finds a MAC Address.
Priority and the timers
Each router has a priority from 1 to 254 (default 100); 255 is kept for the router that really owns the virtual IP as its interface address. The highest priority becomes Master. A Backup decides the Master is gone when it hears no advert for the Master_Down_Interval:
| Timer | Formula (RFC 5798) | R1, priority 120 | R2, priority 100 |
|---|---|---|---|
Skew_Time | (256 − Priority) × Master_Adver_Interval / 256 | 0.531 s | 0.609 s |
Master_Down_Interval | 3 × Master_Adver_Interval + Skew_Time | 3.531 s | 3.609 s |
The skew makes a higher priority wait a little less. At power-on both routers start in Initialize, go to Backup and start their timers. R1's runs out first, at 3.531 s, so R1 becomes Master and its first advert stops R2's timer before it fires. (Demo 2: Boot) The page rounds the times down to whole milliseconds.
Failover and the gratuitous ARP
When the Master crashes, nothing is sent. The Backup just stops hearing adverts. Its timer was last restarted by
the advert at 1.000 s, so it fires at 1.000 + 3.609 = 4.609 s. R2 becomes Master and sends an
advert and a gratuitous ARP: "192.168.10.1 is at 00:00:5e:00:01:0a",
from the virtual MAC. (Demo 3: Master crashes)
The MAC in the gratuitous ARP is the same as before, so PC1's ARP cache does not change, and its gateway does not
change. The ARP is for the switch: it learns the virtual MAC on R2's port p2. Between the
crash and 4.609 s the switch still sends PC1's frames to p1, where nobody listens, and pings time out.
Graceful shutdown: priority 0
A Master that is shut down on purpose sends one last advert with priority 0, which means "I am
leaving". A Backup that hears it sets its timer to only Skew_Time. R2 takes over 0.609 s later instead
of 3.609 s. (Demo 4: Graceful shutdown)
Preemption
Preempt_Mode (default on) decides what happens when R1 comes back. R1 starts as Backup. With
preemption on, it discards R2's adverts because their priority 100 is lower than its own 120. Its timer runs out at
restart + 3.531 s, it becomes Master, and R2 steps down as soon as it hears priority 120. With preemption off, R1
accepts R2's adverts and restarts its timer each time, so R2 keeps the job. Turning preemption off avoids a second
short outage when a flapping router comes back. (Demos 5 and 6)
| A Backup hears an advert with… | What it does |
|---|---|
| priority 0 | timer = Skew_Time |
| priority ≥ its own, or preemption off | timer = Master_Down_Interval (stays Backup) |
| priority lower than its own, preemption on | discards it; the timer keeps running |
HSRP compared
HSRP (Hot Standby Router Protocol) is Cisco's older protocol for the same job, described in RFC 2281. The idea is the same; the names and defaults differ:
| VRRP v3 | HSRP v1 | |
|---|---|---|
| Roles | Master / Backup | Active / Standby (others Listen) |
| Messages | advert to 224.0.0.18, IP protocol 112 | hello to 224.0.0.2, UDP 1985 |
| Timers | advert 1 s, down after about 3.6 s | hello 3 s, hold 10 s |
| Virtual MAC | 00:00:5e:00:01:VRID | 00:00:0c:07:ac:group |
| Preemption | on by default | off by default |
| Virtual IP | may be a real router's own address (priority 255) | must be a spare address |
Without first-hop redundancy
If PC1's gateway is R1's own address 192.168.10.2, R1's crash cuts PC1 off. R2 is still working, and
VRRP even makes it Master of 192.168.10.1, but PC1 never uses that address. (Demo 7: Without
VRRP) How each router then picks the next hop is on How a Router Forwards a
Packet; how a PC gets its gateway address is on How a Device Gets an IP Address.
See also the Ethernet switch page for how the switch learns which port each MAC is on, and Redis Sentinel for the same "detect the dead leader by missed heartbeats, then promote a standby" idea in a database.
What the page leaves out
More than two routers in one group, several VRIDs for load sharing (R1 Master of one, R2 of another), object
tracking (lowering the priority when the uplink fails, a vendor feature), Accept_Mode (whether the
Master answers pings to the virtual IP), VRRP authentication (removed in v3), IPv6 (VRRP sends Neighbor
Advertisements instead of ARP), HSRP v2 and GLBP, the way return traffic from the Internet may come back through
either router, ARP retries and cache aging on PC1, and IGMP snooping on the switch.