How does the TTL value affect the parsing speed? What's the most reasonable way to set it?
You change the A record of a domain from an old IP address to a new IP address in your DNS console and wait for it to take effect. Ten minutes later, some users are already accessing the new server, while others are still accessing the old one—the older version of the website. This is the role of the TTL (Time To Live) value behind the scenes.
TTL stands for Time To Live. It's a fundamental field in DNS records, measured in seconds. It determines how long a DNS record can survive after being cached by recursive DNS servers. When the TTL expires, the recursive DNS discards the cached record and retrieves the latest value from the authoritative server the next time someone queries it.
This mechanism is designed to reduce the load on authoritative servers and improve DNS query efficiency. However, if you want to change a DNS record, the TTL value directly affects the speed at which it takes effect. Simply put: the shorter the TTL, the faster it takes effect; the longer the TTL, the faster the DNS query.
How TTL Works
To understand the impact of TTL on the speed of change, you first need to understand what a DNS record goes through from the authoritative server to the user's browser.
You have a website hosted on a server. You've added an A record with your authoritative DNS provider: www.example.com pointing to 192.0.2.1.
Let's say a user in China is using the public DNS 114.114.114.114. When they first access www.example.com, 114's recursive DNS will retrieve this A record from your authoritative server and return it to the user, while simultaneously storing this record in its local cache. TTL is the lifespan of this cache.
If your TTL is set to 600 seconds, then within the next 10 minutes, if a second or third user accesses the same domain, 114's recursive DNS will not query your authoritative server again, but will directly retrieve 192.0.2.1 from its cache and send it to the user. After 10 minutes, the cache expires, and the next query will retrieve the latest record from your authoritative server.
The essence of the impact of changeover speed lies here: you modify the DNS record, but the recursive DNS servers won't immediately fetch the new record; they will wait until their respective caches expire before updating. There are tens of thousands of recursive DNS nodes globally, and their cache expiration times vary. Therefore, before the TTL expires, some users get the new IP address, while others still have the old one—this is the transitional state of "resolver in progress."
TTL Value Range and Common Scenarios
60 seconds - 300 seconds (Extremely short TTL)
This range is typically used for scenarios requiring frequent switching.
Advantages: Extremely fast resolution. After modifying the record, most recursive DNS caches globally will expire and fetch the new record within minutes.
Disadvantages: Significantly increased recursive DNS query frequency, leading to a noticeable increase in the load on authoritative servers. Each query requires a lookup to the origin server, resulting in slightly slower DNS response times.
Suitable Scenarios: Failover drills, canary releases, CDN dynamic scheduling, or scenarios where you plan to change your server IP address in the near future.
600-3600 seconds (Short TTL)
This is the default configuration range for most websites, ranging from 10 minutes to 1 hour.
Advantages: Achieves a relatively balanced state between activation speed and caching efficiency. After changing the DNS, most users can switch within an hour. During normal access, the cache hit rate is high, and there are no frequent origin server requests.
Disadvantages: If an emergency failure requires switching IPs, one hour is still too long.
Suitable Scenarios: Ordinary corporate websites, blogs, and e-commerce website front-ends. This range is sufficient to cover the daily use of most production environments.
86400 seconds or longer (Long TTL)
A TTL of one day or even one week, belonging to the old-school operations and maintenance style.
Advantages: Extremely high cache hit rate, almost no origin server requests during DNS queries, extremely light load on authoritative servers, and almost no DNS response delay.
Disadvantages: Changing the DNS takes 24 hours or even longer to fully take effect. If the server IP changes, you can only wait; there is no way to speed it up.
Suitable Scenarios: This is strongly discouraged for use on a website's main domain. Consider a long TTL if you have some largely unchanging public services, such as an internal mail server address or a CDN domain for a static resource.
What are the costs of a too-short TTL?
Many people set their TTL to 30 seconds or even shorter in pursuit of "instant effect for DNS changes." While this seems flexible, the costs are real.
The query pressure on the authoritative server will increase significantly. A 30-second TTL means each recursive DNS server will query your authoritative server every 30 seconds. If users worldwide are distributed across hundreds of recursive DNS servers, the number of queries your authoritative server has to handle per second will skyrocket. If your authoritative DNS is billed per query (e.g., AWS Route 53), the bill will be painfully high.
DNS resolution time will increase slightly. When the cache is hit, the resolution time is close to 0 milliseconds (the recursive DNS directly returns the result from memory). When the TTL (Time To Live) is short, frequent cache invalidation forces users to complete a full recursive query chain each time, increasing resolution time from milliseconds to tens or even hundreds of milliseconds. While this latency may be imperceptible to individual users, the cumulative impact is significant if page views (PV) are high.
In extreme cases, it can be mistaken for a DDoS attack. Some DNS providers limit QPS (Queries Per Second) for a single domain. Setting the TTL to an extremely low value can cause a surge in queries to authoritative servers, potentially triggering rate limiting policies and causing resolution failures for users in certain regions.
Adjust the TTL before changing DNS records.
This is a very practical operation and maintenance tip: lower the TTL one or two days before planning to change a server IP.
Suppose your business website normally has a TTL of 3600 seconds (1 hour). Now, the server performance is insufficient, and you are preparing to migrate to a new machine, changing the IP from A to B.
If you directly change the DNS records, within the next hour, some users will still be using the old IP A, while others will have already received the new IP B, causing service interruptions and even data inconsistencies.
The correct procedure is a two-step process:
Step 1: 48 hours in advance, change the TTL from 3600 seconds to 300 seconds. This operation has an immediate effect; after the change, global recursive DNS will gradually update its cache with shorter intervals.
Step 2: Wait at least the original TTL duration (i.e., after 48 hours) to confirm that all recursive DNS are caching according to the new 300-second TTL. Then, officially change the A record to the new IP. Since the TTL has been reduced to 300 seconds, most users worldwide will receive the new IP within 5-10 minutes after the change.
Step 3: After confirming that the service is running stably on the new IP for 24 hours, then adjust the TTL back to 3600 seconds to restore normal operation.
The core logic of this process is that the speed of effectiveness depends on the current TTL value, not the sudden operation when you modify the record. Changing the TTL is itself a DNS change and will have an effectiveness delay. Therefore, lowering the TTL in advance allows global DNS nodes to be "prepared" to refresh their caches more quickly, enabling a fast and smooth switch when the IP address is changed.
TTL Setting Recommendations for Different Record Types
Different types of DNS records have significantly different timeliness requirements, making a one-size-fits-all approach inappropriate.
A/AAAA Records (Core Business Domains): Recommended 600-1800 seconds. This ensures efficient daily caching while providing relatively fast activation when a switch is needed. Unless your business has extremely high requirements for failover speed (such as a transaction system), it doesn't need to be set below 300 seconds.
CNAME Records (Alias Records): The target domain pointed to by a CNAME record also has resolution latency, which, combined, prolongs the overall activation time. It is recommended that the CNAME TTL be no less than 600 seconds to avoid frequent queries putting pressure on upstream domains.
MX Records (Mail Exchange Records): Mail systems have extremely high stability requirements. If the TTL is too short, the mail server may frequently query the DNS, leading to mail queue backlogs. It is recommended to set the TTL of MX records to 3600 seconds or more, even 4 hours is not excessive. Unless you know for certain that your mail server will be migrating its IP address soon, do not change the TTL of your MX records.
TXT records (verification, SPF, DKIM): These records are typically used for domain ownership verification or anti-spam configuration. Verification service providers generally only query once when adding a record and do not rely heavily on the TTL. Setting it to 3600 seconds or even longer is fine.
NS records (authority server records): It is not recommended that the TTL of NS records be lower than 86400 seconds. Because changes to NS records involve the transfer of overall domain name resolution rights, if the TTL is too short, it can easily lead to unstable resolution and even NXDOMAIN errors.
Several common misconceptions:
"The shorter the TTL, the faster the website access": Wrong. TTL determines the speed of DNS resolution, not the speed of website loading. DNS resolution only accounts for a small part of the entire page loading process. A longer TTL results in a higher cache hit rate and faster DNS resolution; a shorter TTL may actually be slower because of frequent origin server lookups. "Manually refreshing after changing the DNS records will take effect immediately": No. When you refresh your browser (F5), you are refreshing the page, not the DNS cache. The recursive DNS cache is not under your control and is not affected by your local operations. You can force a refresh of the local DNS cache using `ipconfig /flushdns` (Windows), but this is meaningless to users; you cannot control all the computers of your website's visitors.
"Setting TTL to 0 will take effect immediately": Theoretically, the RFC allows TTL=0, meaning a 0-second cache time and a return to the origin server on every query. However, in practice, many recursive DNS servers ignore the 0 value and force the server to use its minimum TTL limit. Furthermore, TTL=0 will cause a sharp increase in the load on authoritative servers, which is strongly discouraged.
Finally, the core principle to summarize: The reasonable value of TTL depends on your expected frequency of DNS changes. If you frequently need to change your IP address, shorten the TTL; if you won't change your DNS records for several years, a longer TTL is fine. There is no absolutely correct value, only what suits your business rhythm.
CN
EN