当 2 命中 CloudFront 上的相同路径时的预期缓存行为

Expected caching behavior when 2 hits to same path on CloudFront

所以假设我有一个 CloudFront 分配,我很快又调用了 /path1 然后又调用了 /path1。流量将为

但是,如果对 CloudFront 有 2 个并行点击怎么办。同时,当第二个请求在第一个请求完成之前到达 CloudFront 时,CloudFront 还没有缓存的响应。它会等待第一个请求完成然后 return 吗?或者两个请求都会到达服务器?


查看我的 NGINX 日志时,看起来并行调用到达服务器时,它们都到达服务器。有什么办法可以避免这种情况吗?就像流氓客户端对同一路径发出太多请求一样,我希望 CloudFront 只能向服务器发出 1 个请求并且 return 相同的响应。那可能吗?

如果第一个请求仍在为客户端提供服务,则 CloudFront 尚未对其进行缓存,第一个请求已完成,这意味着 CloudFront 从 Origin 接收到完整数据并将其提供给客户端,它会将响应保存在缓存并为进一步的请求提供服务。

如果您在第一个请求完成之前发出第二个请求,它将是来自云端的 MISS,您会看到两个请求都到达了 CloudFront。

但是,如果您谈论的是并行请求的爆发,CloudFront 有办法处理它们:

Simultaneous Requests for the Same Object

"When a CloudFront edge location receives a request for an object and either the object isn't currently in the cache or the object has expired, CloudFront immediately sends the request to your origin. If there's a traffic spike—if additional requests for the same object arrive at the edge location before your origin responds to the first request—CloudFront pauses briefly before forwarding additional requests for the object to your origin. Typically, the response to the first request will arrive at the CloudFront edge location before the response to subsequent requests. This brief pause helps to reduce unnecessary load on your origin server. If additional requests are not identical because, for example, you configured CloudFront to cache based on request headers or cookies, CloudFront forwards all of the unique requests to your origin."