News for hackers. Stuff that matters.
Re:What is a CDN? How do CDNs work? by filleokus on Wednesday February 08, 2023 @02:41AM (#34705288)
Does anyone know of more technically in-depth presentations / blog posts about the inner workings of CDN's? I'm thinking of stuff like:

- How do they distribute a huge load between multiple servers in a DC (some kind of level 2/3 load balancing I don't know of probably?)

- How is the cached data distributed between POP's?

- The whole TLS termination thing is probably an interesting aspect as well. The sensitive key material is needed on every node / POP, but you probably don't want all keys just lying around in clear text on disks? Some kind of distributed HSM-thingy?

- Just practically, how do you manage all these machines. Something like Ansible, or something more like Nomad/K8S?

Perhaps the fly.io people could write something about this (if they haven't already :-))

Re:What is a CDN? How do CDNs work? by animesh371g on Wednesday February 08, 2023 @03:12AM (#34705476)
As someone mentioned, internally they use high performance load balancer like nginx, which provides most of the things such as TLS termination out of the box. How the machines are managed and scaled is dependent on the CDN provider. For eg:- Akamai would be using an altogether different approach than the one employed by Azure CDN.
Re:What is a CDN? How do CDNs work? by filleokus on Wednesday February 08, 2023 @03:36AM (#34705635)
But there must be something before ≈ nginx as well right? Something something BGP to route it to the closest POP? And when you have traffic to a single pop that are multiples of what even the beefiest machine can handle, something before the machine must load balance it I guess?
Re:What is a CDN? How do CDNs work? by efitz on Wednesday February 08, 2023 @03:52AM (#34705771)
That’s done by DNS. The DNS server examines your DNS query and sends you back an IP address answer that’s “close” to the requesting IP address.

For example, if you are in country X, then even though there are dozens or hundreds of POP addresses for the name you requested, you’ll be answered with one for country X.

Re:What is a CDN? How do CDNs work? by nik736 on Wednesday February 08, 2023 @04:14AM (#34705886)
This can also be done with BGP. Anycast BGP for example is way better suited for this use-case.
Re:What is a CDN? How do CDNs work? by cryptonym on Wednesday February 08, 2023 @04:27AM (#34705962)
BGP is an alternative to DNS but it is not better suited, it depends.

DNS gives more control and precision to CDN provider than BGP. BGP might be good enough if you got few POP around the world but with the scale/distribution of Akamai, DNS is better suited.

Re:What is a CDN? How do CDNs work? by ThePhysicist on Wednesday February 08, 2023 @04:50AM (#34706143)
I think all major CDNs use some form of anycasting as it's pretty essential that you own your IP space. DNS-based load balancing can also be finicky as you'll have to deal with recursive resolvers, so a combination of anycasting and DNS-based load balancing probably works best.
Re:What is a CDN? How do CDNs work? by manigandham on Wednesday February 08, 2023 @05:35AM (#34706394)
How does DNS give you more control than BGP?

One is an actual routing system that tells routers where to send data, the other is a name translation system with multiple layers of caches outside of your control.

DNS is a layer above as BGP is still used to actually navigate to the listed IP, and any large CDN will own that IP space and announce their own routes anyway.

Re:What is a CDN? How do CDNs work? by capableweb on Wednesday February 08, 2023 @05:41AM (#34706427)
* * *
Re:What is a CDN? How do CDNs work? by cryptonym on Wednesday February 08, 2023 @08:28AM (#34707684)
Sure, Internet communications relies on both and you cannot swap one with another. This thread is about load balancing and both can be used for load balancing (Basically DNS resolving to Anycast IP vs DNS resolving to Unicast IPs).
Re:What is a CDN? How do CDNs work? by sophacles on Wednesday February 08, 2023 @11:53AM (#34710845)
What's the difference between an anycast IP and a unicast IP?

Answer: Nothing. They are just IPs.

Using a combination of Anycast and DNS is going to give the best control over steering http traffic. Particularly if you own a few prefixes and can do clever addressing tricks.

Re:What is a CDN? How do CDNs work? by daper on Wednesday February 08, 2023 @07:58AM (#34707380)
With BGP (especially anycast) you don't have direct influence where the requests lands. You can steer traffic by techniques like AS prepend, some priorities per BGP session (I'm not an expert in BGP) but eventually it's not CDN that decides where the request will be routed. It's decided by routers of the client's ISP and backbone networks, each taking decision themself (shorted AS path from his point of view + BGP policies set by network operators), not by you. You can't for example split traffic between DCs with specific proportion (like 30% here and 70% there). You can't split by something else than network properties like forward request to the region that has better chance to have this content in the cache.

With DNS and dynamic responses you are directing request to specific DC, even server, almost on every request. It may be dedicated for this traffic type (live stream different than static images etc). Your DNS server can take the hostname ("www.google.com") into consideration - BGP doesn't even know the hostname in the URL. If you wanted to do it with BGP you would need to place specific content to a dedicated /24 subnet and that is impossible considering how many IPv4 addresses are available.

BGP doesn't even consider network latency, current network load. CDN knows load on their machines, on their network link, where given content is placed. The bottleneck may be storage, network or CPU processing, different for different sites and content type. They need to direct traffic on request basis considering this and at least the hostname from the URL. That's why DNS is used first.

Re:What is a CDN? How do CDNs work? by wbl on Wednesday February 08, 2023 @11:55AM (#34710871)
Anycast doesn't let you adjust traffic flows gradually to shed load: you end up breaking connections. DNS is more flexible but isn't always honored.
Re:What is a CDN? How do CDNs work? by capableweb on Wednesday February 08, 2023 @05:39AM (#34706419)
> That’s done by DNS. The DNS server examines your DNS query and sends you back an IP address answer that’s “close” to the requesting IP address.

Is this really true? Last time I was dealing with load-balancing and DNS queries, DNS was simply "round-robbing" the replies, giving you back a random record basically of the ones replied. So if you have three A records with different IPs, each query will give you back one of them, but not depending on the location.

Maybe things have changed since I last dealt with it, but the DNS ecosystem doesn't tend to move very fast so I'm doubtful...

Re:What is a CDN? How do CDNs work? by pjc50 on Wednesday February 08, 2023 @06:15AM (#34706650)
The DNS server doesn't have to return the same responses to every query. It can geolocate the address making the request and use that to determine the respone.

https://easydns.com/features/geo-dns/

(Having said that, I tried my toy site on cloudflare free tier from the UK and it gave me San Francisco IPs, so presumably they only do this for large enough customers)

Re:What is a CDN? How do CDNs work? by capableweb on Wednesday February 08, 2023 @06:35AM (#34706777)
> It can geolocate the address making the request and use that to determine the respone

It can, but it tends to be a premium feature of specific DNS providers, not a global/by-default feature of DNS as efitz seems to be alluding to.

DNSimple supports it for example, but only on their "Professional" plan (and they call it "Regional Records") while others like Gandi don't support it at all.

Re:What is a CDN? How do CDNs work? by strken on Wednesday February 08, 2023 @06:58AM (#34706932)
If CDN providers are dealing with BGP you'd think they would also run their own DNS servers. I think you can do it with BIND views, from memory.
Re:What is a CDN? How do CDNs work? by jonathantf2 on Wednesday February 08, 2023 @07:30AM (#34707167)
I think that all of Cloudflare's IPs are GeoIP'd to their main office, like how Google's are. You can see the POP you're hitting here: https://<your url>/cdn-cgi/trace under colo. I think that Cloudflare use Anycast instead of geodns though.
Re:What is a CDN? How do CDNs work? by alfons_foobar on Wednesday February 08, 2023 @07:24AM (#34707104)
BGP Anycast (possibly in combination with DNS depending on geolocation).

Basically you announce the same IP address from multiple locations, and BGP chooses the best* Route to reach that address.

* Note that "best" does not nessecarily mean "fastest", not even "least hops" - BGP has many knobs where the routing decisions can be manipulated, but by default it choses the route where traffic has to traverse the smallest number of "Autonomous Systems" (roughly equivalent to "Organizational Entities").

Re:What is a CDN? How do CDNs work? by 9dev on Wednesday February 08, 2023 @04:53AM (#34706164)
Nginx handles TLS termination, but OP brought up a good point - how do you distribute private keys to thousands of nginx nodes without compromising them?
Re:What is a CDN? How do CDNs work? by capableweb on Wednesday February 08, 2023 @05:37AM (#34706410)
Central store (something like Vault) with access keys for accessing the certificates/keys, small cache on the instances themselves (maybe few hours), monitoring with alerts about how often the central store is accessed.
Re:What is a CDN? How do CDNs work? by ksimukka on Wednesday February 08, 2023 @03:56AM (#34705789)
Re:What is a CDN? How do CDNs work? by ly3xqhl8g9 on Wednesday February 08, 2023 @04:02AM (#34705825)
Out of curiosity I checked 2010, Gilbert Held, A Practical Guide to Content Delivery Networks, 2nd ed., CRC Press. However it offers more of a historical overview: it presents general information on things like TCP/IP and the Cache-Control header, but also the Akamai High Definition network and how it uses Flash. Things get old so fast in this area. Probably also outdated, but interesting nonetheless, is 2015, Abhishek Verma et al., Large-scale cluster management at Google with Borg [2] (check chapter 7. Related work for more references). John Wilkes seems to be involved in multiple articles showing some of the Google internals such as 2020, Borg: the Next Generation [3].

[1] https://www.routledge.com/A-Practical-Guide-to-Content-Deliv...

[2] https://research.google/pubs/pub43438 https://www.youtube.com/watch?v=0W49z8hVn0k

[3] https://research.google/pubs/pub49065 https://research.google/people/JohnWilkes

Re:What is a CDN? How do CDNs work? by animesh371g on Wednesday February 08, 2023 @04:31AM (#34705987)
thanks for sharing. this is really helpful.
Re:What is a CDN? How do CDNs work? by 867-5309 on Wednesday February 08, 2023 @04:33AM (#34706010)
around a couple of years ago it always bugged me that CDNs took 15mins to several hours to recognise changes, much like DNS. this made them useless for real-time websites

I wonder if things have changed since then

Re:What is a CDN? How do CDNs work? by animesh371g on Wednesday February 08, 2023 @04:36AM (#34706035)
It depends on the TTL set on the cached content in CDN. If the TTL is for several hours, CDN won't show the recent content. Given that the TTL is set appropriately for the content, CDN will behave correctly.
Re:What is a CDN? How do CDNs work? by capableweb on Wednesday February 08, 2023 @04:47AM (#34706122)
Much like DNS, it depends on the TTL. But yeah, if you're trying to do real-time stuff, it's best not to cache that with out-of-the-box stock CDN products, but rather have your own caching. Then for the rest of the endpoints/assets, you can use a default CDN.

Something some people seem to miss though, is to leverage ETag header properly, so the client and CDN can serve fresh content automatically when it exists, or serve the cached content otherwise. It's not that tricky, but somehow many seems to not even know about it.

Re:What is a CDN? How do CDNs work? by rfmoz on Wednesday February 08, 2023 @05:27AM (#34706339)
And the time that you spent waiting to see the logs. They aggreagate them via attached files in mails sended from the pops.
Re:What is a CDN? How do CDNs work? by manigandham on Wednesday February 08, 2023 @05:51AM (#34706480)
The problem comes down to how quickly configuration data can be pushed to the edge and modern CDNs like Cloudflare and Fastly respond to changes within seconds, or even milliseconds.
Re:What is a CDN? How do CDNs work? by cryptonym on Wednesday February 08, 2023 @08:38AM (#34707810)
Most of the time you change URLs rather than content but it may not apply to real-time scenarios.

Some CDN provides distributed key value stores where you can push data in seconds, you can also deploy your own code in minutes (for real time changes you would push data, not code).

Some providers expose cache purge APIs so you can clear cached data in seconds.

They also allow serving cached content while asynchronously revalidating with the Origin so following users get a fresh version.

With proper configuration/architecture they are a great way to scale your real-time website.

Re:What is a CDN? How do CDNs work? by cortesoft on Wednesday February 08, 2023 @09:50AM (#34708755)
It is changing. I work for a CDN on a team focused on speeding up config propagation. The modern expectation is global distribution of new configs in seconds.
Re:What is a CDN? How do CDNs work? by nosequel on Wednesday February 08, 2023 @10:32AM (#34709418)
One of the reasons Fastly got used by online newspapers was their fast purge. No newspaper wanted to use CDNs for breaking news if purging took 20 minutes to complete. It was one of the advantages Fastly had that allowed them to get customers from other CDNs. According to https://developer.fastly.com/learning/concepts/purging/ they do single-object purges in 150ms globally.
Re:What is a CDN? How do CDNs work? by thakoppno on Wednesday February 08, 2023 @11:07AM (#34710024)
> It was one of the advantages Fastly had that allowed them to get customers from other CDNs.

That’s a neat fact and perhaps an advantage usually reserved for new entrants into an entrenched market. Presumably Fastly benefited, at least in some part, from technological innovation that existing companies could not apply as easily.

Re:What is a CDN? How do CDNs work? by base698 on Wednesday February 08, 2023 @06:40AM (#34706818)
> Just practically, how do you manage all these machines. Something like Ansible, or something more like Nomad/K8S?

Most of the CDN providers predate all those things. Most are custom implementations with varying degrees of documentation and features.

> How is the cached data distributed between POP's?

I've seen two implementations and both do roughly the same thing. A request comes in and if it's not in cache it checks a regional POP preconfigured for that region like it was filling from origin. If it's not there the regional server gets it from origin. That fills the cache for both the regional one and the local. One implementation the regional POP was just config. Meaning the regional POP was just a standard POP that also served the requests from other POPs. In another it was something different only serving regional requests.

Re:What is a CDN? How do CDNs work? by donavanm on Wednesday February 08, 2023 @07:54AM (#34707341)
I spent a couple years in CDN & DNS land. Every meaningful CDN I know of is bespoke, but they all take very similar approaches to common problems.

There are good publications and papers from fastly and facebook circa 2012 to 2016 at events like usenix. Akamai had some seminal works in the early to mid 2000s. Oh, and google maglev and I think cloudlfare talk about IP fragmentation and similar problems where you need persistance but dont get a 5 tuple.

In short: DNS and edns client subnet tell the CDNs name servers the clients network location. Map the client subnet and network topology to a nearby POP. Return those addresses in the A record response.

If you dont get edns0 use a weighted result of the resolvers clients.

The A record IPs get the client to the best available POP considering load, latency, bandwidth, cost, customer domains, etc.

At the pop you play network games to get the tcp session to the first layer of CDN hosts. Ill call them “layer 1” or L1. You almost definitely DO NOT run a full proxy with traffic 100% in:out at this layer. Its simply too wasteful. Instead you can use some combination of ARP, ECMP, or “layer 3 switching” to get tcp flows distributed among the available L1 hosts. These hosts will almost always share those initial “virtual” IP addresses amongst them. The L1 hosts will terminate TCP/TLS and probably parse the HTTP request to determine the customer domain, customer cache rules, etc. The L1 howts will have a local “hot” cache of popular objects, lots of duplication between howts to distribute load. Probably a 50-80% hit rate here to immediately return the result to the client.

If the L1 is a miss they will use a consistent hash function to map the object (think URI plus any customer rules) to an L2 host. The L2 is a segmented cache, maximizing unique bytes stored for content in the “middle” of the popularity distribution. Implementation varies here; “L2” could map to a single L1 host which will have that object. It could map to a single L2 host in a dedicated fleet. It could map to another POP or a shared regional cache. Expect another 50-80% hit rate at this L2.

If your L2 doesnt have the object now you need to go back to the origin. You want to do connection pooling here to save the long distance tcp setup. Use more consistent hashing as necessary. Insert object in the L2 cache.

There are optimizations to make at every stage. Check on probabilistic structures, network encapsulation, cumulative distributions, and network mapping/latencies.

Re:What is a CDN? How do CDNs work? by donavanm on Wednesday February 08, 2023 @08:27AM (#34707667)
Re: TLS you dont require private keys on every L1. You can proxy session setup to smaller & more restricted fleet holding the certs. Pass back the negotiated session. IIRC there are public proposals/examples of this. Youll probably also want to use a session cache and cookies here as well.

On the other side you can extend the “depth” of your cache hierarchy with additional layers, like L3 etc. Think of doing things like having regional caches that your local/edge caches read from. Or store very large media objects in fewer locations with more density. Or centralize all origin requests through 1 or 2 specific locations to minimize the number of requests to the origin. I seem to recall that Akamai made a TON of money on this “net storage” layer historically.

Also forgot to mention previously TCP fast open and precomputed/cached TCP session values like cwnd. Youll want those to avoid slow start & bandwidth probing. Again, lots of optimizations at every step.

Re:What is a CDN? How do CDNs work? by cortesoft on Wednesday February 08, 2023 @09:48AM (#34708718)
I work for a CDN

To answer your questions:

The load is distributed by custom, special purpose load balancers. They route each request to the correct server based on information in the request. The reason requests are routed to a particular server instead of a random one is because we want requests for the same content to go to the same set of servers so that we don’t have to cache more copies of content than is necessary, allowing more total content to be cached in a pop. The server that actually serves the content will then use Direct Server Return to bypass the load balancer when returning the content to the client. Since the content returned is a lot larger than the request for the content, the load balancers are able to handle a lot more requests than if all the connection flow had to go through the load balancer.

Cached content can be distributed to each pop in a few ways. The simplest is just for each pop to individually request the content from origin. This means the origin will see one request for each piece of content per POP. The other main way is by having one POP be the gateway. Other pops use that gateway as their origin, so the content is cached once at the gateway and then served to all the other pops. This way, the customer origin only gets a single request per piece of content from the gateway pop.

TLS termination is a challenge because of what you say. The keys are encrypted for distribution, and then only decrypted and loaded into a servers memory if it gets requests for that domain name. It is complicated.

Managing the machines is a big part of the work a CDN does. I work on one of the teams that works on part of that management. Lots of custom software, asset management, config distribution, etc. I would write more but I need to get my daughter to school!