How to Check my Fastly CDN peformance (Cache hit ratio)

Goal

To review how effective the cache layer is, you may want to check the “Hit Ratio” of the content it is serving.

A busy read-only site with its cache preferences properly configured can be seen to serve percentages in the high 90’s; while if you see a cache ratio down below 60%, the server is probably working pretty hard, and some things can be done - either at the app level, of just in the Response-Headers, to improve cache-ability and responsiveness.

Assumptions

You will need:

  • A Platform.sh plan with managed Fastly cache enabled. This usually means an “Enterprise Dedicated” plan.
  • Read access to your hosting server - the ability to ssh in and read a file there.
    This is best done by installing and using the platform CLI tool, though plain ssh work as well.
  • Access to the curl tool. if you don’t have it installed locally, you can use it when ssh-ed in to your instance.

Challenge

For managed-CDN plans, Platform.sh looks after your Fastly subscriptions. Which is great, because cache management is hard. But this does mean that you don’t get direct access to some of the reporting tools that Fastly provides.

While you may not get dashboard access, you do get to use the Fastly API with an access token that’s been attached to your account.

Steps

1. SSH in to your production instance

platform ssh --environment=production

2. Retrieve your API token

This will have been added to your production instance, conventionally in the location
/mnt/shared/fastly_tokens.txt

$ cat /mnt/shared/fastly_tokens.txt

This should return something like:

Service: myprojectname
Service ID: C8L8ykr4PCC65vQ8TK97td
API Token: 74919c95e59ec799bc82641811e4e3b4

So, save that as variables:

$ FASTLY_SERVICE_ID="C8L8ykr4PCC65vQ8TK97td"
$ FASTLY_API_TOKEN="74919c95e59ec799bc82641811e4e3b4"

3. Use the Fastly API to ask for your statistics

Historical stats can be retrieved from the /stats endpoint

curl "https://api.fastly.com/stats/service/${FASTLY_SERVICE_ID}/field/hit_ratio?from=2+days+ago"  \
  -XGET \
  -H "Fastly-Key: ${FASTLY_API_TOKEN}" \
  -H "Accept: Application/json" \
  -k

4. Check the result

It’s machine-readable, not so much human-readable, but:
Hopefully you’ll see something like:

{
   "msg" : null,
   "status" : "success",
   "data" : [
      {
         "service_id" : "C8L8ykr4PCC65vQ8TK97td",
         "hit_ratio" : 0.818625522455654,
         "start_time" : 1589846900
      }
   ],
   "meta" : {
      "to" : "2020-05-22 12:21:00 UTC",
      "by" : "day",
      "from" : "2020-05-24 12:21:00 UTC",
      "region" : "all"
   }
}

the hit_ratio there indicates that 81% of all requests were cacheable, and being returned by the CDN.

Conclusion

Platform.sh plans with managed Fastly CDN accounts do provide you access to an amount of management options, but you have to work through the API to get at them.

This example gives you the recent “hit ratio”, but there are other stats available also, that can be retrieved in a similar way.

See also How to flush the Fastly Cache

1 Like