Two sides of the byte: Will DNS return multiple IPs when resolving an address?
Two sides of the byte: Will DNS return multiple IPs when resolving an address?
The answer is no. The focus of this question is actually DNS load balancing.
One domain name corresponds to multiple IPs
Theoretically speaking, a domain name can correspond to multiple IPs. In this case, when different users access the domain name, they will access different IP addresses.
To achieve the effect that one domain name corresponds to multiple IP addresses, you only need to add a resolution record on the DNS resolution operation platform, pointing the domain name of the website to the IP address of the server. Generally, one domain name corresponds to one IP address, and only one resolution record needs to be added.
For example, we want to point the domain name www.itmtx.cn to three IPs: 1.1.1.1 (Beijing Telecom), 2.2.2.2 (Shanghai Mobile), and 3.3.3.3 (Shenzhen Unicom).
Then we can configure three A records in the DNS server, which are:
- www.itmtx.cn IN A 114.100.20.201;
- www.itmtx.cn IN A 114.100.20.202;
- www.itmtx.cn IN A 114.100.20.203;
picture
If a Beijing user accesses the domain name itmtx.cn, a Shanghai user is also accessing it. Although both users access the same domain name, the IP addresses they access are different. Instead, they will be resolved according to the DNS pre-configured "resolution policy". The obtained IP address is returned to the corresponding visitor.
Each domain name resolution request will resolve a different IP address and return it to the visitor, thus forming a server cluster and achieving the effect of load balancing. Different users access different server IP addresses nearby, which greatly improves the access speed, and also reduces the access pressure of a single server. At the same time, since there are multiple alternative IPs, when a problem occurs in one of them, it can be switched through downtime to improve business availability.
DNS Resolution Policy
Common DNS resolution strategies include:
- Round Robin: The DNS server returns multiple IP addresses in order to realize the distribution of requests to different servers in turn.
- Weighted: Assign different weights to each IP address, and the server with higher weight is more likely to be selected.
- Response Time (Response Time): The DNS server can select the fastest server by measuring the response time of the server, and resolve the domain name to the corresponding IP address.
Different DNS resolution products may adopt different resolution strategies. Taking DNSPod resolution as an example, it adopts the weight + random strategy:
picture
Advantages and disadvantages of DNS-based load balancing
Load balancing based on DNS is a very simple and effective technical means, and it has the following advantages:
- The load balancing work is handed over to DNS, which saves the trouble of website management and maintenance of load balancing servers;
- The technical implementation is relatively flexible, the operation is simple, the cost is low, and it is suitable for most TCP/IP applications;
- For applications deployed on servers, application access on different machines can be achieved without modifying any code;
- Many DNS systems also support geographical location-based domain name resolution, which can resolve the domain name to the server address closest to the user's geographical location, speeding up user access.
But DNS-based load balancing also has some disadvantages:
- The current DNS system needs to go through multi-level resolution such as recursive server, top server, authoritative server and many caches, and there may be a resolution record cache in each link. If the server IP changes, even if the A record is modified, it will take effect only after the caches at all levels are invalid. During the period before the resolution takes effect, the user may access the server that has been replaced based on the cache record, resulting in access failure.
- In order for the local DNS server to synchronize the latest records on the authoritative server in time, the DNS cache refresh time is generally set relatively small, which will cause DNS to initiate resolution requests frequently, resulting in additional network problems.
- DNS itself does not have the ability to actively monitor server load or dynamically adjust weights. It cannot distinguish the performance and load differences between different servers, and cannot reflect the current operating status of the server. Therefore, some large websites always use DNS domain name resolution as the first Level load balancing means, and then provide a more advanced load balancing algorithm (such as least connection: send the request to the server with the least open connection) through the load balancing server to complete the final request.