DNS Time-To-Live (TTL) values determine how long recursive resolvers cache query results. Getting TTL values right is one of the most impactful yet overlooked aspects of DNS infrastructure optimization. Too short, and you generate excessive upstream queries; too long, and clients receive stale data when records change.
This guide covers DNS TTL optimization strategies across three popular self-hosted recursive resolvers: Unbound, PowerDNS Recursor, and Knot Resolver.
Understanding DNS TTL
When an authoritative nameserver returns a DNS response, it includes a TTL value (in seconds) specifying how long the record may be cached. Recursive resolvers honor this TTL by storing the response and serving cached copies until it expires.
TTL Trade-Offs
| TTL Value | Cached Duration | Upstream Queries | Record Freshness |
|---|---|---|---|
| 30s | 30 seconds | Very high | Excellent |
| 300s (5 min) | 5 minutes | High | Good |
| 3600s (1 hr) | 1 hour | Moderate | Acceptable |
| 86400s (24 hr) | 1 day | Low | Poor for dynamic records |
Common TTL Recommendations
- A/AAAA records: 300-3600 seconds (5 min to 1 hour)
- MX records: 3600-86400 seconds (1 to 24 hours)
- NS records: 86400 seconds (24 hours)
- TXT/SPF records: 3600 seconds (1 hour)
- CNAME records: Match the TTL of the target record
- SRV records: 300-3600 seconds
Unbound — DNSSEC-Validating Recursive Resolver
Unbound is a validating, recursive, and caching DNS resolver developed by NLnet Labs. It is widely deployed as a recursive resolver and supports extensive TTL customization.
Docker Compose Deployment
| |
TTL Optimization Configuration
| |
The serve-expired feature is particularly powerful: when a cached record’s TTL expires and the upstream nameserver is unreachable, Unbound continues serving the stale record for up to serve-expired-ttl seconds while simultaneously attempting to refresh it.
PowerDNS Recursor
PowerDNS Recursor is a high-performance recursive DNS resolver designed for ISPs and large-scale deployments. It features a Lua scripting engine for advanced query processing and TTL manipulation.
Docker Compose Deployment
| |
TTL Optimization Configuration
| |
Lua TTL override script (ttl-rules.lua):
| |
Knot Resolver
Knot Resolver is a modern caching DNS resolver developed by CZ.NIC. It features a modular architecture with Lua scripting, DNS-over-TLS, and DNS-over-HTTPS support built in.
Docker Compose Deployment
| |
TTL Optimization Configuration
| |
Comparison Table
| Feature | Unbound | PowerDNS Recursor | Knot Resolver |
|---|---|---|---|
| TTL min/max override | Yes | Yes | Yes (via policy) |
| Serve stale/expired | Yes | Yes | Yes |
| Prefetch | Yes | Partial (via Lua) | Yes |
| Lua scripting | No | Yes | Yes |
| Per-query TTL rules | No | Yes (Lua) | Yes (policy) |
| DNSSEC validation | Yes | Yes | Yes |
| DNS-over-TLS | Yes | Yes (4.7+) | Yes |
| DNS-over-HTTPS | No (needs proxy) | No (needs proxy) | Yes |
| Cache size config | msg-cache, rrset-cache | max-cache-entries | cache.size |
| Prometheus metrics | Via exporter | Built-in | Built-in |
| GitHub Stars | 4,523 | N/A (powerdns.com) | 437 |
| Best For | DNSSEC-first deployments | Large-scale / ISP | Modern features + DoH |
Advanced TTL Optimization Strategies
Aggressive TTL Capping
When authoritative servers set extremely low TTLs (e.g., 1 second for CDN failover), your resolver generates thousands of upstream queries. Implementing a minimum TTL floor (60-300 seconds) dramatically reduces upstream load while maintaining acceptable freshness.
| |
Selective TTL Override
Different record types have different freshness requirements. Use PowerDNS Lua or Knot Resolver policies to apply TTL rules selectively:
| |
Stale Cache Serving
All three resolvers support serving expired records when upstream servers are unreachable. This is critical for resilience during network partitions or authoritative server outages:
- Unbound:
serve-expired: yeswith configurable TTL reset - PowerDNS Recursor:
serve-stale-extensions=1 - Knot Resolver:
policy.add(policy.serve_stale(86400))
For a comprehensive DNS security hardening guide that complements TTL tuning, see our DNS cache hardening comparison. Organizations managing DNS firewall policies can also benefit from our DNS firewall and RPZ solutions guide.
Why Optimize DNS TTL Settings?
Reduced upstream query load — By enforcing reasonable minimum TTLs, you can reduce upstream DNS queries by 40-80%, especially for domains with very low TTL values. This lowers bandwidth costs and reduces dependency on upstream resolver availability.
Improved response latency — Cached responses return in microseconds, while uncached queries require network round trips to authoritative servers. Aggressive caching with prefetch ensures that popular records are always available locally.
Resilience during outages — Stale cache serving ensures that DNS resolution continues even when authoritative nameservers are unreachable. Users experience no interruption while the resolver works to refresh expired records in the background.
Better control over failover behavior — DNS-based failover mechanisms rely on low TTLs to redirect traffic quickly. By setting appropriate TTL bounds on your recursive resolver, you can balance between fast failover and reasonable caching performance.
Cost optimization — For organizations using paid DNS services, reducing upstream query volume directly reduces costs. Many DNS providers charge per query, making TTL optimization a direct cost-saving measure.
FAQ
What is the ideal DNS TTL value?
There is no universal ideal TTL. For static infrastructure (mail servers, nameservers), use 86400 seconds (24 hours). For dynamic infrastructure (web servers, load balancers), use 300-3600 seconds (5-60 minutes). The key is matching the TTL to your infrastructure’s change frequency.
What happens if I set cache-min-ttl higher than the authoritative TTL?
Your resolver will honor the higher minimum TTL, serving cached results even after the authoritative TTL has expired. This means clients may receive slightly stale data but benefit from faster responses and reduced upstream load. The trade-off is between freshness and performance.
Does serving stale DNS records cause security issues?
Serving stale records is generally safe for most use cases. The records were previously validated (if DNSSEC is enabled) and were considered authoritative at the time of caching. However, for time-sensitive records like CAA or TLSA, consider using shorter stale TTLs or disabling stale serving for specific zones.
How do I monitor DNS cache hit rates?
Unbound: Use unbound-control stats to view cache hit rates. PowerDNS Recursor: Query the built-in HTTP metrics endpoint or use rec_control get-all. Knot Resolver: Enable metrics and query the HTTP endpoint. Look for cache hit rates above 70% as a healthy baseline.
Can I override TTL for specific domains only?
PowerDNS Recursor and Knot Resolver support domain-specific TTL overrides via Lua scripting and policy rules respectively. Unbound does not support per-domain TTL overrides natively — you would need to use local-zone configurations or deploy multiple Unbound instances with different settings.
How does prefetching affect cache performance?
Prefetching proactively refreshes records before their TTL expires, ensuring that popular records are always fresh in the cache. This increases background query volume slightly (typically 5-10% more queries) but eliminates cache miss latency for frequently accessed records. Enable prefetching for resolvers serving many clients.
Should I disable DNS caching entirely for development environments?
For development and testing, you may want shorter TTLs to see DNS changes immediately. Rather than disabling caching entirely, set cache-min-ttl: 0 and cache-max-ttl: 60 to ensure records expire quickly while still providing some caching benefit.