When does DNS use TCP or UDP
There are quite is few services which may use both TCP and UDP protocol while communicating. The primary reason is based on type of request/response which needs to be furnished.Before we further drill into detail of protocol type used in DNS, lets have a quick run through TCP and UDP protocols.
TCP is a connection-oriented protocol where the devices in communication should establish a connection before they can start with data transmission. The same stands true for termination of connection . Notable is that TCP is reliable and it guarantees delivery of data to the destination device.
UDP is a connectionless protocol where there is no establishment of connection before data transmission. Further, there is no overhead related to opening, maintaining and terminating a connection. A key aspect of UDP is that delivery of data to the destination is not guaranteed.
While considering between UDP or TCP protocol for any application, another key aspect to note is that UDP packets are smaller in size and cannot be greater then 512 bytes. Hence, any application needs where data to be transferred is greater than 512 bytes will require TCP protocol.
Lets take scenario of UDP protocol requirement in DNS – A Client queries for a record from DNS server. Even if the DNS server response is lost or becomes corrupt, its not a major challenge since client can ask for it again. Considering such use case, it is rational to use UDP when communicating with DNS for translation of domain name.
So, when does DNS use TCP? In order to maintain a consistent DNS database between DNS Servers. Hence, a transfer of DNS records (Zone transfer) between Primary and secondary DNS Servers is required which uses TCP protocol. The requirement here is that TCP, due to its reliability makes sure zone data is consistent across DNS servers. When a client doesn’t receive a response from DNS, it re-transmits the query using TCP after 3-5 seconds of interval.
Considering the above scenarios, it becomes essential that DNS server operators/providers must provide DNS service over both UDP and TCP. The same understanding stands true for network operators.We may encounter operational challenges when TCP protocol is blocked for communication of DNS service.
Related – DNS Interview Questions