Question: How do I reduce the latency of slow responses from CloudFront?

I'm seeing high latency when objects or images are downloaded from Amazon CloudFront. Requests that receive the "X-Cache:Miss from cloudfront" response are slower to load than requests that receive the "X-Cache:Hit from cloudfront" response. Why is this happening? How can I fix this?ResolutionCloudFront returns "X-Cache:Miss from cloudfront" when the request is sent to the origin. CloudFront returns "X-Cache:Hit from cloudfront" when requests are served from the closest edge location. "Miss" requests might be slower to load because of the additional step of forwarding to the origin.To avoid latency from forwarding requests to the origin, check the following to be sure that requests can be served from CloudFront edge locations:Don't forward all headers, all cookies, or all query strings, because this directs CloudFront to pass through requests instead of caching them.Verify that you have separate cache behaviors for static content (for example, CSS files) that rarely change and for dynamic content (for example, JavaScript files) that change often. For static content, avoid caching based on cookies, query strings, or headers that aren't necessary for the origin to serve the content.Note: For web distributions, CloudFront by default doesn't consider cookies when caching objects in edge locations. If your origin returns two objects and they differ only by the values in the Set-Cookie header, CloudFront caches only one version of the object.Extend the Minimum TTL, Maximum TTL, or Default TTL for path patterns that need more time in the cache before CloudFront queries the origin.If your origin uses Cache-Control headers, verify that the directives are consistent with the Minimum TTL, Maximum TTL, or Default TTL set on the distribution.If your origin uses the Expires header, verify that the header allows CloudFront to cache responses as needed.Verify that your origin server sets valid, accurate values for the Date and Last-Modified header fields.Limit the use of invalidations on objects. Run invalidations only when necessary.Check how frequently objects are requested. If an object is not requested often, CloudFront might remove the object from an edge location.If you're serving dynamic content and you're expecting "X-Cache:Miss from cloudfront" responses for your requests, consider the following to reduce latency without using caching:Set up more origin servers that are geographically closer to your requesters. Then, set up a single latency-based routing DNS record (for example, origin-latencybased-dnsrecord.example.com) that resolves to those servers. In CloudFront, configure the DNS name (for example, origin-latencybased-dnsrecord.example.com) as the origin for your distribution. This configuration allows CloudFront to fetch the content from the origin server with the lowest latency based on the edge location that the request lands on. If you're using Amazon Route 53 as your DNS provider, see Adding another Region to your latency-based routing in Amazon Route 53.Increase the keep-alive idle timeout for your CloudFront origin. This value specifies the amount of time that CloudFront maintains an idle connection with your origin server before closing the connection. The default keep-alive idle timeout is five seconds, but you can set a higher value up to 60 seconds if your origin servers support it. If your distribution forwards requests for dynamic content to the origin, then increasing the timeout value can help reduce latency. This is because CloudFront doesn't have to create a new connection for every request.Note: An origin can have multiple IP addresses assigned to it. To use persistent connections across multiple origin IP addresses, CloudFront relies on a higher request rate and increased frequency of requests. This is because the requests are routed round-robin across the origin IP addresses and connections are persisted for each IP address.

Related informationIncreasing the proportion of requests that are served from CloudFront edge caches (cache hit ratio)What information should I give AWS Support for diagnosing CloudFront web distribution latency issues?Managing how long content stays in an edge cache (expiration)Adding headers to your objects using the Amazon S3 console