I see increased latency in responses from Amazon CloudFront. I want to identify the cause and reduce latency.
Short description
To troubleshoot latency from CloudFront, identify the events that caused the latency. Then, follow these troubleshooting steps to reduce latency.
Resolution
Identify events that cause latency
Complete the following steps:
- Run the following curl command:
curl -w "DNS_resolution: %{time_namelookup}| TCP_negotiation_time: %{time_connect}| SSL_negotiation_time: %{time_appconnect}| TTFB: %{time_starttransfer}| Total time: %{time_total} \n" -o /dev/null -vsL https://www.example.com
Note: Replace example.com with your CloudFront domain name or alternate domain name (CNAME) and URL path.
- Check how long each stage of a network request takes in the developer tools of your web browser. For example, if you use Mozilla Firefox, then check the Timings tab. For more information, see the Timings tab on the Firefox website. If you observed latency in the past, then check the time-taken and time-to-first-byte fields in the CloudFront access logs. CloudFront access logs don't log the time taken by the client for the DNS lookup process and TCP and SSL/TLS negotiations.
Reduce latency in DNS resolution
Complete the following steps:
- Increase DNS caching time in the client-side DNS.
- Increase the Time to Live (TTL) of the cache in the local DNS server.
- Increase the TTL on the DNS record in your DNS registrar.
- If the resolver DNS server from your internet service provider causes latency, then use public DNS servers.
Reduce latency in TCP and SSL/TLS negotiation time
Complete the following steps:
Note: Replace example.com with your domain name.
Reduce latency in TTFB, TTL, and RTT
Complete these steps to reduce latency in Time Taken for First Byte (TTFB), Total Time Taken (TTL), Round Trip Time (RTT).
CloudFront returns an "X-Cache: Hit from cloudfront" response value
CloudFront returns "X-Cache:Hit from cloudfront" when requests come from the closest edge location. To reduce latency, complete the following steps:
CloudFront returns an "X-Cache: Miss from cloudfront" response value
CloudFront returns "X-Cache:Miss from cloudfront" when the request goes to the origin. To reduce latency, complete the following steps:
- Reduce the RTT between CloudFront edge location to your origin location. If a request from a CloudFront edge location goes to the nearest origin location, then the RTT is less. However, TTFB is affected if the request is from an edge location that's geographically distant from the origin. To optimize the RTT, replicate the origin server in multiple AWS Regions that are geographically closer to your users. Then, set up the DNS of your origin domain name so that it routes the request to origin servers based on latency or geolocation. If you use Amazon Route 53 as your DNS provider, then see Choosing a routing policy for more information.
- Turn on CloudFront automatic compression to compress files and reduce download speed. If your file format isn't supported by CloudFront automatic compression, then pre-compress the file in your origin and add a Content-Encoding header.
- To check the latency from the origin to CloudFront, activate the Origin latency metric. Note: Standard CloudWatch rates apply.
- Turn on CloudFront Origin Shield.
- Add response headers policies with the Server-Timing header feature turned on. This feature can help you understand the events that contribute to the latency between CloudFront and the origin.
Related information
How do I reduce the latency of slow responses from CloudFront?
What information do I give AWS Support to diagnose CloudFront web distribution latency issues?