Search code examples
cachingamazon-cloudfront

Expected caching behavior when 2 hits to same path on CloudFront


So lets say I have a CloudFront distribution and I call /path1 then /path1 again soon after. The flow will be

  • 1st request is a CloudFront miss and goes to server
  • 2nd request is a CloudFront hit

But what if theres 2 parallel hits to CloudFront. At the same time such that when the 2nd request reaches CloudFront before the 1st request finishes such that CloudFront does not have the cached response yet. Will it wait for the 1st request to finish and return that? Or both requests will hit the server?


When looking at my NGINX logs, it looks like when parallel calls reaches server, they all reach server. Is there any way to avoid that? Like if a rogue client makes too many request for same path, I was hopping that CloudFront can only make 1 request to server and return that same response. Is that possible?


Solution

  • If the first request is still serving to the client, CloudFront hasn't cached it yet , one the first request is complete which means CloudFront received complete data from Origin and served it to the client, it keeps the response in the cache and served further requests.

    If you're making second request before the first request is completed, it'll a MISS from cloudfront and you'll see both requests reached to CloudFront.

    However, if you're talking about burst of parallel requests , CloudFront has a way to handle them:

    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."