Optimal DNS TTL Configuration for Static Web Hosting: Best Practices for Stability and Performance


1 views

html

Optimal DNS TTL Configuration for Static Web Hosting

DNS Time-to-Live (TTL) determines how long resolvers should cache your DNS records before refreshing. For websites with stable hosting infrastructure, we need to balance caching efficiency with operational flexibility.

example.com.    IN  A      3600    ; TTL of 3600 seconds (1 hour)
                192.0.2.1

For websites with no anticipated server changes:

  • Primary TTL: 86400 (24 hours) - Ideal balance between performance and flexibility
  • Minimum TTL: 3600 (1 hour) - Allow reasonable propagation if emergency changes are needed
  • SOA Refresh: 14400 (4 hours) - Secondary nameserver refresh interval

For a WordPress site on dedicated hosting:

; BIND zone file example
$TTL 86400
@       IN SOA  ns1.example.com. admin.example.com. (
                2023081501 ; serial
                14400      ; refresh
                3600       ; retry
                604800     ; expire
                3600       ; minimum TTL
                )
        IN NS   ns1.example.com.
        IN NS   ns2.example.com.
        IN A    192.0.2.1
www     IN CNAME example.com.

When using CDN services like Cloudflare:

; Cloudflare-optimized settings
$TTL 300  ; Lower TTL during initial setup
@    IN A    198.51.100.1

After propagation, increase to 86400:

$TTL 86400
@    IN A    198.51.100.1

Use dig to check current TTL values:

dig +nocmd +noall +answer +ttlid example.com
;; example.com.     86400   IN  A   192.0.2.1

For batch checking multiple records:

#!/bin/bash
for domain in example.com example.net example.org
do
    echo "Checking $domain:"
    dig +short +ttlid $domain
done

When dealing with DNS configurations, Time-To-Live (TTL) represents the duration (in seconds) that resolvers should cache your DNS records. For static websites with no anticipated server migration, we typically recommend longer TTL values to minimize DNS lookup overhead.

For stable hosting environments:

  • Production websites: 86400 (24 hours)
  • Staging environments: 3600 (1 hour)
  • Development servers: 300 (5 minutes)

Here's how to set TTL in common DNS configurations:

BIND zone file example:

example.com. 86400 IN SOA ns1.example.com. admin.example.com. (
  2023081501 ; serial
  3600       ; refresh
  900        ; retry
  1209600    ; expire
  86400 )    ; minimum TTL

www 86400 IN A 192.0.2.1

AWS Route 53 CLI example:

aws route53 change-resource-record-sets --hosted-zone-id Z1PA6795UKMFR9 \
--change-batch '{
  "Changes": [{
    "Action": "UPSERT",
    "ResourceRecordSet": {
      "Name": "www.example.com",
      "Type": "A",
      "TTL": 86400,
      "ResourceRecords": [{ "Value": "192.0.2.1" }]
    }
  }]
}'

Longer TTL values (24-48 hours) provide several advantages for static websites:

  • Reduced DNS query volume
  • Faster resolution for returning visitors
  • Lower DNS provider costs

Even with stable hosting, always prepare for emergencies. Before any forced migration:

  1. Reduce TTL to 300 seconds at least 48 hours prior
  2. Implement DNS monitoring to verify propagation
  3. Maintain old IPs during transition period

Use dig to verify TTL settings:

dig +nocmd +noall +answer +ttlid www.example.com