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.

PC1's default gateway is 192.168.10.1, a virtual router with MAC 00:00:5e:00:01:0a. R1 (priority 120) is Master: it owns the virtual address and forwards. R2 (priority 100) is Backup: it only listens to R1's advertisements, sent every second to 224.0.0.18.
The PC talks to the virtual router; the Master plays that part, and the Backup waits.

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:

TimerFormula (RFC 5798)R1, priority 120R2, priority 100
Skew_Time(256 − Priority) × Master_Adver_Interval / 2560.531 s0.609 s
Master_Down_Interval3 × Master_Adver_Interval + Skew_Time3.531 s3.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)

Time line of Demo 3. R1 advertises at 0 and 1.000 s and crashes at 1.200 s. A ping at 1.200 s is lost because the switch still sends the virtual MAC to port 1. R2's Master_Down_Timer, restarted at 1.000 s, fires at 4.609 s; R2 becomes Master, sends an advert and a gratuitous ARP, and the switch moves the virtual MAC to port 2. A ping at 4.700 s goes through R2.
The outage lasts until the Backup's timer runs out; the gratuitous ARP moves the virtual MAC, not the PC.

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 0timer = Skew_Time
priority ≥ its own, or preemption offtimer = Master_Down_Interval (stays Backup)
priority lower than its own, preemption ondiscards 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 v3HSRP v1
RolesMaster / BackupActive / Standby (others Listen)
Messagesadvert to 224.0.0.18, IP protocol 112hello to 224.0.0.2, UDP 1985
Timersadvert 1 s, down after about 3.6 shello 3 s, hold 10 s
Virtual MAC00:00:5e:00:01:VRID00:00:0c:07:ac:group
Preemptionon by defaultoff by default
Virtual IPmay 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.

On the mind map

Open the Networking mind mapNetworkingLayers & links: frames on one local networkLayers & linksIP & routing: packets hop between networksIP & routingCore services: address, name and timeCore servicesTCP, UDP & sockets: end to end between two portsTCP, UDP & socketsHTTP & TLS: what a browser does per requestHTTP & TLSAPIs & push: how apps talk to serversAPIs & pushProxies & edge: boxes in the middle of the pathProxies & edgeContainers: virtual networks from Linux partsContainersReal-time media: voice and video over UDPReal-time mediaSecurity at the edge: filter traffic at the borderSecurity at the edgeRouter forwardingRouter forwardinglongest prefix match picks the next hopVRRP / HSRP failoverBackup takes over the gateway in 3.6 sSDN and OpenFlowSDN and OpenFlowcontroller writes rules, switch looks upTracerouteTracerouteTTL 1, 2, 3: each hop sends Time ExceededNATNATmaps private IP:port to one public IPIPv4 vs IPv6IPv4 vs IPv632 vs 128 bits; SLAAC, no NAT neededAnycast and BGPAnycast and BGPsame prefix announced from many sitesOSPF and BGP policyOSPF and BGP policyPolicy first, then path, then IGP costDistance vector vs link stateDistance vector vs link stateRIP shares distances; OSPF floods the mapSubnets and CIDRSubnets and CIDR/24 = mask: same subnet? ARP, else routerVLSM PlanningVLSM PlanningLargest first: no gaps, one summary routeRouter queues and AQMRouter queues and AQMFull buffers add delay; drop earlyDelay, loss, throughputDelay, loss, throughputSlowest link sets pace; full buffer dropsOpen the Networking mind mapNetworkingpackets hop between networksIP & routingRouter forwardingRouter forwardinglongest prefix match picks the next hopVRRP / HSRP failoverBackup takes over the gateway in 3.6 sSDN and OpenFlowSDN and OpenFlowcontroller writes rules, switch looks upTracerouteTracerouteTTL 1, 2, 3: each hop sends Time ExceededNATNATmaps private IP:port to one public IPIPv4 vs IPv6IPv4 vs IPv632 vs 128 bits; SLAAC, no NAT neededAnycast and BGPAnycast and BGPsame prefix announced from many sitesOSPF and BGP policyOSPF and BGP policyPolicy first, then path, then IGP costDistance vector vs link stateDistance vector vs link stateRIP shares distances; OSPF floods the mapSubnets and CIDRSubnets and CIDR/24 = mask: same subnet? ARP, else routerVLSM PlanningVLSM PlanningLargest first: no gaps, one summary routeRouter queues and AQMRouter queues and AQMFull buffers add delay; drop earlyDelay, loss, throughputDelay, loss, throughputSlowest link sets pace; full buffer drops
This page on the Networking mind map · 62 pages in 10 branches · open it to see the whole map and test yourself in recall mode.