If we set an A record and a CNAME record for the same sub-domain, how is the DNS lookup done?
For example,
A record:
example.com => xx.xx.xx.xx
www.example.com => xx.xx.xx.xx
CNAME record:
www. => example.com
When resolving www.example.com, does the DNS resolver directly look up the A record for the subdomain www and get the ip, or look up the CNAME for www to get the redirect domain(example.com), then look up the A record of example.com to get the ip in the end?
What are
CNAME
records?What a
CNAME
record does is to define the owner name (the name where the record is added) to be an alias for the name inCNAME
record value (the canonical name, as per the name of the record type).Note that this definition of being an alias is on the name level, completely disregarding record types.
One implication of the owner name being an alias is that it cannot also have other records, as that would be in direct conflict with it just being an alias for another name.
From the resolver perspective, if you query for
A
, the valid options for a response is anA
typed answer if the name was not an alias or aCNAME
typed answer if the name was an alias. Getting both for the same name is not an option (not one that is allowed by the standard and would have defined behavior, anyway).Then how can my DNS service provider do this?
Assuming that the claim that they allow you to add
CNAME
andA
records side by side is true, there is something non-obvious going on with their service.I would split it into two main options, without knowing more it's not really possible to know which is relevant in your case:
CNAME
records entered by the user, but rather resolve the relevant records from the canonical name from their end and serve those as if they were directly at the alias name (which is non-standard functionality, but some services have implemented this type of dynamic lookups to "flatten"CNAME
).