Sub topic 8
Routing policies: simple, weighted, latency, failover and more
This chapter assumes you can create a hosted zone, write A and CNAME records, and read a change batch (a JSON file that lists record changes). It also assumes you know what TTL means and that a health check can report an endpoint as healthy or unhealthy. If any of these are unfamiliar, review the earlier chapters on record types, alias records and health checks first.
What a routing policy does
A routing policy tells Route 53 how to choose an answer when a query arrives for a record name. Route 53 evaluates the policy on the authoritative side. It looks at the query, picks one or more record values, and returns them. Recursive resolvers then cache the answer for the record's TTL.
Two consequences follow from this:
- Route 53 decides per query that reaches it, not per user request. A resolver that caches an answer for 60 seconds sends every client behind it to the same endpoint for that time.
- The policy only changes which DNS answer is returned. It does not proxy traffic, balance connections, or notice an outage unless a health check is attached.
Every record in a policy group shares the same name and type. A SetIdentifier distinguishes the members of the group, and it must be unique within that name and type. You cannot mix policy types for the same name and type. For example, you cannot have a weighted app.example.com A record and a latency app.example.com A record together.
The available policies
| Policy | Chooses an answer by | Typical use |
|---|---|---|
| Simple | Nothing. Returns every value in the record | One endpoint, or a fixed set of values |
| Weighted | Relative weights | Gradual rollouts, A/B tests |
| Latency | Lowest network latency from the user to an AWS Region | Multi-Region applications |
| Failover | Health check status of a primary | Active-passive disaster recovery |
| Geolocation | The user's location (continent, country, US state) | Content localization, legal restrictions |
| Geoproximity | Distance between user and resource, adjustable with a bias | Shifting load toward or away from a location |
| IP-based | The client's source IP range (CIDR) | Routing known networks or ISPs to specific endpoints |
| Multivalue answer | Health. Returns up to eight healthy values | Cheap client-side spreading with health checking |
Alias records work with all of these policies. The alias target replaces the ResourceRecords list, and alias records have no TTL of their own.
Simple routing
Simple routing is the default. A record has one name, one type, and one or more values. If a record has several values, Route 53 returns all of them in random order, and the client picks one, usually the first. A simple record has no SetIdentifier and cannot have a health check attached to individual values. Use it when you have a single endpoint, or when you do not need Route 53 to make choices.
Weighted routing
Each weighted record has a weight from 0 to 255. Route 53 sums the weights of all records with that name and type, then answers with each record in proportion to weight / sum. Weights are relative, so 90 and 10 behave the same as 9 and 1.
The following change batch sends about 90 percent of answers to the blue endpoint and 10 percent to the green one.
{
"Changes": [
{
"Action": "UPSERT",
"ResourceRecordSet": {
"Name": "app.example.com",
"Type": "A",
"SetIdentifier": "blue",
"Weight": 90,
"TTL": 60,
"ResourceRecords": [{ "Value": "192.0.2.20" }]
}
},
{
"Action": "UPSERT",
"ResourceRecordSet": {
"Name": "app.example.com",
"Type": "A",
"SetIdentifier": "green",
"Weight": 10,
"TTL": 60,
"ResourceRecords": [{ "Value": "192.0.2.30" }]
}
}
]
}To roll out gradually, upsert new weights in steps, for example 90/10, then 50/50, then 0/100. A weight of 0 stops traffic to that record. The exception is when every record in the group has weight 0. In that case Route 53 treats them as equal and returns all of them.
Three details commonly surprise people:
- The split is statistical over many queries. With ten queries you may see 7/3 or 10/0.
- Resolver caching skews the split. One busy resolver holding a cached answer for a large user population sends all of those users to one side until the TTL expires.
- A low TTL makes weight changes take effect faster, at the cost of more queries to Route 53.
You can watch the distribution by querying a Route 53 name server directly, which bypasses resolver caches. Use one of your own zone's name servers.
for i in $(seq 1 50); do
dig @ns-123.awsdns-45.com app.example.com A +short +norecurse
done | sort | uniq -cThe output should show roughly 45 answers for 192.0.2.20 and 5 for 192.0.2.30.
Latency routing
Latency routing needs one record per AWS Region where you run the application. Each record carries a Region value. When a query arrives, Route 53 returns the record whose Region has the lowest measured network latency to the resolver that sent the query, based on latency data AWS collects between networks and Regions.
{
"Changes": [
{
"Action": "UPSERT",
"ResourceRecordSet": {
"Name": "api.example.com",
"Type": "A",
"SetIdentifier": "us-east",
"Region": "us-east-1",
"TTL": 60,
"ResourceRecords": [{ "Value": "192.0.2.60" }]
}
},
{
"Action": "UPSERT",
"ResourceRecordSet": {
"Name": "api.example.com",
"Type": "A",
"SetIdentifier": "eu-west",
"Region": "eu-west-1",
"TTL": 60,
"ResourceRecords": [{ "Value": "192.0.2.70" }]
}
}
]
}Some points to keep in mind:
- The
Regionvalue names the AWS Region that hosts the resource. It does not need to match the resource's actual location, but a mismatch makes the routing meaningless. - Latency is measured to the resolver, not to the end user. A user on a public resolver located far away may be routed on the resolver's position. EDNS Client Subnet support improves this for resolvers that send it.
- Latency routing alone does not remove an unhealthy endpoint. Attach a health check to each record so Route 53 skips a failed Region and answers with the next-lowest-latency one.
Failover routing
Failover is an active-passive pair. Route 53 answers with the primary record while its health check passes. When the check fails, Route 53 answers with the secondary record. The primary must have a health check. The secondary may have one too, and if it does not, Route 53 treats it as healthy.
{
"Changes": [
{
"Action": "UPSERT",
"ResourceRecordSet": {
"Name": "www.example.com",
"Type": "A",
"SetIdentifier": "primary",
"Failover": "PRIMARY",
"TTL": 60,
"HealthCheckId": "HEALTH_CHECK_ID",
"ResourceRecords": [{ "Value": "192.0.2.40" }]
}
},
{
"Action": "UPSERT",
"ResourceRecordSet": {
"Name": "www.example.com",
"Type": "A",
"SetIdentifier": "secondary",
"Failover": "SECONDARY",
"TTL": 60,
"ResourceRecords": [{ "Value": "192.0.2.50" }]
}
}
]
}Replace HEALTH_CHECK_ID with the ID returned by aws route53 create-health-check.
The time a user waits for failover is the sum of three parts. The health check must detect the failure, which is the request interval times the failure threshold. For a 30-second interval and a threshold of 3, that is about 90 seconds. Route 53 then starts returning the secondary. Finally, resolvers must let the cached primary answer expire, up to the TTL. A 60-second TTL with a 30-second interval and threshold 3 gives a worst case of roughly two and a half minutes.
You can also build active-active failover by giving several weighted or latency records their own health checks. Route 53 then removes unhealthy ones from the pool and answers only with healthy ones. If every record is unhealthy, Route 53 answers as if all were healthy, because returning something is better than returning nothing.
Geolocation, geoproximity, IP-based and multivalue
Geolocation maps the user's location to a record. You create records for a continent, a country, or a US state, and Route 53 uses the most specific match. Always create a default record (location *). Without it, a query from an unmapped location, or one whose location cannot be determined, gets no answer (NOERROR with no data). Geolocation does not tell you "nearest". A user in a country with no record falls back to the continent record, then the default.
Geoproximity routes by physical distance between the user and the resource. Each record can carry a bias that expands or shrinks the area it serves, which lets you move load toward a Region without writing a long list of countries. It is the policy to reach for when you want gradual, geography-based load shifting.
IP-based routing uses CIDR blocks you define in a CIDR collection. Queries whose source address matches a block go to the associated record. Use it when you know which networks should reach which endpoint, such as sending a particular ISP to a nearby edge location.
Multivalue answer returns up to eight records chosen at random from the healthy ones. Each record can have its own health check, which is the difference from a simple record with several values. It is not a replacement for a load balancer, because the client still chooses which address to use.
Combining policies
You can nest policies by pointing an alias record at another record in the same hosted zone. A common design is a latency record per Region, where each Region's alias points to a weighted pair of records for that Region. Route 53 evaluates the outer policy first, then the policy of the record it points to. The Route 53 traffic flow visual editor builds these trees for you, and it also lets you version and reuse them.
Set EvaluateTargetHealth to true on an alias record when you want Route 53 to take the health of the target into account. For alias records that point at AWS resources such as an Application Load Balancer, Route 53 can read the resource's own health without a separate health check. This is how you get failover from a load balancer that has no healthy targets.
Common errors
- Missing SetIdentifier. Every record in a weighted, latency, failover, geolocation, geoproximity, IP-based or multivalue group needs a unique one. Simple records must not have one.
- Mixing policies on one name and type. Route 53 rejects a change that creates a latency record next to a weighted record with the same name and type.
- No health check on a failover primary. The change is rejected or, if the primary is unchecked, never fails over.
- Missing geolocation default. Users outside your mapped locations receive no answer.
- Testing through a caching resolver. Repeated
digcalls against a public resolver show cached answers, not the policy. Query a Route 53 name server directly with+norecurse. - Long TTLs on policy records. A 3600-second TTL makes failover and weight shifts take up to an hour to reach users. Use 60 seconds or lower on records that change behavior.
Exercise
Your www.example.com failover pair has a primary health check with a request interval of 30 seconds and a failure threshold of 3. Both records have a TTL of 300 seconds. A user's resolver cached the primary answer one second before the primary stopped responding. Roughly how long can that user keep being sent to the failed primary, and what two settings would you change to shorten it?
Show answer
Detection takes about 90 seconds (30 s x 3), and the resolver's cached answer lasts up to 300 seconds from when it was cached, so the user can be sent to the failed primary for as long as about 300 seconds after the cache was filled. The worst case from the moment of failure is therefore close to 300 seconds, because the cache outlasts detection. Lower the TTL on both records (for example to 60 seconds) and lower the health check's request interval or failure threshold, remembering that a shorter interval or threshold detects failures faster but can cause false positives.