You have some misconceptions about what a content delivery network (CDN) is:
- Yes a CDN does specialize in caching
- A CDN does not typically do any load balancing
- A CDN maintains a network of "edge nodes" -- caching proxy servers distributed globally to be near users.
Here is a network diagram showing multiple web servers being used with a load balancer and a CDN:
To get this to work, a CDN plays tricks with DNS. Rather than just returning an IP address for the domain name, it returns different IP addresses to different users. It does so in such a way as to try to make sure each request from a user goes to the closest edge node.
The speed increase comes when a user requests a cached document from an edge node. Then:
- The edge node is nearby and the network trip to request the document is reduced.
- The edge node returns the document from cache which is often much faster than having the web servers generate the document.
To be able route different requests to different edge nodes, the CDN needs to be in control of DNS. Using an ANAME won't work because that would return the same IP address to every user.
Technically, the CDN is a step removed from DNS requests. And end user makes a DNS request to their ISP's DNS server which then makes a request to the authoritative DNS server for that domain. So when an end user gets an IP address to use, it is usually the one closest to their ISP's DNS server and not always the one closest to them. See this on ServerFault.
Here is an in-depth article that has quite a bit about different ways that CDNs work: https://www.sciencedirect.com/science/article/pii/S2352864817300731 See section 2.4 which goes into DNS based redirecting which is the style that I've described in this answer.
The article notes that CNAME DNS records can be used. However, CNAMEs have practical limitations. They can't be used for the bare domain name, only for subdomains. If you want to run your website without
www, a CNAME is not possible.