Has anyone tried to combine Amazon Cloudfront with Cloudflare, as below:
- In Cloudflare, add
example.comto Cloudflare and update the DNS to proxy the website.
- In Amazon, add a Cloudfront distribution and add
www.example.comas an origin.
- Rewrite the URLs of the static files on
example.comto use the URL of the distribution instead of
example.com, with a WordPress plugin, such as WP Rocket.
In such a way:
- The dynamic generated pages are proxied by Cloudflare CDN.
- The static assets are proxied by Cloudflare CDN, and then cached by Cloudfront CDN. And the visitor will finally retrieve the assets from Cloudfront Edge server. If Cloudfront Edge server miss the asset, it will try to obtain it from Cloudflare Edge server, if Cloudflare Edge server miss the asset, it will try to obtain it from the original server.
I have searched online but cannot find any other articles or posts discussing about this. But based on my tests, due to two level of CDN caches, the static assets in 2) are faster than just using one level(such as Cloudflare CDN).
But will there be any issues on such a way?
Based on my tests:
For small file(24.6kb), Cloudfront has the best performance. Better than Cloudfront + Cloudflare.
For large file(12.2MB), Cloudfront + Cloudflare has the best performance, about 8% faster than Cloudfront.
I perform test as below:
- Min TTL/Default TTL/Max TTL of Cloudfront set to 30 days. There are no way to set the edge TTL in Cloudfront.
- Set Edge TTL for Cloudflare to 14 days.
- Clear all the cache for both Cloudfront & Cloudflare.
- Immediately after step 3, run test from 227 servers around the world, and record the total download time.
- After 3 days, collect the data and analyze them.
One possibility of improvement in combining Cloudfront + Cloudflare is the route, which may use a better route than using Cloudfront or Cloudflare individually. And the route is more prominent for large files?
After studying the statistic data further, I find some interesting points:
- In most of the checkpoints, the performance of the Cloudfront + Cloudflare(abbreviation to C+C) vs Cloudfront(abbreviation to CF) are close. But in such checkpoints such as Jakarta, the C+C is 1.6s while CF is 12.39s. As the final result is generated based on average, a exceptional bad data makes the final result of CF worse than C+C.