Tags: aws route53 dns spring cloud cloudformation
Weighted routing is a powerful strategy for managing traffic distribution, enabling everything from simple load balancing to sophisticated canary deployments. By combining AWS Route 53 at the DNS layer with Spring Cloud Gateway at the application layer, you can create a robust, multi-tiered routing architecture.
The Domain Name System (DNS) is often called the "phone book of the internet." Its primary job is to translate human-readable domain names like api.example.com into machine-readable IP addresses like 203.0.113.42.
When you type a URL into your browser, a multi-step resolution process begins:
8.8.8.8)..com TLD.Understanding record types is crucial for configuring routing policies:
| Record Type | Purpose | Example |
|---|---|---|
| A | Maps a domain to an IPv4 address |
api.example.com -> 203.0.113.42
|
| AAAA | Maps a domain to an IPv6 address |
api.example.com -> 2001:0db8::1
|
| CNAME | Creates an alias for another domain |
www.example.com -> example.com
|
| ALIAS | Route 53 specific; like CNAME but for root domains |
example.com -> alb-123.aws.com
|
| NS | Lists the authoritative nameservers |
example.com -> ns-123.awsdns.com
|
| TXT | Stores arbitrary text (used for SPF/verification) |
example.com -> "v=spf1 ..."
|
Route 53 is a highly available and scalable cloud DNS web service. It’s named after Port 53, the standard port for DNS queries.
While Route 53 handles routing at the "front door," a Reverse Proxy sits in front of your backend servers to orchestrate internal communication.
Spring Cloud Gateway is a popular reactive reverse proxy built on Spring WebFlux. It provides:
id: A unique identifier for the route, used for logging and metrics.uri: The destination address (load balancer or service name) where the request will be forwarded.predicates (e.g., Path): These are the "if" statements. In this case, if the request path starts with /api/users/**, this route is triggered.filters: These allow you to modify the request before it hits the backend or the response before it returns to the client:
AddRequestHeader: Injects metadata (like source identification) for the downstream service to consume.RewritePath: Strips the /api/users prefix so that the backend service (which might just expect /login or /profile) receives a clean path.CircuitBreaker: Provides fault tolerance. If order-service goes down, the gateway can return a fallback response instead of hanging.Weighted routing allows you to associate multiple resources with a single domain name and choose how much traffic is routed to each resource.
You are launching a new "Canary" version of your API. You want 80% of traffic to stay on the stable version and 20% to go to the new version.
203.0.113.42 (Weight: 80)203.0.113.99 (Weight: 20)Always use a low TTL (e.g., 60 seconds) for weighted records. This ensures that when you update weights, DNS resolvers across the world pick up the change quickly.
Using AWS CloudFormation (IaC), you can automate the provisioning of these records. Here is a corrected template for setting up weighted routing:
To adjust the traffic flow (e.g., moving to 100% stable), simply update the stack parameters: