An Easy Primer on TTL (Time to Live)
What is TTL?
TTL, otherwise known as time to live, is a commonly used setting for defining how long a DNS record should remain in a DNS resolver's cache. Using TTL helps improve website speed since if the DNS lookup is already cached locally, it can be retrieved much faster than if a DNS server is required to complete the full lookup process.
Caching DNS records obviously is very beneficial in terms of improving speeds as well as the reducing the amount of load DNS resolvers around the globe experience. However, setting your TTL too high can cause issues. For instance, if a change to a DNS record must be made, you'll need to wait for the TTL to expire before the change will take effect. This is called the propagation period.
You can check a DNS record's TTL value by running a dig command in your CLI such as:
As we can see, the TTL for this particular CNAME record is set to 3600. The DNS TTL values in the screenshot above are displayed in seconds, therefore this record is set to expire in 1 hour. If you don't want to use your CLI or simply want to see more information, you can use our DNS Lookup tool.
How does TTL work?
Time to live works by having a web administrator define a set period of time for which a DNS record will be cached locally. When a new request for a domain is made, the resolver first checks its cache to see if it already contains the DNS record. If so, it will verify whether it has expired or not based on the TTL.
If the record has expired, the resolver goes through the process of asking the root server, TLD server, and so on. However, if the record hasn't expired yet, the resolver will return the cached DNS response to the browser making the DNS lookup process much more efficient.
TTL policy best practices
There are a variety of DNS record types. Therefore, you might be wondering if you should have different TTL values depending on which record type you're creating. The thing is, with TTLs values you want to strike a healthy balance between performance and propagation times.
Depending on the type of record you're creating and whether you know if several DNS changes will be required should determine your TTL value. That being said, here are a few best practices you should consider if you're new to setting TTL values:
- A and AAAA Records - Anywhere between 5 minutes to 1 hour
- CNAME Records - 24 hours
- TXT Records - 12 hours
- MX Records - 12 hours
CDNs and TTL
For a CDN, TTL basically refers to their
Expires let you define how long an asset should remain in cache before checking the original source of that asset (e.g. the origin server). For example, if an image is stored in a CDN's cache for 24 hours, all visitor requests will be delivered that asset from the CDN's cache. However, once the 24 hours expires, the CDN will need to make a request to the origin server and re-cache the asset.
The value you should use for your CDN's "TTL" may vary based on your needs. At KeyCDN, our default
Expires value is set at 1440 minutes (24 hours). If you want to further reduce the load on your origin server you can increase this value as so long that you aren't making frequent updates to your static content.
In the event that you're delivering dynamic content it may be benecifial to reduce this value. However, even with a reduced Expires value, this won't guarantee that you won't run into issues with caching dynamic content. In this case, you should consider implementing mechanisms that will clear the CDN's cache based on certain actions. To learn more about accelerating dynamic content with a CDN check out our dynamic site acceleration guide.
TTL is a great way to speed up the DNS lookup process of your site however the tradeoff between performance and propagation time needs to be considered when you're defining these values. If you have a specific use case or are experienced in defining your own TTL values then you're set. However, if you're still new at DNS configuration and aren't sure what to define, go with the baseline examples provided above. You can always tweak and play around with different settings as you progress.