I'm always confused by CDNs and whether they're doing their job effectively.
I know that in theory, their purpose is to shorten the latency between the server and the user.
However, I usually like to test things before I try. So I uploaded an image to imgur.com and then tested the speed of the image file on 2 sites:
First test: https://tools.keycdn.com/performance
Second test: https://tools.pingdom.com
I've selected New York as the testing location on the second website. So I hit test on the first one - it gave me about 200-300ms latency on New York. Then I hit test on the 2nd website and it also gave me a quite high latency like 300ms or so.
When I hit test the 2nd time it lowered down to 15-30ms of course because that's what a CDN should do.
The question is though that those 2 servers were in the same location, but it looks like the image didn't get cached at all. Why is this happening or what am I missing here? I thought if it's cached then it should already reduce the latency to ANY other requesting server/user in that area. Am I wrong?
A CDN besides trying to deliver your content fast (shorten the latency) can help you also to protect/secure your origin by not exposing it directly, check this post for an introduction about other benefits: what is a CDN?
Regarding your test, there are many factors involved, for example, all new content (
MISS
) always will take more time to be served since it hasn't been cached, that content that as being already pre-fetched and cached (HIT
).You could start by checking the headers, for example in a terminal run this:
You may see an output like this:
Notice the header:
When the resource has been cached and served from the CDN it will be
HIT
if not it may beMISS
, here you can see a list of more possible responses that apply to Cloudflare: https://support.cloudflare.com/hc/en-us/articles/200168266-What-do-the-various-Cloudflare-cache-responses-HIT-Expired-etc-mean-Now to test how fast a resource is loading you could use
curl
, Depending on your shell you may want to add the next functioncurl_time()
into~/.profile
,~/.zshrc
or~/.bashrc
Then give a try with something like this:
In my subsequent requests, I got it delivered faster
time_total: 0.093495
:If just want to get the total_time repeditely you could try this:
Analyzing the headers and the response times is a good start point for checking how the CDN is behaving.