What is Time-To-Live (TTL)?
Time-to-live in networking refers to the time limit imposed on the data packet to be in-network before being discarded. It is an 8-bit binary value set in the header of Internet Protocol (IP) by the sending host. The purpose of a TTL is to prevent data packets from being circulated forever in the network. The maximum TTL value is 255. The value of TTL can be set from 1 to 255 by the administrators.
The usage of TTL in computing applications lies in the performance improvement and management of data caching. It also finds its use in Content Delivery Network (CDN) caching and Domain Name System (DNS) caching.
How time-to-live (TTL) Works?
The number of hops a packet travels before being discarded by a network is known as the time to live (TTL) or hop limit. The maximum range for packets is indicated by TTL values.
- The sending host sets the initial TTL value as an eight binary digit field in the packet header.
- The datagram’s TTL field is set by the sender and reduced by each router along the path to its destination.
- The router reduces the TTL value by at least one while forwarding IP packets.
- When the packet TTL value hits 0, the router discards it and sends an ICMP message back to the originating host.
- This system ensures that a packet moving via the network is dropped after a set amount of time, rather than looping indefinitely.
Thus, using the TTL value there is a restriction on the duration for which the data exists on the network. Furthermore, it also helps to find out the period of data for which it has been on the network and how long it will be on the network.
In the scenario below, Host A wishes to interact with Host B using a ping packet. Host A uses a TTL of 255 in the ping and transmits it to Router A, its gateway. When Router A notices that the packet is going for a layer 3 i.e. Network layer, it hops to Router B, reduces the TTL by 255 – 1 = 254, and delivers it to Router B. Router B and Router C decrement the TTL in the same way. Router B decrements TTL in a packet from 254 to 253 and Router C decrements the TTL from 253 to 252. The ping packet TTL is decreased to 252 when it reaches Host B.
Whenever TTL reaches the value of zero, TTL=0 then the packet is discarded by the router, and the Time Exceeded Error message is sent to the originating host.
Time-to-Live (TTL) in DNS:
DNS TTL refers to the time taken by DNS for caching a record. In other words, the duration for which a DNS record is to be kept or the time it takes for a DNS record to be returned from the cache is referred to as time-to-live. It is a numerical value set in a DNS record on the domain’s authoritative domain name server.
It specifies the number of seconds for which a cache server can provide the record’s cached value. When the set time has elapsed since the previous refresh, the caching server will contact the authoritative server to obtain the current and possibly updated value for the record.
Time-to-Live (TTL) in HTTP:
TTL is measured in seconds and is set by HTTP headers such as the Cache-Control header. If the value is set to “Cache-Control: max-age=60” then it implies refreshing a given resource every 60 seconds before the time to live is surpassed. The setting “max-age=0,” implies that the resource should not be cached at all.
Time to live field has a direct impact on page load time (cached data loads faster) and content freshness on your site (i.e., data cached for too long can become stale).
TTLs should be configured as follows to ensure that your visitors only see the most recent version of your website:
- For static content like images, documents, etc., a longer TTL value is set as they rarely get updated.
- For dynamic content such as HTML files, it is difficult to set TTL values. To exemplify, the comment section of a website frequently changes and its refresh time cannot be predicted at all if a user is permitted to modify the existing posts also then caching is not a recommended practice.
What is time-to-live (TTL) exceeded?
IP packets delivering web HTTP traffic over TCP (Transmission Control Protocol) that have traveled too many network hops are referred to as TTL exceeded. When this situation arises then each router Each router reduces the TTL field of IP packets sent over the network until it reaches 0. The router then drops the IP packet and sends an ICMP packet with a TTL exceeded error code to the sending host.
Application of time-to-live (TTL):
The TTL value is used in the network utilities such as ping, traceroute, and pathping.
- The ping command is used to test the reachability of the destination computer. In other words, it verifies whether the communication can take place between the source and destination computer or a networked device. It works by sending ICMP Echo request messages to the target computer and then waiting for the response. The execution of the ping command gives two important pieces of information: how many responses are returned and how long it takes for them to return.
- The tracert/traceroute command is used to trace the path between two devices. There are multiple routers in the path using which connection is established. So, it will provide the names or IP Addresses of routers existing in the path of two connecting devices.
- In Internet Protocol (IP) multicast, TTL may have control over the packet forwarding scope or range.
- 0 is restricted to the same host
- 1 is restricted to the same subnet
- 32 is restricted to the same site
- 64 is restricted to the same region
- 128 is restricted to the same continent
- 255 is unrestricted
- TTL is also employed in caching for Content Delivery Networks (CDNs). TTLs are used herein for specifying the duration of serving cached information until a new copy is downloaded from an origin server. A CDN can offer updated content without requests propagating back to the origin server if the time between origin server pulls is properly adjusted. This accumulative effect enables a CDN to efficiently offer information closer to a user while minimizing the amount of bandwidth required at the origin.
- TTL is also employed in caching for Domain Name Systems (DNS). TTL is a numerical value that refers to the duration used herein by the DNS Cache server for serving a DNS record before contacting the authoritative server to get a new copy.
TTL: Time Limit or Hop Limit?
In IPv6, it is known as hop limit and in IPv4, it is known as TTL. The Hop field is same as the TTL field in IPv4.
The hop count function is necessary for network operation. It prevents networks from collapsing as a result of packets looping indefinitely. Transport protocols like TCP use the time limit function to ensure that data is transferred reliably.
Even if the elapsed time was significantly less than a second, every router that handles a packet must reduce the TTL by at least one. In this perspective, Time-to-Live serves as a hop counter. So, it puts a limit on how far a datagram can propagate via the Internet.
When a packet is forwarded, the TTL must be reduced by at least one. It may decrease the TTL by one for each second it retains a packet for longer than one second. Time-to-Live is used as a time counter in this manner.
Common TTL Values:
Normally, the TTL value is 86400 seconds or 24 hours. MX and CNAME records, on the other hand, can have a longer TTL because they are expected to change infrequently. It is recommended that you set TTL to 1 hour if your service is vital (3600 seconds). Shorter TTLs can put a strain on an authoritative name server, but they can be advantageous when changing the address of key services like web servers or MX records. As a result, DNS administrators often reduce TTLs before moving services to avoid interruptions.