I love Cloudflare. I’ve always been a fan of their service and their generous free plan, which they’ve never backtracked from. They still offer unlimited bandwidth with very few limitations. Users have stories about serving terabytes of data per day without Cloudflare knocking on their door to upgrade.
我爱 Cloudflare。我一直很喜欢他们的服务和慷慨的免费计划,他们从未放弃过。他们仍然提供无限带宽,几乎没有任何限制。用户们都说他们每天可以处理数 TB 的数据, 而 Cloudflare 却没有催促他们升级。
We, too, use Cloudflare under the hood, which enables us to offer unlimited bandwidth for our users as well.
我们也在后台使用 Cloudflare,这使我们能够为用户提供无限带宽。
This might make people wonder: Why pay for a CDN when Cloudflare is free? When I started using Cloudflare, I thought that too.
这可能会让人疑惑: Cloudflare 既然免费,为什么还要花钱购买 CDN? 我刚开始使用 Cloudflare 的时候也这么想。
But when you compare it to a traditional CDN, Cloudflare does have a few shortcomings:
但与传统的 CDN 相比,Cloudflare 确实存在一些缺点:
1. Unreliable Caching Policies
1.不可靠的缓存策略#
With a traditional CDN, if you set cache-control header, you can be sure that your files will be cached on the edge according to the header.
使用传统的 CDN,如果您设置了缓存控制标头,您可以确保您的文件将根据标头缓存在边缘。
Cloudflare does respect the cache-control headers, but not always. I have served files with public, max-age=31536000, immutable header, and it still results in cache-miss, even when the request comes from the same location and edge.
Cloudflare 确实会遵守缓存控制标头,但并非总是如此。我曾使用 public, max-age=31536000, immutable 标头提供文件,但即使请求来自相同的位置和边缘,它仍然会导致缓存未命中。
Cloudflare is able to offer unlimited bandwidth because they have peering arrangements with large number of ISPs and hosting providers. They help providers save T1 bandwidth cost by caching the most frequently resources at their edge servers.
Cloudflare 之所以能够提供无限带宽,是因为他们与众多 ISP 和托管服务提供商建立了对等连接协议。他们通过在边缘服务器上缓存最常用的资源,帮助提供商节省 T1 带宽成本。
Naturally, they can’t cache every file indefinitely. Considering their free plan, the storage costs would be astronomical. I suspect that Cloudflare caching heavily depends on the frequency of the requests for the file. So, if a file hasn’t been requested in a while, it gets purged from the cache, irrespective of what the headers with which it was served with.
当然,他们不可能无限期地缓存所有文件。考虑到他们的免费方案,存储成本将是天文数字。我怀疑 Cloudflare 的缓存很大程度上取决于文件请求的频率。因此,如果某个文件有一段时间没有被请求,它就会从缓存中清除,无论它使用什么头文件来提供服务。
2. Unreliable Latency 2. 不可靠的延迟#
While latency from a conventional CDN is usually < 80ms, with Cloudflare, I have frequently seen it to be in 150-300ms. To be sure, Cloudflare network is super-capable, files from jsdelivr are frequently served in less than 30ms.
传统 CDN 的延迟通常为 80 毫秒,而 Cloudflare 的延迟则经常在 150-300 毫秒之间。诚然,Cloudflare 网络性能超强, jsdelivr 的文件传输延迟通常不到 30 毫秒。
High and inconsistent latency is just a product of how Cloudflare works as I covered it in the last section. Not all files will be replicated across all of their edge nodes, as it all depends on how frequently they are requested and from where.
正如我在上一节中介绍的那样,高且不一致的延迟只是 Cloudflare 工作方式的产物。并非所有文件都会在其所有边缘节点上复制,因为这完全取决于请求的频率和位置。
This is a cost which one has to keep in mind when using Cloudflare instead of a CDN. That said, the trade-off might be minor as web pages can still load consistently fast even with a high latency. For most websites, Cloudflare would work well-enough to serve most of their visitors.
这是使用 Cloudflare 而非 CDN 时必须考虑的成本。即便如此,这种权衡可能并不大,因为即使延迟较高,网页仍然可以持续快速加载。对于大多数网站来说,Cloudflare 足以满足大多数访客的需求。
3. Lack of Logging, Vary, Origin Shield, Custom Origin
3. 缺少日志记录、Vary、Origin Shield、自定义 Origin#
Most CDNs allow you to access request logs which can be super helpful to extract insights from Traffic. Cloudflare offers this, but only on their Business/Enterprise plans. Similarly, Vary Cache can be useful to server different content based on user’s location, device, etc. Cloudflare only supports this for Images, and only based on Accept headers.
大多数 CDN 都允许您访问请求日志,这对于从流量中提取洞察非常有帮助。Cloudflare 提供此功能,但仅限于其商业/企业计划。同样,Vary Cache 可以根据用户的位置、设备等提供不同的内容。Cloudflare 仅支持图像的此功能,并且仅基于 Accept 标头。
Another useful feature is Origin Shield, which helps reduce requests to your origin server by looking for the file from other edge locations. On traditional CDNs, this can significantly cut down traffic. I have seen requests for the same file drop to zero when enabling it. Cloudflare has something similar called Tiered Cache, but like many Cloudflare features, the way it works is complicated, and isn’t guaranteed same level of benefits as Origin Shield.
另一个实用功能是 Origin Shield ,它可以通过从其他边缘站点查找文件来减少对源服务器的请求。在传统的 CDN 上,这可以显著减少流量。我曾经见过启用此功能后,对同一文件的请求数降至零。Cloudflare 也有一个类似的功能,称为分层缓存 ,但与许多 Cloudflare 功能一样,它的工作原理很复杂,并且不能保证与 Origin Shield 具有相同的效益。
With a traditional CDN, you can pick a hostname (Origin) and the file will be fetched from there. So, you can run a CDN directly over, say, a S3 Cloud bucket. Cloudflare works at your website domain level, and doing this is something like that is not possible.
使用传统的 CDN,您可以选择一个主机名(Origin),然后文件将从该主机名获取。因此,您可以直接在 S3 Cloud bucket 上运行 CDN。Cloudflare 的工作方式是在您的网站域名级别,而这是不可能的。
Cloudflare does provide a few things that help with the problems that I have stated. For example, Cache Reserve can both reduce latency and eliminate requests to your origin. Argo Smart Routing can act towards reducing latency. Advanced features of Tiered Cache on Enterprise plans can introduce another layer of caching to respond to requests even faster.
Cloudflare 确实提供了一些功能来帮助解决我提到的问题。例如, 缓存预留功能可以降低延迟,并消除对源站的请求。Argo 智能路由功能可以降低延迟。企业计划中的分层缓存高级功能可以引入另一层缓存,从而更快地响应请求。
However, these all are paid services and don’t fully address the issues I mentioned.
然而,这些都是付费服务,并不能完全解决我提到的问题。
Conclusion 结论#
Cloudflare is a fantastic service with a generous free plan, but it’s a fairly complex product. CDN is just one of the things they do, and so, it’s not surprising that they have some quirks and limitations compared a traditional CDN.
Cloudflare 的服务很棒,免费方案也很慷慨,但它的产品相当复杂。CDN 只是他们业务之一,因此与传统 CDN 相比,它们有一些怪异之处和局限性也就不足为奇了。
For a casual user or a small website, it might not matter and free plan can go a long way, but if you need predictable, reliable caching, or features commonly found in other CDNs, you might need to pay for Cloudflare’s premium services or look elsewhere.
对于普通用户或小型网站来说,这可能无关紧要,免费计划可以发挥很大作用,但如果您需要可预测、可靠的缓存或其他 CDN 中常见的功能,您可能需要支付 Cloudflare 的高级服务费用或寻找其他地方。