Search code examples
amazon-web-servicesamazon-cloudfront

Why is a CloudFront distribution with an ALB custom origin slower than the ALB without CloudFront?


I have created an CloudFront distribution, with an ALB as custom origin. Caching is enabled with a minimum and default TTL of one day (in seconds of course).

After many tests, I realize that my CloudFront distribution is slower than the origin ALB.

Does any one have an explanation for that?


Solution

  • CloudFront adds an extra hop to the network path.

    Without CloudFront requests would go directly to the ALB and be served from the backend.

    User -> Internet -> ALB -> Backend
    

    Using CloudFront adds another step into the communication flow (on the "first" request).

    User -> Internet -> CloudFront edge location -> ALB -> Backend
    

    This can make initial requests slower when you're geographically (or in terms of the network path) close to the actual origin (ALB).

    Knowing this, CloudFront might sound like it's a useless service, but it actually has a different purpose. It caches information at these edge locations and is able to serve the content to users from there. Since there are hundreds of edge locations distributed all over the world, chances are one of them is closer to your end users and if data is cached there, the request doesn't have to travel back to your ALB.

    This makes it useful for a globally distributed user base and it has another benefit: Load on your backend is reduced, because static content is cached in CloudFront and you can use those expensive compute resources to do ...well... compute.

    Depending on where you're measuring from, CloudFront may be beneficial or not, but it's not necessarily about the speed for any individual user, but the speed for the average user and about reducing load on your expensive backend resources.