I’m also using Cloudflare and Fly.io together in a personal project, and I’m loving it, but my setup is slightly different.
Regarding @michael1’s original numbers, it’s pretty amazing the Cloudflare proxy can sometimes save time (with caching disabled) even though it adds a network hop. That said, I’ve definitely seen it do the opposite (add latency), as it sounds like @michael1 originally expected it would. Maybe this means the rest of my post is stating the obvious, but I was surprised by some of the details.
First of all, Cloudflare’s DNS service is faster than almost anything else out there (typically <15ms vs. 50-100ms+ for other providers), so CF is my DNS provider.
However, I’ve decided not to let CF proxy traffic to my Fly containers (instead the DNS record goes straight to the Fly IP address), in part because I don’t particularly need CF’s proxy/caching, but also because of geography/geometry: it’s just really hard to improve on the ~30ms it takes me to ping the closest Fly data center (this latency varies by user location relative to Fly data centers, of course), even if the closest CF data center is closer to me than the Fly one. Lucky for me, those data centers happen to be located in the same city, but the extra hop through CF is still noticeable.
My general understanding is that, unless the CF proxy is located literally in the same data center, adding another hop along the way can only add geographical/network distance to the already great round-trip times you see when connecting directly to the Fly container—assuming Cloudflare ends up talking to the Fly container, rather than returning a cached response. That assumption isn’t exactly fair to Cloudflare, since caching is one of the main ways CF makes up for the extra latency their proxy adds, but it’s important here.
In case you’re wondering, I first discovered this performance surprise by “pausing” Cloudflare on my site, and then noticing my latencies dropped back to ~30ms (at best), from 50-80ms with CF in the loop. Note: these numbers are from memory, so please don’t quote me!
I’ve created a few subdomains that I let Cloudflare handle with edge workers, but the main domain is hosted directly by Fly. Ironically, I’m now using Fly to cache requests to these CF-powered subdomains, because my Fly containers can maintain in-memory/on-disk caches, whereas CF workers don’t live very long, so in-memory caching is less feasible/useful there.
With all of that said, if your Fly containers are swamped/slow and their responses can be effectively HTTP-cached, I would expect putting Cloudflare in front of Fly to speed things up and reduce load on the origin server, but (for better or worse) I don’t have those problems yet.
On a personal note, this is my first post in these forums, so I hope this has been fun/interesting content for folks to read. I see a few familiar avatars— @kentcdodds! Happy