Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

docs: add debugging snippets for cache hit rate (#17287)

Ziincb6d8e9819df4
diff --git a/apps/docs/pages/guides/storage/cdn.mdx b/apps/docs/pages/guides/storage/cdn.mdx
index bd4527adee5..d349398410f 100644
--- a/apps/docs/pages/guides/storage/cdn.mdx
+++ b/apps/docs/pages/guides/storage/cdn.mdx
@@ -66,6 +66,58 @@ If your asset is updated frequently, we recommend that you re-upload the new ass
Objects in public buckets do not require any Authorization to access objects. This leads to a better cache hit rate compared to private buckets. For private buckets, permissions for accessing each object is checked on a per user level. For example, if two different users access the same object in a private bucket from the same region, it results in a cache miss for both the users since they might have different security policies attached to them. On the other hand, if two different users access the same object in a public bucket from the same region, it results in a cache hit for the second user.
+## Debugging Cache Hits
+
+The [storage report](https://supabase.com/dashboard/project/_/reports/storage) gives a breakdown of your project's cache hit rate.
+
+The [Logs Explorer](https://supabase.com/dashboard/project/_/logs/explorer) can also be used to debug cache misses.
+
+<Admonition type="note">The reporting date range depends on your plan's log retention.</Admonition>
+
+Cache hits can be determined via the `metadata.response.headers.cf_cache_status` key. Any value corresponding to either `HIT`, `STALE`, `REVALIDATED`, or `UPDATING` is considered a cache hit.
+The following example query will show the top cache misses from the `edge_logs`:
+
+```sql
+select
+ r.path as path,
+ r.search as search,
+ count(id) as count
+from
+ edge_logs as f
+ cross join unnest(f.metadata) as m
+ cross join unnest(m.request) as r
+ cross join unnest(m.response) as res
+ cross join unnest(res.headers) as h
+where
+ starts_with(r.path, '/storage/v1/object')
+ and r.method = 'GET'
+ and h.cf_cache_status in ('MISS', 'NONE/UNKNOWN', 'EXPIRED', 'BYPASS', 'DYNAMIC')
+group by path, search
+order by count desc
+limit 50;
+```
+
+Try out this query in the Logs Explorer [here]("https://supabase.com/dashboard/project/_/logs/explorer?q=%0Aselect%0A++r.path+as+path%2C%0A++r.search+as+search%2C%0A++count%28id%29+as+count%0Afrom%0A++edge_logs+as+f%0A++cross+join+unnest%28f.metadata%29+as+m%0A++cross+join+unnest%28m.request%29+as+r%0A++cross+join+unnest%28m.response%29+as+res%0A++cross+join+unnest%28res.headers%29+as+h%0Awhere%0A++starts_with%28r.path%2C+%27%2Fstorage%2Fv1%2Fobject%27%29%0A++and+r.method+%3D+%27GET%27%0A++and+h.cf_cache_status+in+%28%27MISS%27%2C+%27NONE%2FUNKNOWN%27%2C+%27EXPIRED%27%2C+%27BYPASS%27%2C+%27DYNAMIC%27%29%0Agroup+by+path%2C+search%0Aorder+by+count+desc%0Alimit+50%3B").
+
+Your cache hit ratio over time can then be determined using the following query:
+
+```sql
+select
+ timestamp_trunc(timestamp, hour) as timestamp,
+ countif(h.cf_cache_status in ('HIT', 'STALE', 'REVALIDATED', 'UPDATING')) / count(f.id) as ratio
+from
+ edge_logs as f
+ cross join unnest(f.metadata) as m
+ cross join unnest(m.request) as r
+ cross join unnest(m.response) as res
+ cross join unnest(res.headers) as h
+where starts_with(r.path, '/storage/v1/object') and r.method = 'GET'
+group by timestamp
+order by timestamp desc;
+```
+
+Try out this query in the Logs Explorer [here]("https://supabase.com/dashboard/project/_/logs/explorer?q=%0Aselect%0A++timestamp_trunc%28timestamp%2C+hour%29+as+timestamp%2C%0A++countif%28h.cf_cache_status+in+%28%27HIT%27%2C+%27STALE%27%2C+%27REVALIDATED%27%2C+%27UPDATING%27%29%29+%2F+count%28f.id%29+as+ratio%0Afrom%0A++edge_logs+as+f%0A++cross+join+unnest%28f.metadata%29+as+m%0A++cross+join+unnest%28m.request%29+as+r%0A++cross+join+unnest%28m.response%29+as+res%0A++cross+join+unnest%28res.headers%29+as+h%0Awhere+starts_with%28r.path%2C+%27%2Fstorage%2Fv1%2Fobject%27%29+and+r.method+%3D+%27GET%27%0Agroup+by+timestamp%0Aorder+by+timestamp+desc%3B").
+
export const Page = ({ children }) => <Layout meta={meta} children={children} />
export default Page