Skip to main content
← CONCEPTS / SYSTEM DESIGN
An end-to-end domain learning journey

BGP Anycast & Maglev ECMP
Consistent Hashing at Global Edge.

BGP Anycast announces the same /24 IP prefix from every global PoP for automatic L3 proximity and DDoS absorption, while Maglev ECMP consistent lookup tables keep 5-tuple TCP flows pinned during L4/L7 scale-out.
Start the journey5 sections· Last modified Nov 26, 2026
FIRST PRINCIPLES. PRODUCTION SCALE.
The idea to keep with you

When building a global edge ingress, pair BGP Anycast with Maglev consistent hashing and local conntrack so scale-out only shifts ~1/N new flows.

TL;DR30-second simplified summary
▾

BGP Anycast eliminates stale GeoDNS caches at L3, while Maglev's prime-sized permutation table + 5-tuple conntrack limits flow disruption to ~1/N new connections during L4/L7 pool changes.

Before you begin · Building blocksAlready know these? Jump to Stage 01 ↓
TCP 5-Tuple & Connection State
Know that TCP identifies a connection by (src_ip, src_port, dst_ip, dst_port, proto) and resets with RST if packets suddenly land on a different L4/L7 proxy without shared state.
Consistent Hashing
Understand how consistent hashing minimizes key movement when backend nodes are added or removed.
Full walkthrough · 5 stages
01
L4 · FUNDAMENTALS

P · Problem & Why Modulo ECMP Breaks Active TCP.

Suppose a PoP runs N = 10 L4 load balancers behind an ECMP router. Every incoming packet is hashed by its 5-tuple and sent to hash % 10.

If LB #3 crashes or you scale up to N = 11, hash % 11 changes the destination for roughly 10/11 (~91%) of all active TCP connections! Packets for an established connection land on a brand-new L4/L7 box that has never seen its SYN handshake, causing immediate TCP RSTs and dropped user requests.

ECMP Reshuffling

Naive Modulo ECMP vs. Maglev Consistent Lookup Table

Before · Modulo hash(5-tuple) % N
  • ~91% flows remap on N=10->11
  • TCP RST storm on scale-out
  • Stale GeoDNS TTLs on failover
After · Anycast + Maglev Table
  • O(1) array lookup (65,537 slots)
  • Only ~1/N slots move on change
  • BGP withdraws dead PoP in <3s
02
L4 · FUNDAMENTALS

R · Representation: BGP Anycast + Maglev Permutation Table.

At the global layer, 30+ PoPs announce the exact same /24 IPv4 prefix (the minimum routable BGP prefix on the public internet). Each user's ISP picks the shortest BGP AS-path.

Inside each PoP, Maglev builds a lookup table entry[0..M-1] where M is a prime much larger than N (e.g., M = 65,537). Each backend i generates a pseudo-random permutation of 0..M-1 using two independent hashes (offset_i, skip_i). Backends take turns claiming their next unclaimed slot in entry[] until all M slots are filled.

Global to PoP Packet Path

User -> BGP Anycast PoP -> ECMP -> Maglev L4 -> Envoy L7

Global UserNearest Anycast PoPMaglev L4 (XDP)Envoy L7 Backend1. Packet to Anycast 198.51.100.12. ECMP 5-tuple to L4 node3. table[hash % 65537] -> Envoy4. DSR direct return to client
03
L5 · APPLIED DESIGN

I · Invariant: Deterministic Maglev Table & Conntrack.

Maglev guarantees that if backend k is removed, ~1/N of the 65,537 slots change, and all other (N-1)/N slots keep pointing to the exact same backend.

Every L4 balancer runs the identical deterministic algorithm to build the same 65,537-entry table from the healthy backend list. For existing TCP flows that happen to land in the 1/N remapped slots during a scale-up, a local LRU connection-tracking table (keyed by 5-tuple) remembers the original L7 backend for established flows.

L4 XDP Fast Path

Conntrack Cache + Maglev Table + DSR Encapsulation

XDP Packet Hookingress5-Tuple ConntrackcomputeMaglev Table(65537)storageGRE/IPIP -> L7 DSRcontrolestablished flowmiss -> SYN lookupencapsulate to backend
04
L5 · APPLIED DESIGN

M · Mechanics at Scale: BGP Route Shifts & Asymmetric Paths.

BGP is stateless: an internet router can switch a user's path from PoP A to PoP B mid-download if a peering link flaps.

When a TCP packet arrives at PoP B for a connection initiated at PoP A, PoP B has no TCP state! Two production patterns solve this: (1) QUIC with Connection IDs encoding the home PoP/server ID so PoP B forwards packets over an internal backbone tunnel to PoP A; (2) Anycast-over-tunnel where edge PoPs proxy TCP via a regional origin shield.

PoP Health & BGP Drain

Healthy Announce -> Graceful Prepend -> Drain -> Withdraw

graceful-shutdowndrain 60sreroute stragglersAnycast HealthyAS-Path Prepend(Drain)BGP PrefixWithdrawnResidual FlowTunnel
Engineering Decision Framework
Problem
How to route global traffic to the nearest healthy PoP without stale DNS caches or broken TCP state.
Naive Solution
Use GeoDNS with 60s TTL and modulo ECMP inside each PoP.
Why It Fails
ISP resolvers ignore short TTLs during outages, and modulo ECMP resets ~91% of flows when scaling backends.
Alternatives
GeoDNS + stateful NAT vs. BGP Anycast + Maglev ECMP + DSR.
Engineering Decision
Announce shared /24 via BGP Anycast, run Maglev ECMP + conntrack at L4, and use QUIC CIDs or internal tunnels for PoP-flap resilience.
Trade-Offs
Requires own ASN and /24 IPv4 block plus BGP peering, and careful graceful-drain tooling for stateful TCP.
05
L6 · SCALE & TRADE-OFFS

E · Edge Cases, Assumptions & When NOT to Use Anycast.

You cannot announce a single /32 IP over public BGP—global DFZ routers filter anything more specific than /24 IPv4 (or /48 IPv6).

When NOT to use BGP Anycast directly: for multi-hour stateful WebSocket or SSH sessions where mid-stream BGP convergence could shift PoPs, terminate TCP at a stable unicast or QUIC-CID-routed edge, or tunnel straggler packets over a private backbone.

Global Anycast Mesh

Three Independent Anycast PoPs Sharing 198.51.100.0/24

US-East PoP198.51.100.0/24EU-West PoP198.51.100.0/24AP-South PoP198.51.100.0/24
Decision Exercise · Make the Call

You scale your PoP's L4 load balancer pool from 8 to 9 nodes using stateless hash(5-tuple) % N ECMP. Immediately, 88% of active HTTPS downloads fail with Connection reset by peer. What fixes this?

Practice · Explain the consequences

Practice: Make the trade-off yourself.

Question 1 of 3
Score: 0/3

Why does Maglev use a prime table size M (e.g., 65,537) much larger than the backend count N?

Recommended Learning Flow