|
| 1 | +# Content Delivery Networks |
| 2 | + |
| 3 | +Users of the Rust programming language interact with the infrastructure of the |
| 4 | +project in various different ways. They access the project's website and |
| 5 | +documentation, query the crates index, and download Rust releases and crates. |
| 6 | +These resources are hosted by the Rust project and served through a [Content |
| 7 | +Delivery Network] (CDN). |
| 8 | + |
| 9 | +This document outlines why we use CDNs, for what, and how we have set them up. |
| 10 | + |
| 11 | +## Objectives |
| 12 | + |
| 13 | +We have three goals for our use of CDNs in our infrastructure: |
| 14 | + |
| 15 | +1. Reduce costs of outbound traffic through cheaper pricing and caching |
| 16 | +2. Reduce load on origin servers to save compute resources |
| 17 | +3. Provide a way to rewrite legacy URLs for some resources |
| 18 | + |
| 19 | +### Reducing Costs |
| 20 | + |
| 21 | +As an open source project, we have to be very mindful of our infrastructure |
| 22 | +costs. Outbound traffic is by far one of the most expensive items on our monthly |
| 23 | +bills, and one that will continue to increase as Rust gets more popular. |
| 24 | + |
| 25 | +Cloud providers typically charge different rates for outbound traffic based on |
| 26 | +the service. For example, serving data straight from [Amazon S3] is more |
| 27 | +expensive than serving the same data through an [Amazon CloudFront] |
| 28 | +distribution. This is why we now use a CDN by default, even for services that |
| 29 | +can't make use of other features of a CDN such as caching. |
| 30 | + |
| 31 | +## Infrastructure |
| 32 | + |
| 33 | +Most of the project's resources are hosted on [AWS]. Static content is stored in |
| 34 | +[Amazon S3], while dynamic content is loaded from a server. Both types of |
| 35 | +content are served through [Amazon CloudFront], the [Content Delivery Network] |
| 36 | +of AWS. |
| 37 | + |
| 38 | +When a user access a resource, e.g. they are trying to download a crate, they |
| 39 | +will access the resource through the CDN. Different _distributions_ map domain |
| 40 | +names to a configuration and a backend (called the _origin_). For example, |
| 41 | +downloading a crate from `static.crates.io` goes through a _distribution_ that |
| 42 | +fetches the crate from an S3 bucket and then caches it for future requests. |
| 43 | + |
| 44 | +```text |
| 45 | + ┌──► S3 (static content) |
| 46 | + │ |
| 47 | +User ───────► CloudFront ────┤ |
| 48 | + │ |
| 49 | + └──► Server (dynamic content) |
| 50 | +``` |
| 51 | + |
| 52 | +## Distributions |
| 53 | + |
| 54 | +There are many distributions, all of which are configured in the |
| 55 | +[rust-lang/simpleinfra] repository. However, their usage is very unevenly |
| 56 | +distributed. The following distributions are the most important ones for the |
| 57 | +project, both in terms of traffic and criticality for the ecosystem. |
| 58 | + |
| 59 | +### Rust Releases |
| 60 | + |
| 61 | +Whenever a user installs or updates Rust, pre-compiled binaries are downloaded |
| 62 | +from `static.rust-lang.org`. The same is true when Rust is installed in a CI/CD |
| 63 | +pipeline, which is why this distribution has by far the highest traffic volume. |
| 64 | + |
| 65 | +Rust binaries are static and are stored in [Amazon S3], from where they are |
| 66 | +served by the CloudFront distribution. |
| 67 | + |
| 68 | +The distribution for `static.rust-lang.org` has a custom router that runs in a |
| 69 | +[AWS Lambda] function. The router provides a way to list files for a release and |
| 70 | +rewrites the legacy URL for `rustup.sh`. |
| 71 | + |
| 72 | +The cache for Rust releases is invalidated nightly. |
| 73 | + |
| 74 | +### Crates |
| 75 | + |
| 76 | +Similar to Rust releases, crates are served from as static content from |
| 77 | +`static.crates.io`. While still being the second-largest distribution in our |
| 78 | +infrastructure, it is much smaller than the releases. |
| 79 | + |
| 80 | +Crates are static and stored in [Amazon S3], and served through a CloudFront |
| 81 | +distribution. |
| 82 | + |
| 83 | +[amazon cloudfront]: https://aws.amazon.com/cloudfront/ |
| 84 | +[amazon s3]: https://aws.amazon.com/s3/ |
| 85 | +[aws]: https://aws.amazon.com/ |
| 86 | +[aws lambda]: https://aws.amazon.com/lambda/ |
| 87 | +[content delivery network]: https://en.wikipedia.org/wiki/Content_delivery_network |
| 88 | +[rust-lang/simpleinfra]: https://github.com/rust-lang/simpleinfra |
0 commit comments