One way to look at DNS is as a large distributed key-value store.
- The key is made up of a domain name (e.g.
www.google.com
) and a record type (e.g. A
for "Address").
- The value is basically a string, whose meaning depends on the record type.
- Each key can have multiple values. Again, what this means depends on the record type.
When you want to make an HTTP request to serverfault.com
, the first thing you need to do is look up the A
record for that domain. So you query the DNS system to find out the value of that key, and get back an address, like 216.58.206.132
.
Then, you can make a connection to that address, on whatever port you like. This might be a port that the user has specified (e.g. 8080
for the URL http://www.google.com:8080/
) or a default that you expect to work (80
for HTTP, 443
for HTTPS).
In the case of A
records, and HTTP, there is no facility for DNS to tell you which port to use. Other record types (e.g. SRV
) may specify more information, including both an address and a port number, or even a bunch of configuration values you need to use to successfully talk to the server.
If you actually query the A
record for serverfault.com
, what you'll actually get back is a list of answers:
;; QUESTION SECTION:
;serverfault.com. IN A
;; ANSWER SECTION:
serverfault.com. 174 IN A 151.101.129.69
serverfault.com. 174 IN A 151.101.193.69
serverfault.com. 174 IN A 151.101.1.69
serverfault.com. 174 IN A 151.101.65.69
Now, we only need one address to connect to a web server, so we interpret this to mean "pick any of these addresses". Generally, everybody will pick the first one, so the DNS server will change the order each time you ask; that way, load will be spread between the different addresses. This is what is meant by "round-robin DNS".
An important disclaimer here is that DNS isn't actually like a database: when we query a DNS server, it can make up an answer however it likes, just like when we query a web server for a URL. For instance, if I look up the address for google.com
, I get this:
;; QUESTION SECTION:
;google.com. IN A
;; ANSWER SECTION:
google.com. 25 IN A 216.58.206.78
That doesn't mean that there's only one IP address that serves everyone who wants to connect to google.com
; instead, it means that the server in charge of that domain has decided that that's the IP address it's going to give me, which is different from the address it will give you.