5

So, let us say that we have set-up an A record and an AAAA record for @, and for www we have set-up a CNAME pointing to @.

When a new visitor visits example.com and the webserver sends a 301 permanent redirect to www.example.com, will that still require an extra DNS lookup after the CNAME, even though it already knows @'s location?

If so, why do many companies use the CNAME method over an additional A and AAAA for www?

Finally I would like to mention that I have searched through the whole internet for an answer to this question.

Xen
  • 461
  • 1
  • 4
  • 16

3 Answers3

4

If you have many subdomains pointing to one server, it's easier to manage, since you only need to change one A record when the servers IP changes, and the CNAMES will all point to the new IP - no need to setup every alias' IP separately.

Otherwise, it's pretty much your choice which method you use.

mulaz
  • 10,472
  • 1
  • 30
  • 37
4

To answer your initial question, when a CNAME is used to point www.@ in a zone, the initial CNAME lookup is performed, followed by a second lookup for the A record indicated by the CNAME. If the client has not accessed this before, typically this response will be cached and any further lookups of this A record within the TTL of the record looked up will be responded to from the cache.

Many companies use CNAME records where they have multiple subdomains of their main domain pointed to the same server. As above (response by mulaz), when the server IP or location changes, only one record needs to update, rather than a few hundred (which may be the case) saving both time, and making configuration easier.

Matthew Gall
  • 355
  • 1
  • 8
  • I understand the purpose of CNAME's, but I need to know what happens when it already knows @'s location (thus a new visitor visiting example.com who then gets 301 redirected to www.example.com). If it is not that smart to skip the second DNS lookup, then there is no reason for us to be using CNAME's, as that would only add a little extra delay. – Xen Jun 20 '12 at 11:34
  • 1
    If a client already knows the location of @ (i.e it has looked up the A record for @ before) and the copy of the record is still valid (i.e is still within it's TTL) then the response will not be passed to your DNS servers, and will be responded from localhost, which will be significantly faster. – Matthew Gall Jun 20 '12 at 12:11
  • Further to my comment as well, if the record has elapsed from the cache (i.e has expired) then a further lookup is performed. – Matthew Gall Jun 20 '12 at 12:36
2

In your case of www CNAMEd to @ of the same domain, extra lookup is never necessary. Authoritative server, when asked for a www, will send both - CNAME record, and A record(s) of a CNAME target (@ in your case). The same true for any CNAME which points to another hostname within the same zone.

Extra lookup will be necessary only if CNAME points to another domain.

CNAME used because it's easier to manage - when address changes, it needs to be changed only only for one hostname.

IMHO, technically, it's not strictly correct to point CNAMEs to @, as @ usually contains additional records, such as MX for instance. If you CNAME www to @, you mean A record, but all those other records will appear as existing for www as well. Thus someone may think that he can send an email to admin@www.example.com, for instance.

Sandman4
  • 4,045
  • 2
  • 20
  • 27